From 12b7ac617e2f68fabe4acf14a2c2f08a3cf3a833 Mon Sep 17 00:00:00 2001 From: frostbane Date: Fri, 11 Oct 2019 16:56:23 +0900 Subject: [PATCH] psr0 autoloader --- LICENSE.md | 127 +++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 36 ++++++++++++++ src/autoloader/Psr0.php | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 292 insertions(+) create mode 100644 LICENSE.md create mode 100644 composer.json create mode 100644 src/autoloader/Psr0.php diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..34b58bb --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,127 @@ +# Copyright (c) 2017,2018,2019 Frostbane Ac. All rights reserved. # +All rights reserved. + +#### Definitions #### + +- **Copyright Holder** means the individual(s) or organization(s) named +in the copyright notice for the entire Package. + +- **Contributor** means any party that has contributed code or other +material to the Package, in accordance with the Copyright Holder's +procedures. + +- **You** and **your** means any person who would like to copy, +distribute, or modify the Package. + +- **Package** means the collection of files distributed by the Copyright +Holder, and derivatives of that collection and/or of those files. A +given Package may consist of either the Standard Version, or a Modified +Version. + +- **(Re)Distribute** means providing a copy of the Package or making it +accessible to anyone else, or in the case of a company or organization, +to others outside of your company or organization. + +- **Distributor Fee** means any fee that you charge for Distributing +this Package or providing support for this Package to another party. It +does not mean licensing fees. + +- **Standard Version** refers to the Package if it has not been +modified, or has been modified only in ways explicitly requested by the +Copyright Holder. + +- **Modified Version** means the Package, if it has been changed, and +such changes were not explicitly requested by the Copyright Holder. + +- **Original License** means this License as Distributed with the +Standard Version of the Package, in its current version or as it may be +modified by Frostbane Ac in the future. + +- **Source** form means the source code, documentation source, and +configuration files for the Package. + +- **Compiled** form means the compiled bytecode, object code, binary, or +any other form resulting from mechanical transformation or translation +of the Source form. + +#### Terms and Conditions #### + +Redistribution and use in source and binary forms are permitted provided +that the following terms and conditions are met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form, or linking to the package must +reproduce the above copyright notice, this list of conditions and the +following disclaimer in the documentation and/or other materials +provided with the distribution. + +3. All advertising materials mentioning features or use of this software +must display the following acknowledgement: + +``` +This product includes software developed by `Frostbane Ac`. +``` + +4. Neither the name of `frostbane\\Functional` nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +5. Copying the source code and distributing it with another name results +to direct copyright infridgement. + +6. Modification of the source code is allowed without restrictions, +provided that you do not distribute the modified version. + +7. Modified versions may be distributed, provided that you include a +list of modifications together with the distribution and the modified +source code must be included with the distribution and available in +to the public. + +8. You may distribute the modified version without the source code +provided that you must include complete instructions on how to get +the Modified source. Such instructions must be valid at the time +of your distribution. If these instructions, at any time while you +are carrying out such distribution, become invalid, you must provide +new instructions on demand or cease further distribution. If you +provide valid instructions or cease distribution within thirty days +after you become aware that the instructions are invalid, then you +do not forfeit any of your rights under this license. + +9. Modifiying the source code and charging a license fee for the +modifications is not prohibited. The terms and conditions of this +license apply to the source-code modified or not. You may however charge +licensing fees for other components. + +10. Linking to the package, modified or not, is permitted as long as the +resulting binary does not expose a direct interface to the package. +The terms and conditions of this license will also apply to the +resulting binary. + +11. Modules and scripts extending or merely using this package, and do +not modify the package is not subject to this license. + +12. If your Modified Version has been derived from a Modified Version +made by someone other than you, you are nevertheless required to ensure +that your Modified Version complies with the requirements of this +license. + +12. Modifiying the original work to contain hidden harmful content is +prohibited. + +#### Disclaimer of Warranty #### + +``` +THIS SOFTWARE IS PROVIDED BY FROSTBANE ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL {COPYRIGHT HOLDER} BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b0206c6 --- /dev/null +++ b/composer.json @@ -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 index 0000000..3579466 --- /dev/null +++ b/src/autoloader/Psr0.php @@ -0,0 +1,129 @@ +__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 "
";
+        print_r($this->__paths);
+        print "
"; + } +} -- 2.11.0