From 96ce18722e0c36c7184938b5165bef056be1425e Mon Sep 17 00:00:00 2001 From: Marc-Jan Barnhoorn Date: Fri, 4 Oct 2019 14:41:48 +0200 Subject: [PATCH 1/3] added support for loading from composer autoload --- src/ClassNameMapper.php | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/ClassNameMapper.php b/src/ClassNameMapper.php index 6932aaa..d18c3d8 100644 --- a/src/ClassNameMapper.php +++ b/src/ClassNameMapper.php @@ -90,6 +90,23 @@ public static function createFromComposerFile($composerJsonPath = null, $rootPat return $classNameMapper; } + /** + * @param string|null $composerAutoloadPath + * @return ClassNameMapper + */ + public static function createFromComposerAutoload($composerAutoloadPath = null) + { + $classNameMapper = new ClassNameMapper(); + + if ($composerAutoloadPath === null) { + $composerAutoloadPath = __DIR__ . '/../../../autoload.php'; + } + + $classNameMapper->loadComposerAutoload($composerAutoloadPath); + + return $classNameMapper; + } + /** * * @param string $composerJsonPath Path to the composer file @@ -121,6 +138,7 @@ public function loadComposerFile($composerJsonPath, $rootPath = null, $useAutolo if (isset($composer["autoload"]["psr-4"])) { $psr4 = $composer["autoload"]["psr-4"]; + foreach ($psr4 as $namespace => $paths) { if ($relativePath != null) { if (!is_array($paths)) { @@ -169,6 +187,27 @@ public function loadComposerFile($composerJsonPath, $rootPath = null, $useAutolo return $this; } + /** + * @param string $composerAutoloadPath + * @return self + */ + public function loadComposerAutoload($composerAutoloadPath) + { + if (file_exists($composerAutoloadPath)) { + $loader = require $composerAutoloadPath; + + foreach ($loader->getPrefixes() as $namespace => $paths) { + $this->registerPsr0Namespace($namespace, $paths); + } + + foreach ($loader->getPrefixesPsr4() as $namespace => $paths) { + $this->registerPsr4Namespace($namespace, $paths); + } + } + + return $this; + } + /** * Given an existing path, convert it to a path relative to a given starting path. * Shamelessly borrowed to Symfony :). Thanks guys. @@ -325,4 +364,4 @@ private static function unfactorizeAutoload(array $autoload) { private static function normalizeDirectory($dir) { return $dir === '' ? '' : rtrim($dir, '\\/').'/'; } -} \ No newline at end of file +} From 685d5f23a60e101d92459275332c97523d19e28d Mon Sep 17 00:00:00 2001 From: Marc-Jan Barnhoorn Date: Fri, 4 Oct 2019 14:58:49 +0200 Subject: [PATCH 2/3] updated test --- tests/ClassNameMapperTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/ClassNameMapperTest.php b/tests/ClassNameMapperTest.php index d4fd073..45791ac 100644 --- a/tests/ClassNameMapperTest.php +++ b/tests/ClassNameMapperTest.php @@ -14,6 +14,11 @@ public function testGetPossibleFiles() { $this->assertEquals([ 'src/Foo/Bar.php', 'src2/Bar.php' ], $possibleFiles); + //From autoloader instead of composer.json + $mapper = ClassNameMapper::createFromComposerAutoload(__DIR__ . '/../vendor/autoload.php'); + $possibleFiles = $mapper->getPossibleFileNames('Mouf\\Composer\\ClassNameMapper'); + + $this->assertEquals([ realpath(__DIR__ . '/../vendor/composer/') . '/../../src/ClassNameMapper.php', realpath(__DIR__ . '/../vendor/composer/') . '/../../tests/ClassNameMapper.php' ], $possibleFiles); } public function testUseAutoloadDev() { From ded017cafa64afa4c798968a95ac75dabd11903c Mon Sep 17 00:00:00 2001 From: Marc-Jan Barnhoorn Date: Fri, 4 Oct 2019 15:40:49 +0200 Subject: [PATCH 3/3] created test autoload --- tests/ClassNameMapperTest.php | 8 ++++++-- tests/Fixtures/test_autoload.php | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/Fixtures/test_autoload.php diff --git a/tests/ClassNameMapperTest.php b/tests/ClassNameMapperTest.php index 45791ac..aca5a74 100644 --- a/tests/ClassNameMapperTest.php +++ b/tests/ClassNameMapperTest.php @@ -15,10 +15,14 @@ public function testGetPossibleFiles() { $this->assertEquals([ 'src/Foo/Bar.php', 'src2/Bar.php' ], $possibleFiles); //From autoloader instead of composer.json - $mapper = ClassNameMapper::createFromComposerAutoload(__DIR__ . '/../vendor/autoload.php'); + $mapper = ClassNameMapper::createFromComposerAutoload(__DIR__ . '/Fixtures/test_autoload.php'); $possibleFiles = $mapper->getPossibleFileNames('Mouf\\Composer\\ClassNameMapper'); - $this->assertEquals([ realpath(__DIR__ . '/../vendor/composer/') . '/../../src/ClassNameMapper.php', realpath(__DIR__ . '/../vendor/composer/') . '/../../tests/ClassNameMapper.php' ], $possibleFiles); + $this->assertEquals([ + 'src/ClassNameMapper.php' + ], + $possibleFiles + ); } public function testUseAutoloadDev() { diff --git a/tests/Fixtures/test_autoload.php b/tests/Fixtures/test_autoload.php new file mode 100644 index 0000000..b66f2f0 --- /dev/null +++ b/tests/Fixtures/test_autoload.php @@ -0,0 +1,8 @@ +setPsr4('Mouf\\Composer\\', ['src/']); + +return $classLoader;