2015-08-18 00:00:26 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\ClassLoader ;
2018-11-23 12:29:20 +00:00
@ trigger_error ( 'The ' . __NAMESPACE__ . '\ClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use Composer instead.' , E_USER_DEPRECATED );
2015-08-18 00:00:26 +00:00
/**
* ClassLoader implements an PSR - 0 class loader .
*
* See https :// github . com / php - fig / fig - standards / blob / master / accepted / PSR - 0. md
*
* $loader = new ClassLoader ();
*
* // register classes with namespaces
* $loader -> addPrefix ( 'Symfony\Component' , __DIR__ . '/component' );
* $loader -> addPrefix ( 'Symfony' , __DIR__ . '/framework' );
*
* // activate the autoloader
* $loader -> register ();
*
* // to enable searching the include path (e.g. for PEAR packages)
* $loader -> setUseIncludePath ( true );
*
* In this example , if you try to use a class in the Symfony\Component
* namespace or one of its children ( Symfony\Component\Console for instance ),
* the autoloader will first look for the class under the component /
* directory , and it will then fallback to the framework / directory if not
* found before giving up .
*
* @ author Fabien Potencier < fabien @ symfony . com >
* @ author Jordi Boggiano < j . boggiano @ seld . be >
2018-11-23 12:29:20 +00:00
*
* @ deprecated since version 3.3 , to be removed in 4.0 .
2015-08-18 00:00:26 +00:00
*/
class ClassLoader
{
private $prefixes = array ();
private $fallbackDirs = array ();
private $useIncludePath = false ;
/**
* Returns prefixes .
*
* @ return array
*/
public function getPrefixes ()
{
return $this -> prefixes ;
}
/**
* Returns fallback directories .
*
* @ return array
*/
public function getFallbackDirs ()
{
return $this -> fallbackDirs ;
}
/**
* Adds prefixes .
*
* @ param array $prefixes Prefixes to add
*/
public function addPrefixes ( array $prefixes )
{
foreach ( $prefixes as $prefix => $path ) {
$this -> addPrefix ( $prefix , $path );
}
}
/**
* Registers a set of classes .
*
* @ param string $prefix The classes prefix
* @ param array | string $paths The location ( s ) of the classes
*/
public function addPrefix ( $prefix , $paths )
{
if ( ! $prefix ) {
foreach (( array ) $paths as $path ) {
$this -> fallbackDirs [] = $path ;
}
return ;
}
if ( isset ( $this -> prefixes [ $prefix ])) {
2018-11-23 12:29:20 +00:00
if ( \is_array ( $paths )) {
2015-11-17 21:42:33 +00:00
$this -> prefixes [ $prefix ] = array_unique ( array_merge (
$this -> prefixes [ $prefix ],
$paths
));
2018-11-23 12:29:20 +00:00
} elseif ( ! \in_array ( $paths , $this -> prefixes [ $prefix ])) {
2016-04-20 16:56:34 +00:00
$this -> prefixes [ $prefix ][] = $paths ;
2015-11-17 21:42:33 +00:00
}
2015-08-18 00:00:26 +00:00
} else {
2015-11-17 21:42:33 +00:00
$this -> prefixes [ $prefix ] = array_unique (( array ) $paths );
2015-08-18 00:00:26 +00:00
}
}
/**
* Turns on searching the include for class files .
*
* @ param bool $useIncludePath
*/
public function setUseIncludePath ( $useIncludePath )
{
$this -> useIncludePath = ( bool ) $useIncludePath ;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes .
*
* @ return bool
*/
public function getUseIncludePath ()
{
return $this -> useIncludePath ;
}
/**
* Registers this instance as an autoloader .
*
* @ param bool $prepend Whether to prepend the autoloader or not
*/
public function register ( $prepend = false )
{
spl_autoload_register ( array ( $this , 'loadClass' ), true , $prepend );
}
/**
* Unregisters this instance as an autoloader .
*/
public function unregister ()
{
spl_autoload_unregister ( array ( $this , 'loadClass' ));
}
/**
* Loads the given class or interface .
*
* @ param string $class The name of the class
*
* @ return bool | null True , if loaded
*/
public function loadClass ( $class )
{
if ( $file = $this -> findFile ( $class )) {
require $file ;
return true ;
}
}
/**
* Finds the path to the file where the class is defined .
*
* @ param string $class The name of the class
*
* @ return string | null The path , if found
*/
public function findFile ( $class )
{
if ( false !== $pos = strrpos ( $class , '\\' )) {
// namespaced class name
2018-11-23 12:29:20 +00:00
$classPath = str_replace ( '\\' , \DIRECTORY_SEPARATOR , substr ( $class , 0 , $pos )) . \DIRECTORY_SEPARATOR ;
2015-08-18 00:00:26 +00:00
$className = substr ( $class , $pos + 1 );
} else {
// PEAR-like class name
$classPath = null ;
$className = $class ;
}
2018-11-23 12:29:20 +00:00
$classPath .= str_replace ( '_' , \DIRECTORY_SEPARATOR , $className ) . '.php' ;
2015-08-18 00:00:26 +00:00
foreach ( $this -> prefixes as $prefix => $dirs ) {
if ( $class === strstr ( $class , $prefix )) {
foreach ( $dirs as $dir ) {
2018-11-23 12:29:20 +00:00
if ( file_exists ( $dir . \DIRECTORY_SEPARATOR . $classPath )) {
return $dir . \DIRECTORY_SEPARATOR . $classPath ;
2015-08-18 00:00:26 +00:00
}
}
}
}
foreach ( $this -> fallbackDirs as $dir ) {
2018-11-23 12:29:20 +00:00
if ( file_exists ( $dir . \DIRECTORY_SEPARATOR . $classPath )) {
return $dir . \DIRECTORY_SEPARATOR . $classPath ;
2015-08-18 00:00:26 +00:00
}
}
if ( $this -> useIncludePath && $file = stream_resolve_include_path ( $classPath )) {
return $file ;
}
}
}