Skip to content

Commit

Permalink
Merge 685d5f2 into fa32a0e
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-jan committed Oct 4, 2019
2 parents fa32a0e + 685d5f2 commit b4d6c48
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/ClassNameMapper.php
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -325,4 +364,4 @@ private static function unfactorizeAutoload(array $autoload) {
private static function normalizeDirectory($dir) {
return $dir === '' ? '' : rtrim($dir, '\\/').'/';
}
}
}
5 changes: 5 additions & 0 deletions tests/ClassNameMapperTest.php
Expand Up @@ -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() {
Expand Down

0 comments on commit b4d6c48

Please sign in to comment.