PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file autoload.php

Documentation is available at autoload.php

  1. <?php
  2. /**
  3.  * Autoloads files for PHP_CodeSniffer and tracks what has been loaded.
  4.  *
  5.  * Due to different namespaces being used for custom coding standards,
  6.  * the autoloader keeps track of what class is loaded after a file is included,
  7.  * even if the file is ultimately included by another autoloader (such as composer).
  8.  *
  9.  * This allows PHP_CodeSniffer to request the class name after loading a class
  10.  * when it only knows the filename, without having to parse the file to find it.
  11.  *
  12.  * @author    Greg Sherwood <gsherwood@squiz.net>
  13.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  14.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  15.  */
  16.  
  17. namespace PHP_CodeSniffer;
  18.  
  19. class Autoload
  20. {
  21.  
  22.     /**
  23.      * A mapping of file names to class names.
  24.      *
  25.      * @var array<string, string>
  26.      */
  27.     private static $loadedClasses = array();
  28.  
  29.     /**
  30.      * A mapping of class names to file names.
  31.      *
  32.      * @var array<string, string>
  33.      */
  34.     private static $loadedFiles = array();
  35.  
  36.  
  37.     /**
  38.      * Loads a class.
  39.      *
  40.      * This method only loads classes that exist in the PHP_CodeSniffer namespace.
  41.      * All other classes are ignored and loaded by subsequent autoloaders.
  42.      *
  43.      * @param string $class The name of the class to load.
  44.      *
  45.      * @return bool 
  46.      */
  47.     public static function load($class)
  48.     {
  49.         $ds   = DIRECTORY_SEPARATOR;
  50.         $path = null;
  51.  
  52.         if (substr($class016=== 'PHP_CodeSniffer\\'{
  53.             if (substr($class022=== 'PHP_CodeSniffer\Tests\\'{
  54.                 $isInstalled !is_dir(__DIR__.$ds.'tests');
  55.                 if ($isInstalled === false{
  56.                     $path = __DIR__.$ds.'tests';
  57.                 else {
  58.                     $path '@test_dir@'.$ds.'PHP_CodeSniffer'.$ds.'CodeSniffer';
  59.                 }
  60.  
  61.                 $path .= $ds.substr(str_replace('\\'$ds$class)22).'.php';
  62.             else {
  63.                 $path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\'$ds$class)16).'.php';
  64.             }
  65.         }
  66.  
  67.         if ($path !== null && is_file($path=== true{
  68.             self::loadFile($path);
  69.             return true;
  70.         }
  71.  
  72.         return false;
  73.  
  74.     }//end load()
  75.  
  76.  
  77.     /**
  78.      * Includes a file and tracks what class or interface was loaded as a result.
  79.      *
  80.      * @param string $path The path of the file to load.
  81.      *
  82.      * @return string The fully qualified name of the class in the loaded file.
  83.      */
  84.     public static function loadFile($path)
  85.     {
  86.         if (isset(self::$loadedClasses[$path]=== true{
  87.             return self::$loadedClasses[$path];
  88.         }
  89.  
  90.         $classes    = get_declared_classes();
  91.         $interfaces get_declared_interfaces();
  92.  
  93.         include $path;
  94.  
  95.         $className  = null;
  96.         $newClasses array_diff(get_declared_classes()$classes);
  97.         foreach ($newClasses as $name{
  98.             if (isset(self::$loadedFiles[$name]=== false{
  99.                 $className $name;
  100.                 break;
  101.             }
  102.         }
  103.  
  104.         if ($className === null{
  105.             $newClasses array_diff(get_declared_interfaces()$classes);
  106.             foreach ($newClasses as $name{
  107.                 if (isset(self::$loadedFiles[$name]=== false{
  108.                     $className $name;
  109.                     break;
  110.                 }
  111.             }
  112.         }
  113.  
  114.         self::$loadedClasses[$path]    $className;
  115.         self::$loadedFiles[$className$path;
  116.         return self::$loadedClasses[$path];
  117.  
  118.     }//end loadFile()
  119.  
  120.  
  121.     /**
  122.      * Gets the class name for the given file path.
  123.      *
  124.      * @param string $path The name of the file.
  125.      *
  126.      * @throws \Exception If the file path has not been loaded.
  127.      * @return string 
  128.      */
  129.     public static function getLoadedClassName($path)
  130.     {
  131.         if (isset(self::$loadedClasses[$path]=== false{
  132.             throw new \Exception("Cannot get class name for $path; file has not been included");
  133.         }
  134.  
  135.         return self::$loadedClasses[$path];
  136.  
  137.     }//end getLoadedClassName()
  138.  
  139.  
  140.     /**
  141.      * Gets the file path for the given class name.
  142.      *
  143.      * @param string $class The name of the class.
  144.      *
  145.      * @throws \Exception If the class name has not been loaded
  146.      * @return string 
  147.      */
  148.     public static function getLoadedFileName($class)
  149.     {
  150.         if (isset(self::$loadedFiles[$class]=== false{
  151.             throw new \Exception("Cannot get file name for $class; class has not been included");
  152.         }
  153.  
  154.         return self::$loadedFiles[$class];
  155.  
  156.     }//end getLoadedFileName()
  157.  
  158.  
  159.     /**
  160.      * Gets the mapping of file names to class names.
  161.      *
  162.      * @return array<string, string>
  163.      */
  164.     public static function getLoadedClasses()
  165.     {
  166.         return self::$loadedClasses;
  167.  
  168.     }//end getLoadedClasses()
  169.  
  170.  
  171.     /**
  172.      * Gets the mapping of class names to file names.
  173.      *
  174.      * @return array<string, string>
  175.      */
  176.     public static function getLoadedFiles()
  177.     {
  178.         return self::$loadedFiles;
  179.  
  180.     }//end getLoadedFiles()
  181.  
  182.  
  183. }//end class
  184.  
  185.  
  186. // Register the autoloader before any existing autoloaders to ensure
  187. // it gets a chance to hear about every autoload request, and record
  188. // the file and class name for it.
  189. spl_autoload_register(__NAMESPACE__.'\Autoload::load'truetrue);

Documentation generated on Mon, 11 Mar 2019 14:49:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.