--- /dev/null
+# syntax:glob
+
+# project files
+.idea/*
+*.sublime-*
+.viproj*
+.vimproj*
+.project
+.vscode/*
+*.iml
+
+# temporary directories and 3rd party source code directories
+tmp/*
+var/*
+cache/*
+log/*
+logs/*
+docs/*
+dev/.zim/*
+vendor/*
+
+# temporary files
+*.tmp
+*.bak
+*.orig
+*.tmp
+
+# npm generated files/folders
+node_modules/*
+npm-debug.log
+package-lock.json
+
+# composer/phpunit generated files/folders
+composer.phar
+composer.lock
+.phpunit.result.cache
+
+# system generated files
+.buildpath
+.settings
+.DS_Store
+Thumbs.db
+desktop.ini
+
+# project specific
+src/public/assets/scripts/vendor/*
--- /dev/null
+# Autoloader #
+
+----------------------------------------------------------------------
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit backupGlobals="false"
+ backupStaticAttributes="false"
+ bootstrap="vendor/autoload.php"
+ colors="true"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ processIsolation="false"
+ stopOnFailure="true">
+ <testsuites>
+ <testsuite name="autoloader unit tests">
+ <directory suffix=".php">./test/</directory>
+ <exclude>./test/utility</exclude>
+ <exclude>./test/fixture</exclude>
+ </testsuite>
+ </testsuites>
+ <filter>
+ <whitelist>
+ <directory suffix=".php">./src</directory>
+ <exclude>
+ <directory>./src/tmp</directory>
+ <directory>./test</directory>
+ <directory>./vendor</directory>
+ </exclude>
+ </whitelist>
+ </filter>
+ <logging>
+ <log type="coverage-text"
+ target="php://stdout"
+ showUncoveredFiles="true" />
+ <log type="coverage-clover"
+ target="logs/clover.xml" />
+ </logging>
+</phpunit>
*/
private static $__instance;
+ /**
+ * @codeCoverageIgnore
+ *
+ * Psr0 constructor.
+ */
private function __construct()
{
$this->__paths = array();
}
/**
+ * @codeCoverageIgnore
+ *
* @return Psr0
*/
public static function instance()
return static::$__instance;
}
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param string $namespace
+ * @param string $className
+ *
+ * @return array
+ */
private function __explodeUnderscores(&$namespace, &$className)
{
$parts = explode("_", $className);
);
}
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param string $className
+ *
+ * @return array
+ */
private function __getNamespaceAndClass(&$className)
{
$parts = explode("\\", $className);
);
}
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param string $fullClassName
+ *
+ * @return null|string
+ */
private function __findClassFile($fullClassName)
{
list($namespace, $className) =
$this->__getNamespaceAndClass($fullClassName);
$pathName = str_replace("\\", DIRECTORY_SEPARATOR, $namespace . "\\");
- $classPathNames = array(
- $pathName . ucfirst($className),
- $pathName . $className,
- );
+ $classPathNames = array_unique(array(
+ $pathName . ucfirst($className),
+ $pathName . $className,
+ ));
foreach ($classPathNames as $classPathName) {
- foreach ($this->__paths as &$p) {
+ foreach ($this->__paths as $p) {
+ if (is_array($p)) {
+ $prefix = array_keys($p)[0];
+ $p = $p[$prefix];
+
+ if (substr($classPathName, 0, strlen($prefix)) !== $prefix) {
+ continue;
+ }
+
+ $className = substr($className, strlen($prefix));
+ }
+
$file = $p . $classPathName . ".php";
- //\ChromePhp::info("loading '$fullClassName' from '$file'.");
if (file_exists($file)) {
- //\ChromePhp::info(" found '$fullClassName' at '$file'.");
return $file;
}
}
return null;
}
+ /**
+ * If you are calling this method then probably you have a
+ * problem. Try seeing a physician or go to the nearest mental
+ * facility.
+ *
+ * @codeCoverageIgnore
+ *
+ * @param $fullClassName
+ */
public function autoload($fullClassName)
{
$file = $this->__findClassFile($fullClassName);
!empty($file) and require_once $file;
}
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param string[] $paths
+ *
+ * @return array
+ */
private function __filterTrimPaths(array &$paths)
{
return array_map(function ($p) {
return rtrim($p, "/\\") . DIRECTORY_SEPARATOR;
}, array_filter($paths, function ($p) {
- return !empty($p);
+ return !empty($p) && is_string($p);
}));
}
/**
* add path to autoload path(s)
*
+ * @codeCoverageIgnore
+ *
* @param string|array $path Root path
+ *
+ * @deprecated register will be removed in future versions, use
+ * registerPath instead
*/
public function register($path)
{
+ $this->registerPath($path);
+ }
+
+ /**
+ * add path to autoload path(s)
+ *
+ * @param string|array $path Root path
+ *
+ */
+ public function registerPath($path)
+ {
if (!$this->__registered) {
spl_autoload_register(array($this, "autoload"));
/**
* remove path to autoload path(s)
*
+ * @codeCoverageIgnore
+ *
* @param string|array $path Root path
+ *
+ * @deprecated unregister will be removed in future versions, use
+ * unregisterPath instead
*/
public function unregister($path)
{
+ $this->unregisterPath($path);
+ }
+
+ /**
+ * remove path to autoload path(s)
+ *
+ * @param string|array $path Root path
+ *
+ */
+ public function unregisterPath($path)
+ {
if (!$this->__registered) {
spl_autoload_register(array($this, "autoload"));
!is_array($path) and $path = array($path);
- $trimmedPaths = $this->__filterTrimPaths($path);
- $this->__paths[] = array_diff($this->__paths, $trimmedPaths);
+ $trimmedPaths = $this->__filterTrimPaths($path);
+ $this->__paths = array_diff($this->__paths, $trimmedPaths);
+ }
+
+ public function registerNamespacePath($namspace)
+ {
+ throw new \Exception("unimplemented");
+ }
+
+ public function unregisterNamespacePath($namspace)
+ {
+ throw new \Exception("unimplemented");
+ }
+
+ /**
+ * clear all the search paths
+ *
+ * @return array cleared paths
+ */
+ public function clearPaths()
+ {
+ $paths = $this->__paths;
+ $this->__paths = array();
+
+ return $paths;
}
- public function debugPaths()
+ /**
+ * @return array
+ */
+ public function getPaths()
{
- print "<pre>";
- print_r($this->__paths);
- print "</pre>";
+ return $this->__paths;
}
}
--- /dev/null
+<?php
+
+namespace frostbane\autoloader\test;
+
+use frostbane\autoloader\Psr0;
+use PHPUnit_Framework_TestCase;
+
+class Miscellaneous
+ extends PHPUnit_Framework_TestCase
+{
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ /**
+ * @var Psr0
+ */
+ private $loader;
+
+ /**
+ * @var \ReflectionMethod
+ */
+ private $findClassFile;
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public static function
+ setUpBeforeClass()
+ {
+ $loader = Psr0::instance();
+
+ $rexl = new \ReflectionProperty($loader, "__registered");
+
+ $rexl->setAccessible(true);
+ $rexl->setValue($loader, true);
+ }
+
+ public function
+ setUp()
+ {
+ $this->loader = Psr0::instance();
+
+ $this->findClassFile = new \ReflectionMethod($this->loader, "__findClassFile");
+
+ $this->findClassFile->setAccessible(true);
+
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ testInstance()
+ {
+ $loader = Psr0::instance();
+
+ $this->assertInstanceOf("frostbane\\autoloader\\Psr0", $loader);
+ }
+
+ public function
+ testClearPaths()
+ {
+ $paths = array(__DIR__ . "/fixture/classes",
+ __DIR__ . "/fixture/utility");
+ $miniDataClass = "DataNoPrefix\\MiniData";
+ $miniUtilClass = "UtilNoPrefix\\MiniUtil";
+
+ $this->loader->registerPath($paths);
+
+ $minidatapath = $this->findClassFile->invoke($this->loader, $miniDataClass);
+ $miniutilpath = $this->findClassFile->invoke($this->loader, $miniUtilClass);
+
+ $this->assertNotNull($minidatapath);
+ $this->assertNotNull($miniutilpath);
+
+ $this->loader->clearPaths();
+
+ $minidatapath = $this->findClassFile->invoke($this->loader, $miniDataClass);
+ $miniutilpath = $this->findClassFile->invoke($this->loader, $miniUtilClass);
+
+ $this->assertNull($minidatapath);
+ $this->assertNull($miniutilpath);
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ tearDown()
+ {
+ }
+
+ public static function
+ tearDownAfterClass()
+ {
+ }
+}
--- /dev/null
+<?php
+
+namespace frostbane\autoloader\test;
+
+use frostbane\autoloader\Psr0;
+use PHPUnit_Framework_TestCase;
+
+class RegisterLowerCase
+ extends PHPUnit_Framework_TestCase
+{
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ /**
+ * @var Psr0
+ */
+ private $loader;
+
+ /**
+ * @var \ReflectionMethod
+ */
+ private $findClassFile;
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public static function
+ setUpBeforeClass()
+ {
+ $loader = Psr0::instance();
+
+ $rexl = new \ReflectionProperty($loader, "__registered");
+
+ $rexl->setAccessible(true);
+ $rexl->setValue($loader, true);
+ }
+
+ public function
+ setUp()
+ {
+ $this->loader = Psr0::instance();
+
+ $this->findClassFile = new \ReflectionMethod($this->loader, "__findClassFile");
+
+ $this->findClassFile->setAccessible(true);
+
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ testRegisterString()
+ {
+ $this->loader->registerPath(__DIR__ . "/fixture/classes");
+
+ $minidatafile = $this->findClassFile->invoke($this->loader, "DataNoPrefix\\PicoData");
+
+ $this->assertNotNull($minidatafile);
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ tearDown()
+ {
+ $this->loader->clearPaths();
+ }
+
+ public static function
+ tearDownAfterClass()
+ {
+ }
+}
+
--- /dev/null
+<?php
+
+namespace frostbane\autoloader\test;
+
+use frostbane\autoloader\Psr0;
+use PHPUnit_Framework_TestCase;
+
+class RegisterPath
+ extends PHPUnit_Framework_TestCase
+{
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ /**
+ * @var Psr0
+ */
+ private $loader;
+
+ /**
+ * @var \ReflectionMethod
+ */
+ private $findClassFile;
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public static function
+ setUpBeforeClass()
+ {
+ $loader = Psr0::instance();
+
+ $rexl = new \ReflectionProperty($loader, "__registered");
+
+ $rexl->setAccessible(true);
+ $rexl->setValue($loader, true);
+ }
+
+ public function
+ setUp()
+ {
+ $this->loader = Psr0::instance();
+
+ $this->findClassFile = new \ReflectionMethod($this->loader, "__findClassFile");
+
+ $this->findClassFile->setAccessible(true);
+
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ testRegisterEmptyPath()
+ {
+ $this->loader->registerPath(array("",
+ __DIR__ . "/fixture/classes",
+ null,
+ array()));
+
+ $minidatafile = $this->findClassFile->invoke($this->loader, "DataNoPrefix\\MiniData");
+
+ $this->assertNotNull($minidatafile);
+
+ $paths = $this->loader->getPaths();
+
+ $this->assertEquals(1, count($paths));
+ }
+
+ public function
+ testRegisterString()
+ {
+ $this->loader->registerPath(__DIR__ . "/fixture/classes");
+
+ $minidatafile = $this->findClassFile->invoke($this->loader, "DataNoPrefix\\MiniData");
+
+ $this->assertNotNull($minidatafile);
+ }
+
+ public function
+ testRegisterArray()
+ {
+ $this->loader->registerPath(array(__DIR__ . "/fixture/classes",
+ __DIR__ . "/fixture/utility"));
+
+ $minidatafile = $this->findClassFile->invoke($this->loader, "DataNoPrefix\\MiniData");
+
+ $this->assertNotNull($minidatafile);
+
+ $miniutilfle = $this->findClassFile->invoke($this->loader, "UtilNoPrefix\\MiniUtil");
+
+ $this->assertNotNull($miniutilfle);
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ tearDown()
+ {
+ $this->loader->clearPaths();
+ }
+
+ public static function
+ tearDownAfterClass()
+ {
+ }
+}
--- /dev/null
+<?php
+
+namespace frostbane\autoloader\test;
+
+use frostbane\autoloader\Psr0;
+use PHPUnit_Framework_TestCase;
+
+class RegisterUnderscorePath
+ extends PHPUnit_Framework_TestCase
+{
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ /**
+ * @var Psr0
+ */
+ private $loader;
+
+ /**
+ * @var \ReflectionMethod
+ */
+ private $findClassFile;
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public static function
+ setUpBeforeClass()
+ {
+ $loader = Psr0::instance();
+
+ $rexl = new \ReflectionProperty($loader, "__registered");
+
+ $rexl->setAccessible(true);
+ $rexl->setValue($loader, true);
+ }
+
+ public function
+ setUp()
+ {
+ $this->loader = Psr0::instance();
+
+ $this->findClassFile = new \ReflectionMethod($this->loader, "__findClassFile");
+
+ $this->findClassFile->setAccessible(true);
+
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ testRegisterString()
+ {
+ $this->loader->registerPath(__DIR__ . "/fixture/classes");
+
+ $millidata = $this->findClassFile->invoke($this->loader, "DataNoPrefix\\Text_MilliData");
+
+ $this->assertNotNull($millidata);
+ }
+
+ public function
+ testRegisterArray()
+ {
+ $this->loader->registerPath(array(__DIR__ . "/fixture/classes",
+ __DIR__ . "/fixture/utility"));
+
+ $millidata = $this->findClassFile->invoke($this->loader, "DataNoPrefix\\Text_MilliData");
+
+ $this->assertNotNull($millidata);
+
+ $milliutil = $this->findClassFile->invoke($this->loader, "UtilNoPrefix\\Compare_MilliUtil");
+
+ $this->assertNotNull($milliutil);
+ }
+
+ public function
+ testRegisterUnderscoreNamespace()
+ {
+ $this->loader->registerPath(__DIR__ . "/fixture/utility");
+
+ $picoutil = $this->findClassFile->invoke($this->loader, "UtilNoPrefix\\String_Util\\Search_PicoUtil");
+
+ $this->assertNotNull($picoutil);
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ tearDown()
+ {
+ $this->loader->clearPaths();
+ }
+
+ public static function
+ tearDownAfterClass()
+ {
+ }
+}
--- /dev/null
+<?php
+
+namespace frostbane\autoloader\test;
+
+use frostbane\autoloader\Psr0;
+use PHPUnit_Framework_TestCase;
+
+class UnregisterPath
+ extends PHPUnit_Framework_TestCase
+{
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ /**
+ * @var Psr0
+ */
+ private $loader;
+
+ /**
+ * @var \ReflectionMethod
+ */
+ private $findClassFile;
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public static function
+ setUpBeforeClass()
+ {
+ $loader = Psr0::instance();
+
+ $rexl = new \ReflectionProperty($loader, "__registered");
+
+ $rexl->setAccessible(true);
+ $rexl->setValue($loader, true);
+ }
+
+ public function
+ setUp()
+ {
+ $this->loader = Psr0::instance();
+
+ $this->findClassFile = new \ReflectionMethod($this->loader, "__findClassFile");
+
+ $this->findClassFile->setAccessible(true);
+
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ testUnregisterString()
+ {
+ $path = __DIR__ . "/fixture/classes";
+ $class = "DataNoPrefix\\MiniData";
+
+ $this->loader->registerPath($path);
+
+ $file = $this->findClassFile->invoke($this->loader, $class);
+
+ $this->assertNotNull($file);
+ $this->loader->unregisterPath($path);
+
+ $file = $this->findClassFile->invoke($this->loader, $class);
+
+ $this->assertNull($file);
+ }
+
+ public function
+ testUnregisterArray()
+ {
+ $paths = array(__DIR__ . "/fixture/classes",
+ __DIR__ . "/fixture/utility");
+ $miniDataClass = "DataNoPrefix\\MiniData";
+ $miniUtilClass = "UtilNoPrefix\\MiniUtil";
+
+ $this->loader->registerPath($paths);
+
+ $minidatapath = $this->findClassFile->invoke($this->loader, $miniDataClass);
+ $miniutilpath = $this->findClassFile->invoke($this->loader, $miniUtilClass);
+
+ $this->assertNotNull($minidatapath);
+ $this->assertNotNull($miniutilpath);
+
+ $this->loader->unregisterPath($paths);
+
+ $minidatapath = $this->findClassFile->invoke($this->loader, $miniDataClass);
+ $miniutilpath = $this->findClassFile->invoke($this->loader, $miniUtilClass);
+
+ $this->assertNull($minidatapath);
+ $this->assertNull($miniutilpath);
+ }
+
+ public function
+ testClearPaths()
+ {
+ $paths = array(__DIR__ . "/fixture/classes",
+ __DIR__ . "/fixture/utility");
+ $miniDataClass = "DataNoPrefix\\MiniData";
+ $miniUtilClass = "UtilNoPrefix\\MiniUtil";
+
+ $this->loader->registerPath($paths);
+
+ $minidatapath = $this->findClassFile->invoke($this->loader, $miniDataClass);
+ $miniutilpath = $this->findClassFile->invoke($this->loader, $miniUtilClass);
+
+ $this->assertNotNull($minidatapath);
+ $this->assertNotNull($miniutilpath);
+
+ $this->loader->clearPaths();
+
+ $minidatapath = $this->findClassFile->invoke($this->loader, $miniDataClass);
+ $miniutilpath = $this->findClassFile->invoke($this->loader, $miniUtilClass);
+
+ $this->assertNull($minidatapath);
+ $this->assertNull($miniutilpath);
+ }
+
+ /* ***************************************************************** */
+ /* */
+ /* ***************************************************************** */
+
+ public function
+ tearDown()
+ {
+ $this->loader->clearPaths();
+ }
+
+ public static function
+ tearDownAfterClass()
+ {
+ }
+}
--- /dev/null
+<?php
+
+namespace DataNoPrefix;
+
+class MiniData
+{
+}
--- /dev/null
+<?php
+
+namespace DataNoPrefix;
+
+class Text_MilliData
+{
+}
--- /dev/null
+<?php
+
+namespace DataNoPrefix;
+
+class PicoData
+{
+}
--- /dev/null
+<?php
+
+namespace fixture\prefix\Data;
+
+class MicroData
+{
+}
--- /dev/null
+<?php
+
+namespace UtilNoPrefix;
+
+class Compare_MilliUtil
+{
+}
--- /dev/null
+<?php
+
+namespace UtilNoPrefix;
+
+class MiniUtil
+{
+}
--- /dev/null
+<?php
+
+namespace UtilNoPrefix\String_Util;
+
+class Search_PicoUtil
+{
+}