-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[ClassLoader] A PSR-4 compatible class loader #10100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,94 @@ | ||
<?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, '\\').'\\'; | ||
$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)); | ||
$file = $currentBaseDir . str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutPrefix) . '.php'; | ||
if (file_exists($file)) { | ||
return $file; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param string $class | ||
* | ||
* @return Boolean | ||
*/ | ||
public function loadClass($class) | ||
{ | ||
$file = $this->findFile($class); | ||
if (null !== $file) { | ||
require $file; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Registers this instance as an autoloader. | ||
* | ||
* @param Boolean $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')); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Class_With_Underscores.php
This file contains hidden or 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,7 @@ | ||
<?php | ||
|
||
namespace Acme\DemoLib; | ||
|
||
class Class_With_Underscores | ||
{ | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Foo.php
This file contains hidden or 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,7 @@ | ||
<?php | ||
|
||
namespace Acme\DemoLib; | ||
|
||
class Foo | ||
{ | ||
} |
7 changes: 7 additions & 0 deletions
7
...fony/Component/ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Class_With_Underscores.php
This file contains hidden or 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,7 @@ | ||
<?php | ||
|
||
namespace Acme\DemoLib\Lets\Go\Deeper; | ||
|
||
class Class_With_Underscores | ||
{ | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Foo.php
This file contains hidden or 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,7 @@ | ||
<?php | ||
|
||
namespace Acme\DemoLib\Lets\Go\Deeper; | ||
|
||
class Foo | ||
{ | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Symfony/Component/ClassLoader/Tests/Psr4ClassLoaderTest.php
This file contains hidden or 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,71 @@ | ||
<?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\Tests; | ||
|
||
use Symfony\Component\ClassLoader\Psr4ClassLoader; | ||
|
||
class Psr4ClassLoaderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @param string $className | ||
* @dataProvider getLoadClassTests | ||
*/ | ||
public function testLoadClass($className) | ||
{ | ||
$loader = new Psr4ClassLoader(); | ||
$loader->addPrefix( | ||
'Acme\\DemoLib', | ||
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'psr-4' | ||
); | ||
$loader->loadClass($className); | ||
$this->assertTrue(class_exists($className), sprintf('loadClass() should load %s', $className)); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getLoadClassTests() | ||
{ | ||
return array( | ||
array('Acme\\DemoLib\\Foo'), | ||
array('Acme\\DemoLib\\Class_With_Underscores'), | ||
array('Acme\\DemoLib\\Lets\\Go\\Deeper\\Foo'), | ||
array('Acme\\DemoLib\\Lets\\Go\\Deeper\\Class_With_Underscores') | ||
); | ||
} | ||
|
||
/** | ||
* @param string $className | ||
* @dataProvider getLoadNonexistentClassTests | ||
*/ | ||
public function testLoadNonexistentClass($className) | ||
{ | ||
$loader = new Psr4ClassLoader(); | ||
$loader->addPrefix( | ||
'Acme\\DemoLib', | ||
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'psr-4' | ||
); | ||
$loader->loadClass($className); | ||
$this->assertFalse(class_exists($className), sprintf('loadClass() should not load %s', $className)); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getLoadNonexistentClassTests() | ||
{ | ||
return array( | ||
array('Acme\\DemoLib\\I_Do_Not_Exist'), | ||
array('UnknownVendor\\SomeLib\\I_Do_Not_Exist') | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn’t this be using
@see
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, why not.