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

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