2013-08-28 11:04:02 +01: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\Debug ;
/**
* Autoloader checking if the class is really defined in the file found .
*
2014-02-04 10:33:08 +00:00
* The ClassLoader will wrap all registered autoloaders
* and will throw an exception if a file is found but does
2013-08-28 11:04:02 +01:00
* not declare the class .
*
* @ author Fabien Potencier < fabien @ symfony . com >
* @ author Christophe Coevoet < stof @ notk . org >
2014-02-04 10:33:08 +00:00
* @ author Nicolas Grekas < p @ tchwork . com >
2013-08-28 11:04:02 +01:00
*/
class DebugClassLoader
{
2014-02-04 10:33:08 +00:00
private $classLoader ;
private $isFinder ;
2017-08-25 18:21:35 +01:00
private $loaded = array ();
2014-05-06 14:29:22 +01:00
private static $caseCheck ;
2017-12-20 19:11:52 +00:00
private static $checkedClasses = array ();
2017-01-15 16:05:29 +00:00
private static $final = array ();
2017-01-30 18:27:23 +00:00
private static $finalMethods = array ();
2014-12-29 17:11:47 +00:00
private static $deprecated = array ();
2017-08-07 13:16:05 +01:00
private static $internal = array ();
private static $internalMethods = array ();
2017-12-20 19:11:52 +00:00
private static $php7Reserved = array ( 'int' => 1 , 'float' => 1 , 'bool' => 1 , 'string' => 1 , 'true' => 1 , 'false' => 1 , 'null' => 1 );
2015-08-03 20:57:20 +01:00
private static $darwinCache = array ( '/' => array ( '/' , array ()));
2013-08-28 11:04:02 +01:00
2015-04-02 08:55:37 +01:00
public function __construct ( callable $classLoader )
2013-08-28 11:04:02 +01:00
{
2015-04-02 08:55:37 +01:00
$this -> classLoader = $classLoader ;
2018-07-26 09:45:46 +01:00
$this -> isFinder = \is_array ( $classLoader ) && method_exists ( $classLoader [ 0 ], 'findFile' );
2014-05-06 14:29:22 +01:00
if ( ! isset ( self :: $caseCheck )) {
2018-07-26 12:13:39 +01:00
$file = file_exists ( __FILE__ ) ? __FILE__ : rtrim ( realpath ( '.' ), \DIRECTORY_SEPARATOR );
$i = strrpos ( $file , \DIRECTORY_SEPARATOR );
2016-03-29 18:31:32 +01:00
$dir = substr ( $file , 0 , 1 + $i );
$file = substr ( $file , 1 + $i );
$test = strtoupper ( $file ) === $file ? strtolower ( $file ) : strtoupper ( $file );
$test = realpath ( $dir . $test );
if ( false === $test || false === $i ) {
2016-03-12 15:40:45 +00:00
// filesystem is case sensitive
self :: $caseCheck = 0 ;
2018-07-05 12:24:53 +01:00
} elseif ( substr ( $test , - \strlen ( $file )) === $file ) {
2016-03-29 18:31:32 +01:00
// filesystem is case insensitive and realpath() normalizes the case of characters
2016-03-12 15:40:45 +00:00
self :: $caseCheck = 1 ;
2016-03-29 18:31:32 +01:00
} elseif ( false !== stripos ( PHP_OS , 'darwin' )) {
// on MacOSX, HFS+ is case insensitive but realpath() doesn't normalize the case of characters
2016-03-12 15:40:45 +00:00
self :: $caseCheck = 2 ;
2016-03-29 18:31:32 +01:00
} else {
// filesystem case checks failed, fallback to disabling them
self :: $caseCheck = 0 ;
2016-03-12 15:40:45 +00:00
}
2014-05-06 14:29:22 +01:00
}
2013-08-28 11:04:02 +01:00
}
/**
* Gets the wrapped class loader .
*
2015-04-02 08:55:37 +01:00
* @ return callable The wrapped class loader
2013-08-28 11:04:02 +01:00
*/
public function getClassLoader ()
{
2015-04-02 08:55:37 +01:00
return $this -> classLoader ;
2013-08-28 11:04:02 +01:00
}
/**
2015-04-26 09:04:53 +01:00
* Wraps all autoloaders .
2013-08-28 11:04:02 +01:00
*/
public static function enable ()
{
2014-02-04 10:33:08 +00:00
// Ensures we don't hit https://bugs.php.net/42098
2014-05-26 11:29:25 +01:00
class_exists ( 'Symfony\Component\Debug\ErrorHandler' );
2014-07-06 12:06:03 +01:00
class_exists ( 'Psr\Log\LogLevel' );
2014-02-04 10:33:08 +00:00
2018-07-05 12:24:53 +01:00
if ( ! \is_array ( $functions = spl_autoload_functions ())) {
2013-08-28 11:04:02 +01:00
return ;
}
foreach ( $functions as $function ) {
spl_autoload_unregister ( $function );
}
foreach ( $functions as $function ) {
2018-07-05 12:24:53 +01:00
if ( ! \is_array ( $function ) || ! $function [ 0 ] instanceof self ) {
2014-02-04 10:33:08 +00:00
$function = array ( new static ( $function ), 'loadClass' );
2013-08-28 11:04:02 +01:00
}
spl_autoload_register ( $function );
}
}
/**
* Disables the wrapping .
*/
public static function disable ()
{
2018-07-05 12:24:53 +01:00
if ( ! \is_array ( $functions = spl_autoload_functions ())) {
2013-08-28 11:04:02 +01:00
return ;
}
foreach ( $functions as $function ) {
spl_autoload_unregister ( $function );
}
foreach ( $functions as $function ) {
2018-07-05 12:24:53 +01:00
if ( \is_array ( $function ) && $function [ 0 ] instanceof self ) {
2014-02-04 10:33:08 +00:00
$function = $function [ 0 ] -> getClassLoader ();
2013-08-28 11:04:02 +01:00
}
spl_autoload_register ( $function );
}
}
/**
* Loads the given class or interface .
*
* @ param string $class The name of the class
*
2014-12-04 20:26:11 +00:00
* @ return bool | null True , if loaded
2013-08-28 11:04:02 +01:00
*
* @ throws \RuntimeException
*/
public function loadClass ( $class )
{
2017-05-21 16:02:52 +01:00
$e = error_reporting ( error_reporting () | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR );
2014-02-04 10:33:08 +00:00
try {
2017-08-25 18:21:35 +01:00
if ( $this -> isFinder && ! isset ( $this -> loaded [ $class ])) {
$this -> loaded [ $class ] = true ;
2017-12-20 19:11:52 +00:00
if ( $file = $this -> classLoader [ 0 ] -> findFile ( $class ) ? : false ) {
2018-01-31 22:22:47 +00:00
$wasCached = \function_exists ( 'opcache_is_script_cached' ) && @ opcache_is_script_cached ( $file );
2017-12-20 19:11:52 +00:00
2017-08-25 18:21:35 +01:00
require $file ;
2017-12-20 19:11:52 +00:00
if ( $wasCached ) {
return ;
}
2014-02-04 10:33:08 +00:00
}
} else {
2018-07-05 12:24:53 +01:00
\call_user_func ( $this -> classLoader , $class );
2014-02-04 10:33:08 +00:00
$file = false ;
}
2015-09-28 01:26:11 +01:00
} finally {
2017-05-21 16:02:52 +01:00
error_reporting ( $e );
2014-02-04 10:33:08 +00:00
}
2017-12-20 19:11:52 +00:00
$this -> checkClass ( $class , $file );
}
2014-02-04 10:33:08 +00:00
2017-12-20 19:11:52 +00:00
private function checkClass ( $class , $file = null )
{
$exists = null === $file || \class_exists ( $class , false ) || \interface_exists ( $class , false ) || \trait_exists ( $class , false );
if ( null !== $file && $class && '\\' === $class [ 0 ]) {
2014-05-06 14:29:22 +01:00
$class = substr ( $class , 1 );
}
2014-02-04 10:33:08 +00:00
if ( $exists ) {
2017-12-20 19:11:52 +00:00
if ( isset ( self :: $checkedClasses [ $class ])) {
return ;
}
self :: $checkedClasses [ $class ] = true ;
2014-05-06 14:29:22 +01:00
$refl = new \ReflectionClass ( $class );
2017-12-20 19:11:52 +00:00
if ( null === $file && $refl -> isInternal ()) {
return ;
}
2014-05-06 14:29:22 +01:00
$name = $refl -> getName ();
2014-02-04 10:33:08 +00:00
2017-12-20 19:11:52 +00:00
if ( $name !== $class && 0 === \strcasecmp ( $name , $class )) {
2017-04-17 14:29:15 +01:00
throw new \RuntimeException ( sprintf ( 'Case mismatch between loaded and declared class names: "%s" vs "%s".' , $class , $name ));
2014-02-04 10:33:08 +00:00
}
2014-12-29 17:11:47 +00:00
2017-08-06 14:38:20 +01:00
// Don't trigger deprecations for classes in the same vendor
2017-12-20 19:11:52 +00:00
if ( 2 > $len = 1 + ( \strpos ( $name , '\\' ) ? : \strpos ( $name , '_' ))) {
2017-08-06 14:38:20 +01:00
$len = 0 ;
$ns = '' ;
2015-02-12 08:13:06 +00:00
} else {
2017-12-20 19:11:52 +00:00
$ns = \substr ( $name , 0 , $len );
2017-08-06 14:38:20 +01:00
}
2014-12-29 17:11:47 +00:00
2017-08-07 13:16:05 +01:00
// Detect annotations on the class
if ( false !== $doc = $refl -> getDocComment ()) {
foreach ( array ( 'final' , 'deprecated' , 'internal' ) as $annotation ) {
2017-12-20 19:11:52 +00:00
if ( false !== \strpos ( $doc , $annotation ) && preg_match ( '#\n \* @' . $annotation . '(?:( .+?)\.?)?\r?\n \*(?: @|/$)#s' , $doc , $notice )) {
2017-08-07 13:16:05 +01:00
self :: ${$annotation} [ $name ] = isset ( $notice [ 1 ]) ? preg_replace ( '#\s*\r?\n \* +#' , ' ' , $notice [ 1 ]) : '' ;
}
}
}
2017-12-20 19:11:52 +00:00
$parentAndTraits = \class_uses ( $name , false );
if ( $parent = \get_parent_class ( $class )) {
2017-08-07 13:16:05 +01:00
$parentAndTraits [] = $parent ;
2017-12-20 19:11:52 +00:00
if ( ! isset ( self :: $checkedClasses [ $parent ])) {
$this -> checkClass ( $parent );
}
2017-08-07 13:16:05 +01:00
if ( isset ( self :: $final [ $parent ])) {
@ trigger_error ( sprintf ( 'The "%s" class is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".' , $parent , self :: $final [ $parent ], $name ), E_USER_DEPRECATED );
}
}
// Detect if the parent is annotated
foreach ( $parentAndTraits + $this -> getOwnInterfaces ( $name , $parent ) as $use ) {
2017-12-20 19:11:52 +00:00
if ( ! isset ( self :: $checkedClasses [ $use ])) {
$this -> checkClass ( $use );
}
if ( isset ( self :: $deprecated [ $use ]) && \strncmp ( $ns , $use , $len )) {
2017-08-07 13:16:05 +01:00
$type = class_exists ( $name , false ) ? 'class' : ( interface_exists ( $name , false ) ? 'interface' : 'trait' );
$verb = class_exists ( $use , false ) || interface_exists ( $name , false ) ? 'extends' : ( interface_exists ( $use , false ) ? 'implements' : 'uses' );
@ trigger_error ( sprintf ( 'The "%s" %s %s "%s" that is deprecated%s.' , $name , $type , $verb , $use , self :: $deprecated [ $use ]), E_USER_DEPRECATED );
}
2017-12-20 19:11:52 +00:00
if ( isset ( self :: $internal [ $use ]) && \strncmp ( $ns , $use , $len )) {
2017-08-06 14:38:20 +01:00
@ trigger_error ( sprintf ( 'The "%s" %s is considered internal%s. It may change without further notice. You should not use it from "%s".' , $use , class_exists ( $use , false ) ? 'class' : ( interface_exists ( $use , false ) ? 'interface' : 'trait' ), self :: $internal [ $use ], $name ), E_USER_DEPRECATED );
}
}
2017-11-11 15:25:43 +00:00
// Inherit @final and @internal annotations for methods
2017-08-07 13:16:05 +01:00
self :: $finalMethods [ $name ] = array ();
self :: $internalMethods [ $name ] = array ();
foreach ( $parentAndTraits as $use ) {
2017-11-11 15:25:43 +00:00
foreach ( array ( 'finalMethods' , 'internalMethods' ) as $property ) {
2017-08-07 13:16:05 +01:00
if ( isset ( self :: ${$property} [ $use ])) {
2017-12-20 19:11:52 +00:00
self :: ${$property} [ $name ] = self :: ${$property} [ $name ] ? self :: ${$property} [ $use ] + self :: ${$property} [ $name ] : self :: ${$property} [ $use ];
2017-08-07 13:16:05 +01:00
}
2017-08-06 14:38:20 +01:00
}
2017-08-07 13:16:05 +01:00
}
2014-12-29 17:11:47 +00:00
2017-12-20 19:11:52 +00:00
$isClass = \class_exists ( $name , false );
2017-08-07 13:16:05 +01:00
foreach ( $refl -> getMethods ( \ReflectionMethod :: IS_PUBLIC | \ReflectionMethod :: IS_PROTECTED ) as $method ) {
if ( $method -> class !== $name ) {
continue ;
2017-08-06 14:38:20 +01:00
}
2016-01-09 16:31:38 +00:00
2017-08-07 13:16:05 +01:00
if ( $isClass && $parent && isset ( self :: $finalMethods [ $parent ][ $method -> name ])) {
2017-08-09 17:49:26 +01:00
list ( $declaringClass , $message ) = self :: $finalMethods [ $parent ][ $method -> name ];
@ trigger_error ( sprintf ( 'The "%s::%s()" method is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".' , $declaringClass , $method -> name , $message , $name ), E_USER_DEPRECATED );
2017-08-07 13:16:05 +01:00
}
2018-09-09 10:07:24 +01:00
if ( isset ( self :: $internalMethods [ $name ][ $method -> name ])) {
list ( $declaringClass , $message ) = self :: $internalMethods [ $name ][ $method -> name ];
if ( \strncmp ( $ns , $declaringClass , $len )) {
@ trigger_error ( sprintf ( 'The "%s::%s()" method is considered internal%s. It may change without further notice. You should not extend it from "%s".' , $declaringClass , $method -> name , $message , $name ), E_USER_DEPRECATED );
2016-01-09 16:31:38 +00:00
}
2017-08-06 14:38:20 +01:00
}
2016-01-09 16:31:38 +00:00
2018-09-09 10:07:24 +01:00
// Method from a trait
if ( $method -> getFilename () !== $refl -> getFileName ()) {
continue ;
}
2017-08-07 13:16:05 +01:00
// Detect method annotations
if ( false === $doc = $method -> getDocComment ()) {
continue ;
}
2017-11-11 15:25:43 +00:00
foreach ( array ( 'final' , 'internal' ) as $annotation ) {
2017-12-20 19:11:52 +00:00
if ( false !== \strpos ( $doc , $annotation ) && preg_match ( '#\n\s+\* @' . $annotation . '(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$)#s' , $doc , $notice )) {
2017-08-07 13:16:05 +01:00
$message = isset ( $notice [ 1 ]) ? preg_replace ( '#\s*\r?\n \* +#' , ' ' , $notice [ 1 ]) : '' ;
2017-08-09 17:49:26 +01:00
self :: $ { $annotation . 'Methods' }[ $name ][ $method -> name ] = array ( $name , $message );
2014-12-29 17:11:47 +00:00
}
}
}
2017-08-07 13:16:05 +01:00
2017-12-20 19:11:52 +00:00
if ( isset ( self :: $php7Reserved [ \strtolower ( $refl -> getShortName ())])) {
2017-08-07 13:16:05 +01:00
@ trigger_error ( sprintf ( 'The "%s" class uses the reserved name "%s", it will break on PHP 7 and higher' , $name , $refl -> getShortName ()), E_USER_DEPRECATED );
}
2014-02-04 10:33:08 +00:00
}
if ( $file ) {
2014-05-06 14:29:22 +01:00
if ( ! $exists ) {
if ( false !== strpos ( $class , '/' )) {
throw new \RuntimeException ( sprintf ( 'Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".' , $class ));
}
2014-02-04 10:33:08 +00:00
2014-05-06 14:29:22 +01:00
throw new \RuntimeException ( sprintf ( 'The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.' , $class , $file ));
}
2015-09-14 08:37:59 +01:00
if ( self :: $caseCheck ) {
$real = explode ( '\\' , $class . strrchr ( $file , '.' ));
2018-07-26 12:13:39 +01:00
$tail = explode ( \DIRECTORY_SEPARATOR , str_replace ( '/' , \DIRECTORY_SEPARATOR , $file ));
2015-09-14 08:37:59 +01:00
2018-07-05 12:24:53 +01:00
$i = \count ( $tail ) - 1 ;
$j = \count ( $real ) - 1 ;
2015-09-14 08:37:59 +01:00
2016-01-21 08:36:03 +00:00
while ( isset ( $tail [ $i ], $real [ $j ]) && $tail [ $i ] === $real [ $j ]) {
2015-09-14 08:37:59 +01:00
-- $i ;
-- $j ;
}
array_splice ( $tail , 0 , $i + 1 );
}
if ( self :: $caseCheck && $tail ) {
2018-07-26 12:13:39 +01:00
$tail = \DIRECTORY_SEPARATOR . implode ( \DIRECTORY_SEPARATOR , $tail );
2018-07-05 12:24:53 +01:00
$tailLen = \strlen ( $tail );
2015-07-07 20:01:23 +01:00
$real = $refl -> getFileName ();
2014-04-24 13:55:37 +01:00
2014-05-06 14:29:22 +01:00
if ( 2 === self :: $caseCheck ) {
// realpath() on MacOSX doesn't normalize the case of characters
2015-08-03 20:57:20 +01:00
$i = 1 + strrpos ( $real , '/' );
$file = substr ( $real , $i );
$real = substr ( $real , 0 , $i );
if ( isset ( self :: $darwinCache [ $real ])) {
$kDir = $real ;
} else {
$kDir = strtolower ( $real );
if ( isset ( self :: $darwinCache [ $kDir ])) {
$real = self :: $darwinCache [ $kDir ][ 0 ];
} else {
$dir = getcwd ();
chdir ( $real );
$real = getcwd () . '/' ;
chdir ( $dir );
$dir = $real ;
$k = $kDir ;
2018-07-05 12:24:53 +01:00
$i = \strlen ( $dir ) - 1 ;
2015-08-03 20:57:20 +01:00
while ( ! isset ( self :: $darwinCache [ $k ])) {
self :: $darwinCache [ $k ] = array ( $dir , array ());
self :: $darwinCache [ $dir ] = & self :: $darwinCache [ $k ];
while ( '/' !== $dir [ -- $i ]) {
}
$k = substr ( $k , 0 , ++ $i );
$dir = substr ( $dir , 0 , $i -- );
}
}
}
$dirFiles = self :: $darwinCache [ $kDir ][ 1 ];
if ( isset ( $dirFiles [ $file ])) {
$kFile = $file ;
} else {
$kFile = strtolower ( $file );
if ( ! isset ( $dirFiles [ $kFile ])) {
foreach ( scandir ( $real , 2 ) as $f ) {
if ( '.' !== $f [ 0 ]) {
$dirFiles [ $f ] = $f ;
if ( $f === $file ) {
$kFile = $k = $file ;
} elseif ( $f !== $k = strtolower ( $f )) {
$dirFiles [ $k ] = $f ;
}
}
2014-05-06 14:29:22 +01:00
}
2015-08-03 20:57:20 +01:00
self :: $darwinCache [ $kDir ][ 1 ] = $dirFiles ;
2014-02-21 16:50:25 +00:00
}
}
2015-08-03 20:57:20 +01:00
$real .= $dirFiles [ $kFile ];
2014-04-24 13:55:37 +01:00
}
2014-02-21 16:50:25 +00:00
2015-08-03 20:57:20 +01:00
if ( 0 === substr_compare ( $real , $tail , - $tailLen , $tailLen , true )
&& 0 !== substr_compare ( $real , $tail , - $tailLen , $tailLen , false )
2014-04-24 13:55:37 +01:00
) {
2017-04-17 14:29:15 +01:00
throw new \RuntimeException ( sprintf ( 'Case mismatch between class and real file names: "%s" vs "%s" in "%s".' , substr ( $tail , - $tailLen + 1 ), substr ( $real , - $tailLen + 1 ), substr ( $real , 0 , - $tailLen + 1 )));
2014-02-04 10:33:08 +00:00
}
2014-04-24 13:55:37 +01:00
}
2013-08-28 11:04:02 +01:00
}
}
2017-08-07 13:16:05 +01:00
/**
* `class_implements` includes interfaces from the parents so we have to manually exclude them .
*
* @ param string $class
* @ param string | false $parent
*
* @ return string []
*/
private function getOwnInterfaces ( $class , $parent )
{
$ownInterfaces = class_implements ( $class , false );
if ( $parent ) {
foreach ( class_implements ( $parent , false ) as $interface ) {
unset ( $ownInterfaces [ $interface ]);
}
}
foreach ( $ownInterfaces as $interface ) {
foreach ( class_implements ( $interface ) as $interface ) {
unset ( $ownInterfaces [ $interface ]);
}
}
return $ownInterfaces ;
}
2013-08-28 11:04:02 +01:00
}