-
-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #163 Get the autoloader class from current registered autoloa…
…d functions (silvester) This PR was squashed before being merged into the 1.0-dev branch (closes #163). Discussion ---------- Get the autoloader class from current registered autoload functions If the default folder for vendor is changed the AutloaderUtil class is no more able to find the ClassLoader. Testing the failure can be easily done by changing the **vendor-dir** in composer.json. After adjusting the autoload require in console file, all makers that use AutoloadUtil will fail. Motivation behind this is development with docker. The application is a lot faster on windows and mac if the vendor folder is not a shared folder. Commits ------- bc04e9a Get the autoloader class from current registered autoload functions
- Loading branch information
Showing
3 changed files
with
57 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Bundle\MakerBundle\Util; | ||
|
||
use Composer\Autoload\ClassLoader; | ||
use Symfony\Component\Debug\DebugClassLoader; | ||
|
||
/** | ||
* @author Ryan Weaver <[email protected]> | ||
|
@@ -20,13 +21,10 @@ | |
*/ | ||
class AutoloaderUtil | ||
{ | ||
private static $classLoader; | ||
private $rootDir; | ||
|
||
public function __construct(string $rootDir) | ||
{ | ||
$this->rootDir = $rootDir; | ||
} | ||
/** | ||
* @var ClassLoader | ||
*/ | ||
private $classLoader; | ||
|
||
/** | ||
* Returns the relative path to where a new class should live. | ||
|
@@ -76,16 +74,27 @@ public function getNamespacePrefixForClass(string $className): string | |
|
||
private function getClassLoader(): ClassLoader | ||
{ | ||
if (null === self::$classLoader) { | ||
$autoloadPath = $this->rootDir.'/vendor/autoload.php'; | ||
|
||
if (!file_exists($autoloadPath)) { | ||
throw new \Exception(sprintf('Could not find the autoload file: "%s"', $autoloadPath)); | ||
if (null === $this->classLoader) { | ||
$autoloadFunctions = spl_autoload_functions(); | ||
foreach ($autoloadFunctions as $autoloader) { | ||
if (is_array($autoloader) && isset($autoloader[0]) && is_object($autoloader[0])) { | ||
if ($autoloader[0] instanceof ClassLoader) { | ||
$this->classLoader = $autoloader[0]; | ||
break; | ||
} | ||
if ($autoloader[0] instanceof DebugClassLoader | ||
&& is_array($autoloader[0]->getClassLoader()) | ||
&& $autoloader[0]->getClassLoader()[0] instanceof ClassLoader) { | ||
$this->classLoader = $autoloader[0]->getClassLoader()[0]; | ||
break; | ||
} | ||
} | ||
} | ||
if (null === $this->classLoader) { | ||
throw new \Exception('Composer ClassLoader not found!'); | ||
} | ||
|
||
self::$classLoader = require $autoloadPath; | ||
} | ||
|
||
return self::$classLoader; | ||
return $this->classLoader; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters