浏览代码

[php] Add externs for `SplFileInfo` and `SplFileObject` classes + some SPL iterators (#10366)

* [php] Add more SPL iterators

* [php] Add externs for `SplFileInfo` and `SplFileObject` classes

* [php] Fixed the signature of `RecursiveDirectoryIterator.hasChildren` method

* [php] Fixed the `RecursiveDirectoryIterator.hasChildren` overload
Cédric Belin 4 年之前
父节点
当前提交
8a9722d4bd

+ 40 - 0
std/php/DirectoryIterator.hx

@@ -0,0 +1,40 @@
+/*
+ * Copyright (C)2005-2021 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package php;
+
+/**
+	The `DirectoryIterator` class provides a simple interface for viewing the contents of filesystem directories.
+	@see https://www.php.net/manual/en/class.directoryiterator.php
+**/
+@:native("DirectoryIterator")
+extern class DirectoryIterator extends SplFileInfo implements SeekableIterator<Int, DirectoryIterator> {
+	function new(directory: String);
+
+	function current(): DirectoryIterator;
+	function isDot(): Bool;
+	function key(): Int;
+	function next(): Void;
+	function rewind(): Void;
+	function seek(offset: Int): Void;
+	function valid(): Bool;
+}

+ 51 - 0
std/php/FilesystemIterator.hx

@@ -0,0 +1,51 @@
+/*
+ * Copyright (C)2005-2021 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package php;
+
+import haxe.extern.EitherType;
+
+/**
+	The Filesystem iterator.
+	@see https://www.php.net/manual/en/class.filesystemiterator.php
+**/
+@:native("FilesystemIterator")
+extern class FilesystemIterator extends DirectoryIterator implements SeekableIterator<String, Dynamic> {
+	@:phpClassConst static final CURRENT_AS_FILEINFO: Int;
+	@:phpClassConst static final CURRENT_AS_PATHNAME: Int;
+	@:phpClassConst static final CURRENT_AS_SELF: Int;
+	@:phpClassConst static final CURRENT_MODE_MASK: Int;
+	@:phpClassConst static final FOLLOW_SYMLINKS: Int;
+	@:phpClassConst static final KEY_AS_FILENAME: Int;
+	@:phpClassConst static final KEY_AS_PATHNAME: Int;
+	@:phpClassConst static final KEY_MODE_MASK: Int;
+	@:phpClassConst static final NEW_CURRENT_AND_KEY: Int;
+	@:phpClassConst static final SKIP_DOTS: Int;
+	@:phpClassConst static final UNIX_PATHS: Int;
+
+	function new(directory: String, ?flags: Int);
+
+	function current(): EitherType<FilesystemIterator, EitherType<SplFileInfo, String>>;
+	function getFlags(): Int;
+	function key(): String;
+	function setFlags(flags: Int): Void;
+}

+ 39 - 0
std/php/RecursiveDirectoryIterator.hx

@@ -0,0 +1,39 @@
+/*
+ * Copyright (C)2005-2021 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package php;
+
+/**
+	The `RecursiveDirectoryIterator` provides an interface for iterating recursively over filesystem directories.
+	@see https://www.php.net/manual/en/class.recursivedirectoryiterator.php
+**/
+@:native("RecursiveDirectoryIterator")
+extern class RecursiveDirectoryIterator extends FilesystemIterator implements RecursiveIterator<String, Dynamic> implements SeekableIterator<String, Dynamic> {
+	function new(directory: String, ?flags: Int);
+
+	function getChildren(): Null<RecursiveDirectoryIterator>;
+	function getSubPath(): String;
+	function getSubPathname(): String;
+
+	@:overload(function(allowLinks: Bool): Bool {})
+	function hasChildren(): Bool;
+}

+ 33 - 0
std/php/RecursiveIterator.hx

@@ -0,0 +1,33 @@
+/*
+ * Copyright (C)2005-2021 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package php;
+
+/**
+	Classes implementing `RecursiveIterator` can be used to iterate over iterators recursively.
+	@see https://www.php.net/manual/en/class.recursiveiterator.php
+**/
+@:native("RecursiveIterator")
+extern interface RecursiveIterator<K, V> extends NativeIterator<K, V> {
+	function getChildren(): Null<RecursiveIterator<K, V>>;
+	function hasChildren(): Bool;
+}

+ 73 - 0
std/php/SplFileInfo.hx

@@ -0,0 +1,73 @@
+/*
+ * Copyright (C)2005-2021 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package php;
+
+import haxe.extern.EitherType;
+
+/**
+	The `SplFileInfo` class offers a high-level object-oriented interface to information for an individual file.
+	@see https://www.php.net/manual/en/class.splfileinfo.php
+**/
+@:native("SplFileInfo")
+extern class SplFileInfo {
+	function new(filename: String);
+
+	function getATime(): EitherType<Int, Bool>;
+	function getBasename(suffix: String = ""): String;
+	function getCTime(): EitherType<Int, Bool>;
+	function getExtension(): String;
+	function getFileInfo(?className: String): SplFileInfo;
+	function getFilename(): String;
+	function getGroup(): EitherType<Int, Bool>;
+	function getInode(): EitherType<Int, Bool>;
+	function getLinkTarget(): EitherType<String, Bool>;
+	function getMTime(): EitherType<Int, Bool>;
+	function getOwner(): EitherType<Int, Bool>;
+	function getPath(): String;
+	function getPathInfo(?className: String): Null<SplFileInfo>;
+	function getPathname(): String;
+	function getPerms(): EitherType<Int, Bool>;
+	function getRealPath(): EitherType<String, Bool>;
+	function getSize(): EitherType<Int, Bool>;
+	function getType(): EitherType<SplFileInfoType, Bool>;
+	function isDir(): Bool;
+	function isExecutable(): Bool;
+	function isFile(): Bool;
+	function isLink(): Bool;
+	function isReadable(): Bool;
+	function isWritable(): Bool;
+	function openFile(mode: String = "r", useIncludePath: Bool = false, ?context: Resource): SplFileObject;
+	function setFileClass(?className: String): Void;
+	function setInfoClass(?className: String): Void;
+}
+
+enum abstract SplFileInfoType(String) to String {
+	var Block = "block";
+	var Char = "char";
+	var Dir = "dir";
+	var Fifo = "fifo";
+	var File = "file";
+	var Link = "link";
+	var Socket = "socket";
+	var Unknown = "unknown";
+}

+ 71 - 0
std/php/SplFileObject.hx

@@ -0,0 +1,71 @@
+/*
+ * Copyright (C)2005-2021 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package php;
+
+import haxe.extern.EitherType;
+import haxe.extern.Rest;
+
+/**
+	The `SplFileObject` class offers an object-oriented interface for a file.
+	@see https://www.php.net/manual/en/class.splfileobject.php
+**/
+@:native("SplFileObject")
+extern class SplFileObject extends SplFileInfo implements RecursiveIterator<Int, Dynamic> implements SeekableIterator<Int, Dynamic> {
+	@:phpClassConst static final DROP_NEW_LINE: Int;
+	@:phpClassConst static final READ_AHEAD: Int;
+	@:phpClassConst static final READ_CSV: Int;
+	@:phpClassConst static final SKIP_EMPTY: Int;
+
+	function new(filename: String, mode: String = "r", useIncludePath: Bool = false, ?context: Resource);
+
+	function current(): EitherType<String, EitherType<NativeIndexedArray<String>, Bool>>;
+	function eof(): Bool;
+	function fflush(): Bool;
+	function fgetc(): EitherType<String, Bool>;
+	function fgetcsv(separator: String = ",", enclosure: String = "\"", escape: String = "\\"): EitherType<NativeIndexedArray<String>, Bool>;
+	function fgets(): String;
+	function fgetss(?allowable_tags: String): String;
+	function flock(operation: Int, ?wouldBlock: Ref<Int>): Bool;
+	function fpassthru(): Int;
+	function fputcsv(fields: NativeIndexedArray<String>, separator: String = ",", enclosure: String = "\"", escape: String = "\\"): EitherType<Int, Bool>;
+	function fread(length: Int): EitherType<String, Bool>;
+	function fscanf(format: String, vars: Rest<Ref<Dynamic>>): EitherType<Int, NativeIndexedArray<Dynamic>>;
+	function fseek(offset: Int, ?whence: Int): Int;
+	function fstat(): NativeArray;
+	function ftell(): EitherType<Int, Bool>;
+	function ftruncate(size: Int): Bool;
+	function fwrite(data: String, length: Int = 0): EitherType<Int, Bool>;
+	function getChildren(): Null<RecursiveIterator<Int, Dynamic>>;
+	function getCsvControl(): NativeIndexedArray<String>;
+	function getFlags(): Int;
+	function getMaxLineLen(): Int;
+	function hasChildren(): Bool;
+	function key(): Int;
+	function next(): Void;
+	function rewind(): Void;
+	function seek(line: Int): Void;
+	function setCsvControl(separator: String = ",", enclosure: String = "\"", escape: String = "\\"): Void;
+	function setFlags(flags: Int): Void;
+	function setMaxLineLen(maxLength: Int): Void;
+	function valid(): Bool;
+}