Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.97 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('BASEPATH')) {
  4.     exit('No direct script access allowed');
  5. }
  6.  
  7. /**
  8.  * An example of a general-purpose implementation that includes the optional
  9.  * functionality of allowing multiple base directories for a single namespace
  10.  * prefix.
  11.  *
  12.  * Given a foo-bar package of classes in the file system at the following
  13.  * paths ...
  14.  *
  15.  *     /path/to/packages/foo-bar/
  16.  *         src/
  17.  *             Baz.php             # Foo\Bar\Baz
  18.  *             Qux/
  19.  *                 Quux.php        # Foo\Bar\Qux\Quux
  20.  *         tests/
  21.  *             BazTest.php         # Foo\Bar\BazTest
  22.  *             Qux/
  23.  *                 QuuxTest.php    # Foo\Bar\Qux\QuuxTest
  24.  *
  25.  * ... add the path to the class files for the \Foo\Bar\ namespace prefix
  26.  * as follows:
  27.  *
  28.  *      <?php
  29.  *      // instantiate the loader
  30.  *      $loader = new \Example\autoloader_psr4;
  31.  *
  32.  *      // register the autoloader
  33.  *      $loader->register();
  34.  *
  35.  *      // register the base directories for the namespace prefix
  36.  *      $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src');
  37.  *      $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests');
  38.  *
  39.  * The following line would cause the autoloader to attempt to load the
  40.  * \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php:
  41.  *
  42.  *      <?php
  43.  *      new \Foo\Bar\Qux\Quux;
  44.  *
  45.  * The following line would cause the autoloader to attempt to load the
  46.  * \Foo\Bar\Qux\QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php:
  47.  *
  48.  *      <?php
  49.  *      new \Foo\Bar\Qux\QuuxTest;
  50.  */
  51. class Autoloader_psr4 {
  52.  
  53.     /**
  54.      * An associative array where the key is a namespace prefix and the value
  55.      * is an array of base directories for classes in that namespace.
  56.      *
  57.      * @var array
  58.      */
  59.     protected $prefixes = array();
  60.  
  61.     /**
  62.      * Register loader with SPL autoloader stack.
  63.      *
  64.      * @return void
  65.      */
  66.     public function register() {
  67.         spl_autoload_register(array($this, 'loadClass'));
  68.     }
  69.  
  70.     /**
  71.      * Adds a base directory for a namespace prefix.
  72.      *
  73.      * @param string $prefix The namespace prefix.
  74.      * @param string $base_dir A base directory for class files in the
  75.      * namespace.
  76.      * @param boolean $prepend If true, prepend the base directory to the stack
  77.      * instead of appending it; this causes it to be searched first rather
  78.      * than last.
  79.      * @return void
  80.      */
  81.     public function addNamespace($prefix, $base_dir, $prepend = false) {
  82.         // normalize namespace prefix
  83.         $prefix = trim($prefix, '\\') . '\\';
  84.  
  85.         // normalize the base directory with a trailing separator
  86.         $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/';
  87.  
  88.         // initialize the namespace prefix array
  89.         if (isset($this->prefixes[$prefix]) === false) {
  90.             $this->prefixes[$prefix] = array();
  91.         }
  92.  
  93.         // retain the base directory for the namespace prefix
  94.         if ($prepend) {
  95.             array_unshift($this->prefixes[$prefix], $base_dir);
  96.         } else {
  97.             array_push($this->prefixes[$prefix], $base_dir);
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Loads the class file for a given class name.
  103.      *
  104.      * @param string $class The fully-qualified class name.
  105.      * @return mixed The mapped file name on success, or boolean false on
  106.      * failure.
  107.      */
  108.     public function loadClass($class) {
  109.         // the current namespace prefix
  110.         $prefix = $class;
  111.  
  112.         // work backwards through the namespace names of the fully-qualified
  113.         // class name to find a mapped file name
  114.         while (false !== $pos = strrpos($prefix, '\\')) {
  115.  
  116.             // retain the trailing namespace separator in the prefix
  117.             $prefix = substr($class, 0, $pos + 1);
  118.  
  119.             // the rest is the relative class name
  120.             $relative_class = substr($class, $pos + 1);
  121.  
  122.             // try to load a mapped file for the prefix and relative class
  123.             $mapped_file = $this->loadMappedFile($prefix, $relative_class);
  124.             if ($mapped_file) {
  125.                 return $mapped_file;
  126.             }
  127.  
  128.             // remove the trailing namespace separator for the next iteration
  129.             // of strrpos()
  130.             $prefix = rtrim($prefix, '\\');
  131.         }
  132.  
  133.         // never found a mapped file
  134.         return false;
  135.     }
  136.  
  137.     /**
  138.      * Load the mapped file for a namespace prefix and relative class.
  139.      *
  140.      * @param string $prefix The namespace prefix.
  141.      * @param string $relative_class The relative class name.
  142.      * @return mixed Boolean false if no mapped file can be loaded, or the
  143.      * name of the mapped file that was loaded.
  144.      */
  145.     protected function loadMappedFile($prefix, $relative_class) {
  146.         // are there any base directories for this namespace prefix?
  147.         if (isset($this->prefixes[$prefix]) === false) {
  148.             return false;
  149.         }
  150.  
  151.         // look through base directories for this namespace prefix
  152.         foreach ($this->prefixes[$prefix] as $base_dir) {
  153.  
  154.             // replace the namespace prefix with the base directory,
  155.             // replace namespace separators with directory separators
  156.             // in the relative class name, append with .php
  157.             $file = $base_dir
  158.                     . str_replace('\\', '/', $relative_class)
  159.                     . '.php';
  160.  
  161.             // if the mapped file exists, require it
  162.             if ($this->requireFile($file)) {
  163.                 // yes, we're done
  164.                 return $file;
  165.             }
  166.         }
  167.  
  168.         // never found it
  169.         return false;
  170.     }
  171.  
  172.     /**
  173.      * If a file exists, require it from the file system.
  174.      *
  175.      * @param string $file The file to require.
  176.      * @return bool True if the file exists, false if not.
  177.      */
  178.     protected function requireFile($file) {
  179.         if (file_exists($file)) {
  180.             require $file;
  181.             return true;
  182.         }
  183.         return false;
  184.     }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement