This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ClassLoader] added a debug class loader
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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; | ||
|
||
/** | ||
* Checks that the class is actually declared in the included file. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class DebugUniversalClassLoader extends UniversalClassLoader | ||
{ | ||
/** | ||
* Replaces all regular UniversalClassLoader instances by a DebugUniversalClassLoader ones. | ||
*/ | ||
static public function enable() | ||
{ | ||
if (!is_array($functions = spl_autoload_functions())) { | ||
return; | ||
} | ||
|
||
foreach ($functions as $function) { | ||
spl_autoload_unregister($function); | ||
} | ||
|
||
foreach ($functions as $function) { | ||
if (is_array($function) && $function[0] instanceof UniversalClassLoader) { | ||
$loader = new static(); | ||
$loader->registerNamespaceFallback($function[0]->getNamespaceFallback()); | ||
$loader->registerPrefixFallback($function[0]->getPrefixFallback()); | ||
$loader->registerNamespaces($function[0]->getNamespaces()); | ||
$loader->registerPrefixes($function[0]->getPrefixes()); | ||
|
||
$function[0] = $loader; | ||
} | ||
|
||
spl_autoload_register($function); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function loadClass($class) | ||
{ | ||
if ($file = $this->findFile($class)) { | ||
require $file; | ||
|
||
if (!class_exists($class, false) && !interface_exists($class, false)) { | ||
throw new \Exception(sprintf('The autoloader expected class "%s" to be defined in file "%s". You probably have a typo in the namespace or the class name.', $class, $file)); | ||
} | ||
} | ||
} | ||
} |