This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php

95 lines
2.1 KiB
PHP
Raw Normal View History

<?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;
/**
* A PSR-4 compatible class loader.
*
* See http://www.php-fig.org/psr/psr-4/
*
* @author Alexander M. Turek <me@derrabus.de>
*/
class Psr4ClassLoader
{
/**
* @var array
*/
private $prefixes = array();
/**
* @param string $prefix
* @param string $baseDir
*/
public function addPrefix($prefix, $baseDir)
{
$prefix = trim($prefix, '\\').'\\';
Merge branch '2.4' into 2.5 * 2.4: fixed CS [Process] fixed some volatile tests [HttpKernel] fixed a volatile test [HttpFoundation] fixed some volatile tests [Tests] PHPUnit Optimizations Use getPathname() instead of string casting to get BinaryFileReponse file path Conflicts: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php src/Symfony/Component/ClassLoader/Tests/ApcUniversalClassLoaderTest.php src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/HttpKernel/Tests/DataCollector/LoggerDataCollectorTest.php src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php src/Symfony/Component/Process/Tests/AbstractProcessTest.php src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php src/Symfony/Component/Security/Core/Authentication/Provider/PreAuthenticatedAuthenticationProvider.php src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php src/Symfony/Component/Validator/Constraints/ChoiceValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php src/Symfony/Component/Validator/Tests/ValidationVisitorTest.php src/Symfony/Component/Yaml/Parser.php
2014-09-22 10:14:18 +01:00
$baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
$this->prefixes[] = array($prefix, $baseDir);
}
/**
* @param string $class
*
* @return string|null
*/
public function findFile($class)
{
$class = ltrim($class, '\\');
foreach ($this->prefixes as $current) {
list($currentPrefix, $currentBaseDir) = $current;
if (0 === strpos($class, $currentPrefix)) {
$classWithoutPrefix = substr($class, strlen($currentPrefix));
Merge branch '2.4' into 2.5 * 2.4: fixed CS [Process] fixed some volatile tests [HttpKernel] fixed a volatile test [HttpFoundation] fixed some volatile tests [Tests] PHPUnit Optimizations Use getPathname() instead of string casting to get BinaryFileReponse file path Conflicts: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php src/Symfony/Component/ClassLoader/Tests/ApcUniversalClassLoaderTest.php src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/HttpKernel/Tests/DataCollector/LoggerDataCollectorTest.php src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php src/Symfony/Component/Process/Tests/AbstractProcessTest.php src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php src/Symfony/Component/Security/Core/Authentication/Provider/PreAuthenticatedAuthenticationProvider.php src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php src/Symfony/Component/Validator/Constraints/ChoiceValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php src/Symfony/Component/Validator/Tests/ValidationVisitorTest.php src/Symfony/Component/Yaml/Parser.php
2014-09-22 10:14:18 +01:00
$file = $currentBaseDir.str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutPrefix).'.php';
if (file_exists($file)) {
return $file;
}
}
}
}
/**
* @param string $class
*
2014-04-16 11:36:34 +01:00
* @return bool
*/
public function loadClass($class)
{
$file = $this->findFile($class);
if (null !== $file) {
require $file;
return true;
}
return false;
}
/**
* Registers this instance as an autoloader.
*
2014-12-04 20:26:11 +00:00
* @param bool $prepend
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Removes this instance from the registered autoloaders.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
}