Skip to content

Commit 4aff8ba

Browse files
Only return the autoloader when it contains the root namespace
1 parent 186ffc8 commit 4aff8ba

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

src/DependencyInjection/MakerExtension.php

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function load(array $configs, ContainerBuilder $container)
3939

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

42+
$autoloaderFinderDefinition = $container->getDefinition('maker.autoloader_finder');
43+
$autoloaderFinderDefinition->replaceArgument(0, $rootNamespace);
44+
4245
$makeCommandDefinition = $container->getDefinition('maker.generator');
4346
$makeCommandDefinition->replaceArgument(1, $rootNamespace);
4447

src/Resources/config/services.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
<argument>%kernel.project_dir%</argument>
1414
</service>
1515

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

1820
<service id="maker.autoloader_util" class="Symfony\Bundle\MakerBundle\Util\AutoloaderUtil">
1921
<argument type="service" id="maker.autoloader_finder" />

src/Util/ComposerAutoloaderFinder.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@
1919
*/
2020
class ComposerAutoloaderFinder
2121
{
22+
/**
23+
* @var string
24+
*/
25+
private $rootNamespace;
26+
2227
/**
2328
* @var ClassLoader|null
2429
*/
2530
private $classLoader = null;
2631

32+
public function __construct(string $rootNamespace = 'App\\')
33+
{
34+
$this->rootNamespace = rtrim($rootNamespace, '\\').'\\';
35+
}
36+
2737
public function getClassLoader(): ClassLoader
2838
{
2939
if (null === $this->classLoader) {
@@ -46,13 +56,15 @@ private function findComposerClassLoader()
4656

4757
foreach ($autoloadFunctions as $autoloader) {
4858
if (\is_array($autoloader) && isset($autoloader[0]) && \is_object($autoloader[0])) {
49-
if ($autoloader[0] instanceof ClassLoader) {
59+
if ($autoloader[0] instanceof ClassLoader
60+
&& isset($autoloader[0]->getPrefixesPsr4()[$this->rootNamespace])) {
5061
return $autoloader[0];
5162
}
5263

5364
if ($autoloader[0] instanceof DebugClassLoader
5465
&& \is_array($autoloader[0]->getClassLoader())
55-
&& $autoloader[0]->getClassLoader()[0] instanceof ClassLoader) {
66+
&& $autoloader[0]->getClassLoader()[0] instanceof ClassLoader
67+
&& isset($autoloader[0]->getClassLoader()[0]->getPrefixesPsr4()[$this->rootNamespace])) {
5668
return $autoloader[0]->getClassLoader()[0];
5769
}
5870
}

tests/Maker/FunctionalTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function getCommandTests()
205205
])
206206
->addExtraDependencies('orm')
207207
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFormForEntity')
208-
];
208+
];
209209

210210
yield 'form_for_non_entity_dto' => [MakerTestDetails::createTest(
211211
$this->getMakerInstance(MakeForm::class),

tests/Util/ComposerAutoloaderFinderTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class ComposerAutoloaderFinderTest extends TestCase
1010
{
1111
public static $getSplAutoloadFunctions = 'spl_autoload_functions';
1212

13+
private static $rootNamespace = 'Symfony\\Bundle\\MakerBundle\\';
14+
1315
/**
1416
* @after
1517
*/
@@ -20,7 +22,7 @@ public function resetAutoloadFunction()
2022

2123
public function testGetClassLoader()
2224
{
23-
$loader = (new ComposerAutoloaderFinder())->getClassLoader();
25+
$loader = (new ComposerAutoloaderFinder(static::$rootNamespace))->getClassLoader();
2426

2527
$this->assertInstanceOf(ClassLoader::class, $loader, 'Wrong ClassLoader found');
2628
}
@@ -35,7 +37,7 @@ public function testGetClassLoaderWhenItIsEmpty()
3537
};
3638

3739
// throws \Exception
38-
(new ComposerAutoloaderFinder())->getClassLoader();
40+
(new ComposerAutoloaderFinder(static::$rootNamespace))->getClassLoader();
3941
}
4042
}
4143

0 commit comments

Comments
 (0)