Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
27 / 27
Psr0
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
8 / 8
12
100.00% covered (success)
100.00%
27 / 27
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 setIncludePath
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getIncludePath
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setFileExtension
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getFileExtension
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 unregister
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 loadClass
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
11 / 11
<?php
namespace Puyo\Loader;
/**
 * PSR-0準拠のautoloader
 *
 * 1つのnamespace-pathの対応とその登録/解除を行う
 */
class Psr0
{
    const NS_SEPARATOR = '\\';
    private $namespace;
    private $includePath;
    private $fileExt = '.php';
    /**
     * constructor
     *
     * PEAR形式をマッピングする場合、$ns には null を指定する。
     * @param string $nsRoot (optional)
     * @param string $includePath (optional)
     * @param string $fileExt (optional) default='.php'
     */
    public function __construct($nsRoot=null, $includePath=null, $fileExt='.php') {
        $this->namespace = $nsRoot;
        $this->setIncludePath($includePath);
        $this->setFileExtension($fileExt);
    }
    /**
     * Sets the base include path for all class files in the namespace of this class loader.
     *
     * @param string $includePath
     */
    public function setIncludePath($includePath) {
        $this->includePath = $includePath;
    }
    /**
     * Gets the base include path for all class files in the namespace of this class loader.
     *
     * @return string $includePath
     */
    public function getIncludePath() {
        return $this->includePath;
    }
    /**
     * 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) {
        $nsAndSeparator = $this->namespace.self::NS_SEPARATOR;
        if (null === $this->namespace || $nsAndSeparator === substr($className, 0, strlen($nsAndSeparator))) {
            $fileName = '';
            if (false !== ($lastNsPos = strripos($className, self::NS_SEPARATOR))) {
                $namespace = substr($className, 0, $lastNsPos);
                $className = substr($className, $lastNsPos + 1);
                $fileName = str_replace(self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
            }
            $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExt;
            $prefix = $this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '';
            require $prefix . $fileName;
        }
    }
}