Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/ClassNameMapper.php
Original file line numberDiff line numberDiff line change
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 DownExpand 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 DownExpand 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 DownExpand Up@@ -325,4 +364,4 @@ private static function unfactorizeAutoload(array $autoload) {
private static function normalizeDirectory($dir) {
return $dir === '' ? '' : rtrim($dir, '\\/').'/';
}
}
}
9 changes: 9 additions & 0 deletions tests/ClassNameMapperTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,6 +14,15 @@ public function testGetPossibleFiles() {

$this->assertEquals([ 'src/Foo/Bar.php', 'src2/Bar.php' ], $possibleFiles);

//From autoloader instead of composer.json
$mapper = ClassNameMapper::createFromComposerAutoload(__DIR__ . '/Fixtures/test_autoload.php');
$possibleFiles = $mapper->getPossibleFileNames('Mouf\\Composer\\ClassNameMapper');

$this->assertEquals([
'src/ClassNameMapper.php'
],
$possibleFiles
);
}

public function testUseAutoloadDev() {
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/test_autoload.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
<?php

use Composer\Autoload\ClassLoader;

$classLoader = new ClassLoader();
$classLoader->setPsr4('Mouf\\Composer\\', ['src/']);

return $classLoader;