Skip to content

More robust autoloader detection #379

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

Merged
merged 3 commits into from
Jun 14, 2019
Merged
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
3 changes: 3 additions & 0 deletions src/DependencyInjection/MakerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function load(array $configs, ContainerBuilder $container)

$rootNamespace = trim($config['root_namespace'], '\\');

$autoloaderFinderDefinition = $container->getDefinition('maker.autoloader_finder');
$autoloaderFinderDefinition->replaceArgument(0, $rootNamespace);

$makeCommandDefinition = $container->getDefinition('maker.generator');
$makeCommandDefinition->replaceArgument(1, $rootNamespace);

Expand Down
4 changes: 3 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<argument>%kernel.project_dir%</argument>
</service>

<service id="maker.autoloader_finder" class="Symfony\Bundle\MakerBundle\Util\ComposerAutoloaderFinder" />
<service id="maker.autoloader_finder" class="Symfony\Bundle\MakerBundle\Util\ComposerAutoloaderFinder" >
<argument /> <!-- root namespace -->
</service>

<service id="maker.autoloader_util" class="Symfony\Bundle\MakerBundle\Util\AutoloaderUtil">
<argument type="service" id="maker.autoloader_finder" />
Expand Down
74 changes: 63 additions & 11 deletions src/Util/ComposerAutoloaderFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@
*/
class ComposerAutoloaderFinder
{
private $rootNamespace;

/**
* @var ClassLoader|null
*/
private $classLoader = null;

public function __construct(string $rootNamespace)
{
$this->rootNamespace = [
'psr0' => rtrim($rootNamespace, '\\'),
'psr4' => rtrim($rootNamespace, '\\').'\\',
];
}

public function getClassLoader(): ClassLoader
{
if (null === $this->classLoader) {
$this->classLoader = $this->findComposerClassLoader();
}

if (null === $this->classLoader) {
throw new \Exception('Composer ClassLoader not found!');
throw new \Exception("Could not find a Composer autoloader that autoloads from '{$this->rootNamespace['psr4']}'");
}

return $this->classLoader;
Expand All @@ -45,19 +55,61 @@ private function findComposerClassLoader()
$autoloadFunctions = spl_autoload_functions();

foreach ($autoloadFunctions as $autoloader) {
if (\is_array($autoloader) && isset($autoloader[0]) && \is_object($autoloader[0])) {
if ($autoloader[0] instanceof ClassLoader) {
return $autoloader[0];
}

if ($autoloader[0] instanceof DebugClassLoader
&& \is_array($autoloader[0]->getClassLoader())
&& $autoloader[0]->getClassLoader()[0] instanceof ClassLoader) {
return $autoloader[0]->getClassLoader()[0];
}
$classLoader = $this->extractComposerClassLoader($autoloader);
if (null === $classLoader) {
continue;
}

$finalClassLoader = $this->locateMatchingClassLoader($classLoader);
if (null !== $finalClassLoader) {
return $finalClassLoader;
}
}

return null;
}

/**
* @return ClassLoader|null
*/
private function extractComposerClassLoader(array $autoloader)
{
if (isset($autoloader[0]) && \is_object($autoloader[0])) {
if ($autoloader[0] instanceof ClassLoader) {
return $autoloader[0];
}
if ($autoloader[0] instanceof DebugClassLoader
&& \is_array($autoloader[0]->getClassLoader())
&& $autoloader[0]->getClassLoader()[0] instanceof ClassLoader) {
return $autoloader[0]->getClassLoader()[0];
}
}

return null;
}

/**
* @return ClassLoader|null
*/
private function locateMatchingClassLoader(ClassLoader $classLoader)
{
$makerClassLoader = null;
foreach ($classLoader->getPrefixesPsr4() as $prefix => $paths) {
if ('Symfony\\Bundle\\MakerBundle\\' === $prefix) {
$makerClassLoader = $classLoader;
}
if (0 === strpos($this->rootNamespace['psr4'], $prefix)) {
return $classLoader;
}
}

foreach ($classLoader->getPrefixes() as $prefix => $paths) {
if (0 === strpos($this->rootNamespace['psr0'], $prefix)) {
return $classLoader;
}
}

// We can default to using the autoloader containing this component if none are matching.
return $makerClassLoader ?: null;
}
}
2 changes: 1 addition & 1 deletion tests/Maker/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function getCommandTests()
])
->addExtraDependencies('orm')
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFormForEntity')
];
];

yield 'form_for_non_entity_dto' => [MakerTestDetails::createTest(
$this->getMakerInstance(MakeForm::class),
Expand Down
1 change: 1 addition & 0 deletions tests/Util/AutoloaderUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private function createComposerAutoloaderFinder(array $composerJsonParams = null
/** @var \PHPUnit_Framework_MockObject_MockObject|ComposerAutoloaderFinder $finder */
$finder = $this
->getMockBuilder(ComposerAutoloaderFinder::class)
->setConstructorArgs(['App\\'])
->getMock();

$finder
Expand Down
38 changes: 35 additions & 3 deletions tests/Util/ComposerAutoloaderFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ComposerAutoloaderFinderTest extends TestCase
{
public static $getSplAutoloadFunctions = 'spl_autoload_functions';

private static $rootNamespace = 'Fake\\It\\Till\\You\\Make\\It\\';

/**
* @after
*/
Expand All @@ -18,9 +20,20 @@ public function resetAutoloadFunction()
self::$getSplAutoloadFunctions = 'spl_autoload_functions';
}

public function testGetClassLoader()
public function providerNamespaces(): \Generator
{
$loader = (new ComposerAutoloaderFinder())->getClassLoader();
yield 'Configured PSR-0' => [rtrim(static::$rootNamespace, '\\'), null];
yield 'Configured PSR-4' => [null, static::$rootNamespace];
yield 'Fallback default' => [null, 'Symfony\\Bundle\\MakerBundle\\'];
}

/**
* @dataProvider providerNamespaces
*/
public function testGetClassLoader($psr0, $psr4)
{
$this->setupAutoloadFunctions($psr0, $psr4);
$loader = (new ComposerAutoloaderFinder(static::$rootNamespace))->getClassLoader();

$this->assertInstanceOf(ClassLoader::class, $loader, 'Wrong ClassLoader found');
}
Expand All @@ -35,7 +48,26 @@ public function testGetClassLoaderWhenItIsEmpty()
};

// throws \Exception
(new ComposerAutoloaderFinder())->getClassLoader();
(new ComposerAutoloaderFinder(static::$rootNamespace))->getClassLoader();
}

/**
* @param string|null $psr0
* @param string|null $psr4
*/
private function setupAutoloadFunctions($psr0, $psr4)
{
self::$getSplAutoloadFunctions = function () use ($psr0, $psr4) {
$loader = new ClassLoader();
if ($psr0) {
$loader->add($psr0, __DIR__);
}
if ($psr4) {
$loader->addPsr4($psr4, __DIR__);
}

return [[$loader, 'loadClass']];
};
}
}

Expand Down