浏览代码

init repo

olinox14 1 年之前
当前提交
eeb3c7f00a
共有 10 个文件被更改,包括 335 次插入0 次删除
  1. 8 0
      .idea/.gitignore
  2. 1 0
      .idea/.name
  3. 8 0
      .idea/modules.xml
  4. 11 0
      .idea/path.iml
  5. 20 0
      .idea/php.xml
  6. 10 0
      .idea/phpunit.xml
  7. 6 0
      .idea/vcs.xml
  8. 28 0
      composer.json
  9. 0 0
      readme.md
  10. 243 0
      src/Path/Path.php

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 1 - 0
.idea/.name

@@ -0,0 +1 @@
+path

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/../path/.idea/path.iml" filepath="$PROJECT_DIR$/../path/.idea/path.iml" />
+    </modules>
+  </component>
+</project>

+ 11 - 0
.idea/path.iml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="Path\" />
+      <sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="Path\Tests\" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 20 - 0
.idea/php.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="MessDetectorOptionsConfiguration">
+    <option name="transferred" value="true" />
+  </component>
+  <component name="PHPCSFixerOptionsConfiguration">
+    <option name="transferred" value="true" />
+  </component>
+  <component name="PHPCodeSnifferOptionsConfiguration">
+    <option name="highlightLevel" value="WARNING" />
+    <option name="transferred" value="true" />
+  </component>
+  <component name="PhpProjectSharedConfiguration" php_language_level="8.0" />
+  <component name="PhpStanOptionsConfiguration">
+    <option name="transferred" value="true" />
+  </component>
+  <component name="PsalmOptionsConfiguration">
+    <option name="transferred" value="true" />
+  </component>
+</project>

+ 10 - 0
.idea/phpunit.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="PHPUnit">
+    <option name="directories">
+      <list>
+        <option value="$PROJECT_DIR$/tests" />
+      </list>
+    </option>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 28 - 0
composer.json

@@ -0,0 +1,28 @@
+{
+  "name": "olinox14/path",
+  "description": "Simple, intuitive and object-oriented file and path operations, inspired by the path?py python library",
+  "type": "library",
+  "license": "MIT",
+  "authors": [
+    {
+      "name": "Olivier Massot",
+      "email": "olinox14@tuta.io"
+    }
+  ],
+  "require": {},
+  "autoload": {
+    "psr-4": {
+      "Path\\": "src/"
+    }
+  },
+  "autoload-dev": {
+    "psr-4": {
+      "Path\\Tests\\": "tests/"
+    }
+  },
+  "require-dev": {
+    "phpunit/phpunit": "^9.0"
+  },
+  "minimum-stability": "dev",
+  "prefer-stable": true
+}

+ 0 - 0
readme.md


+ 243 - 0
src/Path/Path.php

@@ -0,0 +1,243 @@
+<?php
+
+namespace Path\Path;
+
+class Path
+{
+    protected string $path;
+
+    public function __construct(string $path)
+    {
+        $this->path = $path;
+    }
+
+    /**
+     * Resolve the path.
+     *
+     * @return bool|string Returns the resolved path as a string if successful, otherwise returns false.
+     */
+    public function resolve(): bool|string
+    {
+        return realpath($this->path);
+    }
+
+    /**
+     * Check if the path refers to a regular file.
+     *
+     * @return bool Returns true if the path refers to a regular file, otherwise returns false.
+     */
+    public function isFile(): bool
+    {
+        return is_file($this->path);
+    }
+
+    /**
+     * Check if the given path is a directory.
+     *
+     * @return bool Returns true if the path is a directory, false otherwise.
+     */
+    public function isDir()
+    {
+        return is_dir($this->path);
+    }
+
+    /**
+     * Get the extension of the given path.
+     *
+     * @return array|string Returns the extension of the path as a string if it exists, or an empty string otherwise.
+     */
+    public function extension(): array|string
+    {
+        return pathinfo($this->path, PATHINFO_EXTENSION);
+    }
+
+    /**
+     * Get the name of the file or path.
+     *
+     * @return array|string Returns the name of the file or path.
+     * If the path has an extension, it returns the name without the extension as a string.
+     * If the path doesn't have an extension, it returns the name as an array containing the directory name and the file name.
+     */
+    public function name(): array|string
+    {
+        return pathinfo($this->path, PATHINFO_FILENAME);
+    }
+
+    /**
+     * Get the base name of the path.
+     *
+     * @return string The base name of the path.
+     */
+    public function basename(): string
+    {
+        return pathinfo($this->path, PATHINFO_BASENAME);
+    }
+
+    /**
+     * Creates a new directory.
+     *
+     * @param int $mode (optional) The permissions for the new directory. Default is 0777.
+     * @param bool $recursive (optional) Indicates whether to create parent directories if they do not exist. Default is false.
+     *
+     * @return void
+     */
+    public function mkdir($mode = 0777, $recursive = false): void
+    {
+        if (!file_exists($this->path)) {
+            mkdir($this->path, $mode, $recursive);
+        }
+    }
+
+    /**
+     * Deletes a file or a directory.
+     *
+     * @return void
+     */
+    public function delete(): void
+    {
+        if (is_file($this->path)) {
+            unlink($this->path); //for file
+        }
+        else if (is_dir($this->path)) {
+            rmdir($this->path); //for directory
+        }
+    }
+
+    /**
+     * Copies a file to a specified destination.
+     *
+     * @param string $destination The path to the destination file or directory to copy to.
+     *
+     * @return void
+     */
+    public function copy($destination): void
+    {
+        if (is_file($this->path)) {
+            copy($this->path, $destination);
+        }
+        else if (is_dir($this->path)) {
+            // Copy dir needs special handling, not covered in this example.
+        }
+    }
+
+    /**
+     * Moves a file or directory to a new location.
+     *
+     * @param string $destination The new location where the file or directory should be moved to.
+     *
+     * @return void
+     */
+    public function move($destination): void
+    {
+        rename($this->path, $destination);
+    }
+
+    /**
+     * Updates the access and modification time of a file or creates a new empty file if it doesn't exist.
+     *
+     * @param int|null $time (optional) The access and modification time to set. Default is the current time.
+     * @param int|null $atime (optional) The access time to set. Default is the value of $time.
+     *
+     * @return void
+     */
+    public function touch($time = null, $atime = null): void
+    {
+        if (!file_exists($this->path)) {
+            touch($this->path, $time, $atime);
+        }
+    }
+
+    /**
+     * Returns the last modified timestamp of a file or directory.
+     *
+     * @return int|bool The last modified timestamp, or false if an error occurred.
+     */
+    public function lastModified(): bool|int
+    {
+        return filemtime($this->path);
+    }
+
+    /**
+     * Calculates the size of a file.
+     *
+     * @return bool|int The size of the file in bytes. Returns false if the file does not exist or on failure.
+     */
+    public function size(): bool|int
+    {
+        return filesize($this->path);
+    }
+
+    /**
+     * Retrieves the parent directory of a file or directory path.
+     *
+     * @return string The parent directory of the specified path.
+     */
+    public function parent(): string
+    {
+        return dirname($this->path);
+    }
+
+    /**
+     * Retrieves the contents of a file.
+     *
+     * @return bool|string The contents of the file as a string. Returns false if the file does not exist or on failure.
+     */
+    public function getContents(): bool|string
+    {
+        return file_get_contents($this->path);
+    }
+
+    /**
+     * Writes contents to a file.
+     *
+     * @param mixed $contents The contents to be written to the file.
+     * @return void
+     */
+    public function putContents($contents): void
+    {
+        file_put_contents($this->path, $contents);
+    }
+
+    /**
+     * Appends contents to a file.
+     *
+     * @param string $contents The contents to append to the file.
+     *
+     * @return void
+     */
+    public function appendContents($contents): void
+    {
+        file_put_contents($this->path, $contents, FILE_APPEND);
+    }
+
+    /**
+     * Retrieves the permissions of a file or directory.
+     *
+     * @return string The permissions of the file or directory in octal notation. Returns an empty string if the file or directory does not exist.
+     */
+    public function getPermissions(): string
+    {
+        return substr(sprintf('%o', fileperms($this->path)), -4);
+    }
+
+    /**
+     * Changes the permissions of a file or directory.
+     *
+     * @param int $permissions The new permissions to set. The value should be an octal number.
+     * @return bool Returns true on success, false on failure.
+     */
+    public function changePermissions($permissions): bool
+    {
+        return chmod($this->path, $permissions);
+    }
+
+    /**
+     * Checks if a file exists.
+     *
+     * @return bool Returns true if the file exists, false otherwise.
+     */
+    public function exists(): bool
+    {
+        return file_exists($this->path);
+    }
+}