OSDN Git Service

psr0 autoloader v1.0.0
authorfrostbane <frostbane@programmer.net>
Fri, 11 Oct 2019 07:56:23 +0000 (16:56 +0900)
committerfrostbane <frostbane@programmer.net>
Fri, 11 Oct 2019 07:56:23 +0000 (16:56 +0900)
LICENSE.md [new file with mode: 0644]
composer.json [new file with mode: 0644]
src/autoloader/Psr0.php [new file with mode: 0644]

diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644 (file)
index 0000000..34b58bb
--- /dev/null
@@ -0,0 +1,127 @@
+# Copyright (c) 2017,2018,2019 Frostbane Ac. All rights reserved. #\r
+All rights reserved.\r
+\r
+#### Definitions ####\r
+\r
+- **Copyright Holder** means the individual(s) or organization(s) named\r
+in the copyright notice for the entire Package.\r
+\r
+- **Contributor** means any party that has contributed code or other\r
+material to the Package, in accordance with the Copyright Holder's\r
+procedures.\r
+\r
+- **You** and **your** means any person who would like to copy,\r
+distribute, or modify the Package.\r
+\r
+- **Package** means the collection of files distributed by the Copyright\r
+Holder, and derivatives of that collection and/or of those files. A\r
+given Package may consist of either the Standard Version, or a Modified\r
+Version.\r
+\r
+- **(Re)Distribute** means providing a copy of the Package or making it\r
+accessible to anyone else, or in the case of a company or organization,\r
+to others outside of your company or organization.\r
+\r
+- **Distributor Fee** means any fee that you charge for Distributing\r
+this Package or providing support for this Package to another party. It\r
+does not mean licensing fees.\r
+\r
+- **Standard Version** refers to the Package if it has not been\r
+modified, or has been modified only in ways explicitly requested by the\r
+Copyright Holder.\r
+\r
+- **Modified Version** means the Package, if it has been changed, and\r
+such changes were not explicitly requested by the Copyright Holder.\r
+\r
+- **Original License** means this License as Distributed with the\r
+Standard Version of the Package, in its current version or as it may be\r
+modified by Frostbane Ac in the future.\r
+\r
+- **Source** form means the source code, documentation source, and\r
+configuration files for the Package.\r
+\r
+- **Compiled** form means the compiled bytecode, object code, binary, or\r
+any other form resulting from mechanical transformation or translation\r
+of the Source form.\r
+\r
+#### Terms and Conditions ####\r
+\r
+Redistribution and use in source and binary forms are permitted provided\r
+that the following terms and conditions are met:\r
+\r
+1. Redistributions of source code must retain the above copyright\r
+notice, this list of conditions and the following disclaimer.\r
+\r
+2. Redistributions in binary form, or linking to the package must\r
+reproduce the above copyright notice, this list of conditions and the\r
+following disclaimer in the documentation and/or other materials\r
+provided with the distribution.\r
+\r
+3. All advertising materials mentioning features or use of this software\r
+must display the following acknowledgement:\r
+\r
+```\r
+This product includes software developed by `Frostbane Ac`.\r
+```\r
+\r
+4. Neither the name of `frostbane\\Functional` nor the names of its\r
+contributors may be used to endorse or promote products derived from\r
+this software without specific prior written permission.\r
+\r
+5. Copying the source code and distributing it with another name results\r
+to direct copyright infridgement.\r
+\r
+6. Modification of the source code is allowed without restrictions,\r
+provided that you do not distribute the modified version.\r
+\r
+7. Modified versions may be distributed, provided that you include a\r
+list of modifications together with the distribution and the modified\r
+source code must be included with the distribution and available in\r
+to the public.\r
+\r
+8. You may distribute the modified version without the source code\r
+provided that you must include complete instructions on how to get\r
+the Modified source. Such instructions must be valid at the time\r
+of your distribution. If these instructions, at any time while you\r
+are carrying out such distribution, become invalid, you must provide\r
+new instructions on demand or cease further distribution. If you\r
+provide valid instructions or cease distribution within thirty days\r
+after you become aware that the instructions are invalid, then you\r
+do not forfeit any of your rights under this license.\r
+\r
+9. Modifiying the source code and charging a license fee for the\r
+modifications is not prohibited. The terms and conditions of this\r
+license apply to the source-code modified or not. You may however charge\r
+licensing fees for other components.\r
+\r
+10. Linking to the package, modified or not, is permitted as long as the\r
+resulting binary does not expose a direct interface to the package.\r
+The terms and conditions of this license will also apply to the\r
+resulting binary.\r
+\r
+11. Modules and scripts extending or merely using this package, and do\r
+not modify the package is not subject to this license.\r
+\r
+12. If your Modified Version has been derived from a Modified Version\r
+made by someone other than you, you are nevertheless required to ensure\r
+that your Modified Version complies with the requirements of this\r
+license.\r
+\r
+12. Modifiying the original work to contain hidden harmful content is\r
+prohibited.\r
+\r
+#### Disclaimer of Warranty ####\r
+\r
+```\r
+THIS SOFTWARE IS PROVIDED BY FROSTBANE ''AS IS'' AND ANY\r
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL {COPYRIGHT HOLDER} BE LIABLE\r
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR\r
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\r
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+```\r
diff --git a/composer.json b/composer.json
new file mode 100644 (file)
index 0000000..b0206c6
--- /dev/null
@@ -0,0 +1,36 @@
+{
+       "name"             : "frostbane/autoloader",
+       "description"      : "Autoloader.",
+       "type"             : "package",
+       "keywords"         : [
+               "autoloader",
+               "php",
+               "fuel",
+               "fuelphp",
+               "psr0"
+       ],
+       "license"          : "BSD-4-Clause",
+       "authors"          : [
+               {
+                       "name" : "frostbane",
+                       "email": "frostbane@programmer.net"
+               }
+       ],
+       "minimum-stability": "dev",
+       "require"          : {
+               "php": ">=5.4.16"
+       },
+       "require-dev"      : {
+               "phpunit/phpunit": "^4.8.36"
+       },
+       "autoload"         : {
+               "psr-4": {
+                       "frostbane\\": "src/"
+               }
+       },
+       "autoload-dev"     : {
+               "psr-4": {
+                       "frostbane\\autoloader\\test\\": "test/"
+               }
+       }
+}
diff --git a/src/autoloader/Psr0.php b/src/autoloader/Psr0.php
new file mode 100644 (file)
index 0000000..3579466
--- /dev/null
@@ -0,0 +1,129 @@
+<?php
+
+namespace frostbane\autoloader;
+
+class Psr0
+{
+    private $__paths;
+    private $__registered;
+
+    /**
+     * @var Psr0
+     */
+    private static $__instance;
+
+    private function __construct()
+    {
+        $this->__paths      = array();
+        $this->__registered = false;
+    }
+
+    /**
+     * @return Psr0
+     */
+    public static function instance()
+    {
+        empty(static::$__instance) and static::$__instance = new self();
+
+        return static::$__instance;
+    }
+
+    private function __explodeUnderscores(&$namespace, &$className)
+    {
+        $parts     = explode("_", $className);
+        $className = array_pop($parts);
+
+        foreach ($parts as &$folder) {
+            if (empty($folder)) {
+                continue;
+            }
+
+            $namespace .= "\\" . $folder;
+        }
+
+        return array(
+            $namespace,
+            $className,
+        );
+    }
+
+    private function __getNamespaceAndClass(&$className)
+    {
+        $parts     = explode("\\", $className);
+        $className = array_pop($parts);
+        $namespace = implode("\\", $parts);
+
+        if (strpos($className, "_") !== false) {
+            list($namespace, $className) = $this->__explodeUnderscores($namespace, $className);
+        }
+
+        return array(
+            $namespace,
+            $className,
+        );
+    }
+
+    private function __findClassFile($fullClassName)
+    {
+        list($namespace, $className) =
+            $this->__getNamespaceAndClass($fullClassName);
+
+        $pathName       = str_replace("\\", DIRECTORY_SEPARATOR, $namespace . "\\");
+        $classPathNames = array(
+            $pathName . ucfirst($className),
+            $pathName . $className,
+        );
+
+        foreach ($classPathNames as $classPathName) {
+            foreach ($this->__paths as &$p) {
+                $file = $p . $classPathName . ".php";
+                //\ChromePhp::info("loading '$fullClassName' from '$file'.");
+
+                if (file_exists($file)) {
+                    //\ChromePhp::info("    found '$fullClassName' at '$file'.");
+                    return $file;
+                }
+            }
+        }
+
+        return null;
+    }
+
+    public function autoload($fullClassName)
+    {
+        $file = $this->__findClassFile($fullClassName);
+
+        !empty($file) and require_once $file;
+    }
+
+    /**
+     * @param string|array $path Root path
+     */
+    public function register($path)
+    {
+        if ( !$this->__registered) {
+            spl_autoload_register(array($this, "autoload"));
+
+            $this->__registered = true;
+        }
+
+        !is_array($path) and $path = array($path);
+
+        foreach ($path as &$p) {
+            if (empty($p)) {
+                continue;
+            }
+
+            $trimmedPath = rtrim($p, "/\\") . DIRECTORY_SEPARATOR;
+
+            in_array($trimmedPath, $this->__paths) or $this->__paths[] = $trimmedPath;
+        }
+    }
+
+    public function debugPaths()
+    {
+        print "<pre>";
+        print_r($this->__paths);
+        print "</pre>";
+    }
+}