File tree Expand file tree Collapse file tree 3 files changed +21
-5
lines changed Expand file tree Collapse file tree 3 files changed +21
-5
lines changed Original file line number Diff line number Diff line change 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 >%maker.root_namespace%</argument >
18+ </service >
1719
1820 <service id =" maker.autoloader_util" class =" Symfony\Bundle\MakerBundle\Util\AutoloaderUtil" >
1921 <argument type =" service" id =" maker.autoloader_finder" />
Original file line number Diff line number Diff line change 1919 */
2020class 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 }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments