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. if (class_exists('PHP_CodeSniffer\Autoload'false=== false{
  20.     class Autoload
  21.     {
  22.  
  23.         /**
  24.          * The composer autoloader.
  25.          *
  26.          * @var Composer\Autoload\ClassLoader 
  27.          */
  28.         private static $composerAutoloader = null;
  29.  
  30.         /**
  31.          * A mapping of file names to class names.
  32.          *
  33.          * @var array<string, string>
  34.          */
  35.         private static $loadedClasses = array();
  36.  
  37.         /**
  38.          * A mapping of class names to file names.
  39.          *
  40.          * @var array<string, string>
  41.          */
  42.         private static $loadedFiles = array();
  43.  
  44.         /**
  45.          * A list of additional directories to search during autoloading.
  46.          *
  47.          * This is typically a list of coding standard directories.
  48.          *
  49.          * @var string[] 
  50.          */
  51.         private static $searchPaths = array();
  52.  
  53.  
  54.         /**
  55.          * Loads a class.
  56.          *
  57.          * This method only loads classes that exist in the PHP_CodeSniffer namespace.
  58.          * All other classes are ignored and loaded by subsequent autoloaders.
  59.          *
  60.          * @param string $class The name of the class to load.
  61.          *
  62.          * @return bool 
  63.          */
  64.         public static function load($class)
  65.         {
  66.             // Include the composer autoloader if there is one, but re-register it
  67.             // so this autoloader runs before the composer one as we need to include
  68.             // all files so we can figure out what the class/interface/trait name is.
  69.             if (self::$composerAutoloader === null{
  70.                 if (strpos(__DIR__'phar://'!== 0
  71.                     && file_exists(__DIR__.'/../../autoload.php'=== true
  72.                 {
  73.                     self::$composerAutoloader include __DIR__.'/../../autoload.php';
  74.                     if (self::$composerAutoloader instanceof \Composer\Autoload\ClassLoader{
  75.                         self::$composerAutoloader->unregister();
  76.                         self::$composerAutoloader->register();
  77.                     else {
  78.                         // Something went wrong, so keep going without the autoloader
  79.                         // although namespaced sniffs might error.
  80.                         self::$composerAutoloader = false;
  81.                     }
  82.                 else {
  83.                     self::$composerAutoloader = false;
  84.                 }
  85.             }
  86.  
  87.             $ds   = DIRECTORY_SEPARATOR;
  88.             $path = false;
  89.  
  90.             if (substr($class016=== 'PHP_CodeSniffer\\'{
  91.                 if (substr($class022=== 'PHP_CodeSniffer\Tests\\'{
  92.                     $isInstalled !is_dir(__DIR__.$ds.'tests');
  93.                     if ($isInstalled === false{
  94.                         $path = __DIR__.$ds.'tests';
  95.                     else {
  96.                         $path '@test_dir@'.$ds.'PHP_CodeSniffer'.$ds.'CodeSniffer';
  97.                     }
  98.  
  99.                     $path .= $ds.substr(str_replace('\\'$ds$class)22).'.php';
  100.                 else {
  101.                     $path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\'$ds$class)16).'.php';
  102.                 }
  103.             }
  104.  
  105.             // See if the composer autoloader knows where the class is.
  106.             if ($path === false && self::$composerAutoloader !== false{
  107.                 $path = self::$composerAutoloader->findFile($class);
  108.             }
  109.  
  110.             // See if the class is inside one of our alternate search paths.
  111.             if ($path === false{
  112.                 foreach (self::$searchPaths as $searchPath => $nsPrefix{
  113.                     $className $class;
  114.                     if ($nsPrefix !== '' && substr($class0strlen($nsPrefix)) === $nsPrefix{
  115.                         $className substr($class(strlen($nsPrefix+ 1));
  116.                     }
  117.  
  118.                     $path $searchPath.$ds.str_replace('\\'$ds$className).'.php';
  119.                     if (is_file($path=== true{
  120.                         break;
  121.                     }
  122.  
  123.                     $path = false;
  124.                 }
  125.             }
  126.  
  127.             if ($path !== false && is_file($path=== true{
  128.                 self::loadFile($path);
  129.                 return true;
  130.             }
  131.  
  132.             return false;
  133.  
  134.         }//end load()
  135.  
  136.  
  137.         /**
  138.          * Includes a file and tracks what class or interface was loaded as a result.
  139.          *
  140.          * @param string $path The path of the file to load.
  141.          *
  142.          * @return string The fully qualified name of the class in the loaded file.
  143.          */
  144.         public static function loadFile($path)
  145.         {
  146.             if (strpos(__DIR__'phar://'!== 0{
  147.                 $path realpath($path);
  148.                 if ($path === false{
  149.                     return false;
  150.                 }
  151.             }
  152.  
  153.             if (isset(self::$loadedClasses[$path]=== true{
  154.                 return self::$loadedClasses[$path];
  155.             }
  156.  
  157.             $classes    = get_declared_classes();
  158.             $interfaces get_declared_interfaces();
  159.             $traits     get_declared_traits();
  160.  
  161.             include $path;
  162.  
  163.             $className  = null;
  164.             $newClasses array_reverse(array_diff(get_declared_classes()$classes));
  165.             foreach ($newClasses as $name{
  166.                 if (isset(self::$loadedFiles[$name]=== false{
  167.                     $className $name;
  168.                     break;
  169.                 }
  170.             }
  171.  
  172.             if ($className === null{
  173.                 $newClasses array_reverse(array_diff(get_declared_interfaces()$interfaces));
  174.                 foreach ($newClasses as $name{
  175.                     if (isset(self::$loadedFiles[$name]=== false{
  176.                         $className $name;
  177.                         break;
  178.                     }
  179.                 }
  180.             }
  181.  
  182.             if ($className === null{
  183.                 $newClasses array_reverse(array_diff(get_declared_traits()$traits));
  184.                 foreach ($newClasses as $name{
  185.                     if (isset(self::$loadedFiles[$name]=== false{
  186.                         $className $name;
  187.                         break;
  188.                     }
  189.                 }
  190.             }
  191.  
  192.             self::$loadedClasses[$path]    $className;
  193.             self::$loadedFiles[$className$path;
  194.             return self::$loadedClasses[$path];
  195.  
  196.         }//end loadFile()
  197.  
  198.  
  199.         /**
  200.          * Adds a directory to search during autoloading.
  201.          *
  202.          * @param string $path     The path to the directory to search.
  203.          * @param string $nsPrefix The namespace prefix used by files under this path.
  204.          *
  205.          * @return void 
  206.          */
  207.         public static function addSearchPath($path$nsPrefix='')
  208.         {
  209.             self::$searchPaths[$path= rtrim(trim((string) $nsPrefix)'\\');
  210.  
  211.         }//end addSearchPath()
  212.  
  213.  
  214.         /**
  215.          * Gets the class name for the given file path.
  216.          *
  217.          * @param string $path The name of the file.
  218.          *
  219.          * @throws \Exception If the file path has not been loaded.
  220.          * @return string 
  221.          */
  222.         public static function getLoadedClassName($path)
  223.         {
  224.             if (isset(self::$loadedClasses[$path]=== false{
  225.                 throw new \Exception("Cannot get class name for $path; file has not been included");
  226.             }
  227.  
  228.             return self::$loadedClasses[$path];
  229.  
  230.         }//end getLoadedClassName()
  231.  
  232.  
  233.         /**
  234.          * Gets the file path for the given class name.
  235.          *
  236.          * @param string $class The name of the class.
  237.          *
  238.          * @throws \Exception If the class name has not been loaded
  239.          * @return string 
  240.          */
  241.         public static function getLoadedFileName($class)
  242.         {
  243.             if (isset(self::$loadedFiles[$class]=== false{
  244.                 throw new \Exception("Cannot get file name for $class; class has not been included");
  245.             }
  246.  
  247.             return self::$loadedFiles[$class];
  248.  
  249.         }//end getLoadedFileName()
  250.  
  251.  
  252.         /**
  253.          * Gets the mapping of file names to class names.
  254.          *
  255.          * @return array<string, string>
  256.          */
  257.         public static function getLoadedClasses()
  258.         {
  259.             return self::$loadedClasses;
  260.  
  261.         }//end getLoadedClasses()
  262.  
  263.  
  264.         /**
  265.          * Gets the mapping of class names to file names.
  266.          *
  267.          * @return array<string, string>
  268.          */
  269.         public static function getLoadedFiles()
  270.         {
  271.             return self::$loadedFiles;
  272.  
  273.         }//end getLoadedFiles()
  274.  
  275.  
  276.     }//end class
  277.  
  278.     // Register the autoloader before any existing autoloaders to ensure
  279.     // it gets a chance to hear about every autoload request, and record
  280.     // the file and class name for it.
  281.     spl_autoload_register(__NAMESPACE__.'\Autoload::load'truetrue);
  282. }//end if

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