From 1d7d5cae8ef7c27d09b906d569ef2d8710496155 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 18 Jan 2014 22:42:22 +0100 Subject: [PATCH 1/5] [ClassLoader] A PSR-4 compatible class loader. --- .../Component/ClassLoader/Psr4ClassLoader.php | 94 +++++++++++++++++++ .../Fixtures/psr-4/Class_With_Underscores.php | 7 ++ .../ClassLoader/Tests/Fixtures/psr-4/Foo.php | 7 ++ .../Lets/Go/Deeper/Class_With_Underscores.php | 7 ++ .../Fixtures/psr-4/Lets/Go/Deeper/Foo.php | 7 ++ .../ClassLoader/Tests/Psr4ClassLoaderTest.php | 71 ++++++++++++++ 6 files changed, 193 insertions(+) create mode 100644 src/Symfony/Component/ClassLoader/Psr4ClassLoader.php create mode 100644 src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Class_With_Underscores.php create mode 100644 src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Foo.php create mode 100644 src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Class_With_Underscores.php create mode 100644 src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Foo.php create mode 100644 src/Symfony/Component/ClassLoader/Tests/Psr4ClassLoaderTest.php diff --git a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php new file mode 100644 index 0000000000000..057a2c042367a --- /dev/null +++ b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php @@ -0,0 +1,94 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\ClassLoader; + +/** + * ClassLoader implements an PSR-4 class loader + * + * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md + * + * @author Alexander M. Turek + */ +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 (strpos($class, $currentPrefix) === 0) { + $classWithoutPrefix = substr($class, strlen($currentPrefix)); + $file = $currentBaseDir . str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutPrefix) . '.php'; + if (file_exists($file)) { + return $file; + } + } + } + + return null; + } + + /** + * @param string $class + * @return bool + */ + public function loadClass($class) + { + $file = $this->findFile($class); + if ($file !== null) { + require $file; + + return true; + } + + return false; + } + + /** + * Registers this instance as an autoloader. + * + * @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')); + } +} diff --git a/src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Class_With_Underscores.php b/src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Class_With_Underscores.php new file mode 100644 index 0000000000000..ce9b8ea323fd7 --- /dev/null +++ b/src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Class_With_Underscores.php @@ -0,0 +1,7 @@ + + * + * 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') + ); + } +} From 0322b67ffcbf9a8e2d7e186f38e656798e8b6fda Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 21 Jan 2014 16:04:05 +0100 Subject: [PATCH 2/5] CS fixes --- src/Symfony/Component/ClassLoader/Psr4ClassLoader.php | 2 +- .../ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Foo.php | 2 +- src/Symfony/Component/ClassLoader/Tests/Psr4ClassLoaderTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php index 057a2c042367a..30376604d08f0 100644 --- a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php +++ b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php @@ -91,4 +91,4 @@ public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } -} +} diff --git a/src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Foo.php b/src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Foo.php index dd6f9016a9680..53ead9f427228 100644 --- a/src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Foo.php +++ b/src/Symfony/Component/ClassLoader/Tests/Fixtures/psr-4/Lets/Go/Deeper/Foo.php @@ -4,4 +4,4 @@ class Foo { -} +} diff --git a/src/Symfony/Component/ClassLoader/Tests/Psr4ClassLoaderTest.php b/src/Symfony/Component/ClassLoader/Tests/Psr4ClassLoaderTest.php index db8bebd3b5f5b..65cae485ad2a1 100644 --- a/src/Symfony/Component/ClassLoader/Tests/Psr4ClassLoaderTest.php +++ b/src/Symfony/Component/ClassLoader/Tests/Psr4ClassLoaderTest.php @@ -68,4 +68,4 @@ public function getLoadNonexistentClassTests() array('UnknownVendor\\SomeLib\\I_Do_Not_Exist') ); } -} +} From 9549cf150465ab38c14b89a79ec1e572798c645c Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 21 Jan 2014 16:59:25 +0100 Subject: [PATCH 3/5] CS fixes --- .../Component/ClassLoader/Psr4ClassLoader.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php index 30376604d08f0..2da45a4eece94 100644 --- a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php +++ b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php @@ -12,9 +12,9 @@ namespace Symfony\Component\ClassLoader; /** - * ClassLoader implements an PSR-4 class loader + * A PSR-4 compatible class loader. * - * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md + * See http://www.php-fig.org/psr/psr-4/ * * @author Alexander M. Turek */ @@ -31,13 +31,14 @@ class Psr4ClassLoader */ public function addPrefix($prefix, $baseDir) { - $prefix = trim($prefix, '\\') . '\\'; + $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) @@ -46,7 +47,7 @@ public function findFile($class) foreach ($this->prefixes as $current) { list($currentPrefix, $currentBaseDir) = $current; - if (strpos($class, $currentPrefix) === 0) { + if (0 === strpos($class, $currentPrefix)) { $classWithoutPrefix = substr($class, strlen($currentPrefix)); $file = $currentBaseDir . str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutPrefix) . '.php'; if (file_exists($file)) { @@ -60,12 +61,13 @@ public function findFile($class) /** * @param string $class + * * @return bool */ public function loadClass($class) { $file = $this->findFile($class); - if ($file !== null) { + if (null !== $file) { require $file; return true; From 2bc2d25a65a680629abad64a039e91bce236e69c Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 21 Jan 2014 17:17:01 +0100 Subject: [PATCH 4/5] CS fix --- src/Symfony/Component/ClassLoader/Psr4ClassLoader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php index 2da45a4eece94..e9438ee1c2f67 100644 --- a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php +++ b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php @@ -56,7 +56,7 @@ public function findFile($class) } } - return null; + return; } /** From cc2ef32ae40e846cb779d39b3cce0837375aee08 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 22 Jan 2014 09:55:59 +0100 Subject: [PATCH 5/5] CS fixes --- src/Symfony/Component/ClassLoader/Psr4ClassLoader.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php index e9438ee1c2f67..a705a7f7bf391 100644 --- a/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php +++ b/src/Symfony/Component/ClassLoader/Psr4ClassLoader.php @@ -55,14 +55,12 @@ public function findFile($class) } } } - - return; } /** * @param string $class * - * @return bool + * @return Boolean */ public function loadClass($class) { @@ -79,7 +77,7 @@ public function loadClass($class) /** * Registers this instance as an autoloader. * - * @param bool $prepend + * @param Boolean $prepend */ public function register($prepend = false) { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy