Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
26 / 26 |
| Psr4 | |
100.00% |
1 / 1 |
|
100.00% |
8 / 8 |
9 | |
100.00% |
26 / 26 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| setBaseDir | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getBaseDir | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setFileExtension | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getFileExtension | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| register | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| unregister | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| loadClass | |
100.00% |
1 / 1 |
2 | |
100.00% |
8 / 8 |
|||
| <?php | |
| namespace Puyo\Loader; | |
| /** | |
| * PSR-4準拠のautoloader | |
| * | |
| * 1つの namespacePrefix - baseDir の対応とその登録/解除を行う | |
| */ | |
| class Psr4 | |
| { | |
| const NS_SEPARATOR = '\\'; | |
| private $nsPrefix; | |
| private $baseDir; | |
| private $fileExt = '.php'; | |
| /** | |
| * constructor | |
| * | |
| * @param string $nsPrefix (optional) | |
| * @param string $baseDir (optional) | |
| * @param string $fileExt (optional) default='.php' | |
| */ | |
| public function __construct($nsPrefix=null, $baseDir=null, $fileExt='.php') { | |
| $this->nsPrefix = $nsPrefix; | |
| $this->setBaseDir($baseDir); | |
| $this->setFileExtension($fileExt); | |
| } | |
| /** | |
| * Sets the base include path for all class files in the namespace of this class loader. | |
| * | |
| * @param string $baseDir | |
| */ | |
| public function setBaseDir($baseDir) { | |
| $this->baseDir = $baseDir; | |
| } | |
| /** | |
| * Gets the base include path for all class files in the namespace of this class loader. | |
| * | |
| * @return string $baseDir | |
| */ | |
| public function getBaseDir() { | |
| return $this->baseDir; | |
| } | |
| /** | |
| * Sets the file extension of class files in the namespace of this class loader. | |
| * | |
| * @param string $fileExt | |
| */ | |
| public function setFileExtension($fileExt) { | |
| $this->fileExt = $fileExt; | |
| } | |
| /** | |
| * Gets the file extension of class files in the namespace of this class loader. | |
| * | |
| * @return string $fileExtension | |
| */ | |
| public function getFileExtension() { | |
| return $this->fileExt; | |
| } | |
| /** | |
| * Installs this class loader on the SPL autoload stack. | |
| */ | |
| public function register() { | |
| spl_autoload_register(array( | |
| $this,'loadClass' | |
| )); | |
| } | |
| /** | |
| * Uninstalls this class loader from the SPL autoloader stack. | |
| */ | |
| public function unregister() { | |
| spl_autoload_unregister(array( | |
| $this,'loadClass' | |
| )); | |
| } | |
| /** | |
| * Loads the given class or interface. | |
| * | |
| * @param string $className The name of the class to load. | |
| * @return void | |
| */ | |
| public function loadClass($className) { | |
| $relativePath = substr($className, strlen($this->nsPrefix.self::NS_SEPARATOR)); | |
| $relativePath = str_replace(self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $relativePath); | |
| $relativePath .= $this->fileExt; | |
| $fullpath = $this->baseDir . DIRECTORY_SEPARATOR . $relativePath; | |
| if(is_file($fullpath)) { | |
| require $fullpath; | |
| } | |
| } | |
| } |