diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 0d585e410..d47c2d8bd 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -18,6 +18,8 @@ use Composer\Package\AliasPackage; use Composer\Package\PackageInterface; use Composer\Repository\RepositoryInterface; use Composer\Util\Filesystem; +use Composer\Script\EventDispatcher; +use Composer\Script\ScriptEvents; /** * @author Igor Wiedler @@ -25,6 +27,16 @@ use Composer\Util\Filesystem; */ class AutoloadGenerator { + /** + * @var EventDispatcher + */ + private $eventDispatcher; + + public function __construct(EventDispatcher $eventDispatcher) + { + $this->eventDispatcher = $eventDispatcher; + } + public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '') { $filesystem = new Filesystem(); @@ -174,6 +186,8 @@ EOF; file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath)); copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); + + $this->eventDispatcher->dispatch(ScriptEvents::POST_AUTOLOAD_DUMP); } public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages) diff --git a/src/Composer/Command/DumpAutoloadCommand.php b/src/Composer/Command/DumpAutoloadCommand.php index 710c25bc6..aa01aecbb 100755 --- a/src/Composer/Command/DumpAutoloadCommand.php +++ b/src/Composer/Command/DumpAutoloadCommand.php @@ -49,7 +49,6 @@ EOT $package = $composer->getPackage(); $config = $composer->getConfig(); - $generator = new AutoloadGenerator(); - $generator->dump($config, $localRepos, $package, $installationManager, 'composer', $input->getOption('optimize')); + $composer->getAutoloadGenerator()->dump($config, $localRepos, $package, $installationManager, 'composer', $input->getOption('optimize')); } } diff --git a/src/Composer/Composer.php b/src/Composer/Composer.php index 2222f85f9..8c5d0785f 100644 --- a/src/Composer/Composer.php +++ b/src/Composer/Composer.php @@ -17,6 +17,8 @@ use Composer\Package\Locker; use Composer\Repository\RepositoryManager; use Composer\Installer\InstallationManager; use Composer\Downloader\DownloadManager; +use Composer\Script\EventDispatcher; +use Composer\Autoload\AutoloadGenerator; /** * @author Jordi Boggiano @@ -56,6 +58,16 @@ class Composer */ private $config; + /** + * @var Script\EventDispatcher + */ + private $eventDispatcher; + + /** + * @var Autoload\AutoloadGenerator + */ + private $autoloadGenerator; + /** * @param Package\RootPackageInterface $package * @return void @@ -152,4 +164,36 @@ class Composer { return $this->installationManager; } + + /** + * @param Script\EventDispatcher $eventDispatcher + */ + public function setEventDispatcher(EventDispatcher $eventDispatcher) + { + $this->eventDispatcher = $eventDispatcher; + } + + /** + * @return Script\EventDispatcher + */ + public function getEventDispatcher() + { + return $this->eventDispatcher; + } + + /** + * @param Autoload\AutoloadGenerator $autoloadGenerator + */ + public function setAutoloadGenerator(AutoloadGenerator $autoloadGenerator) + { + $this->autoloadGenerator = $autoloadGenerator; + } + + /** + * @return Autoload\AutoloadGenerator + */ + public function getAutoloadGenerator() + { + return $this->autoloadGenerator; + } } diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 0d055d81c..64ea0724f 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -20,6 +20,8 @@ use Composer\Repository\RepositoryManager; use Composer\Util\ProcessExecutor; use Composer\Util\RemoteFilesystem; use Symfony\Component\Console\Formatter\OutputFormatterStyle; +use Composer\Script\EventDispatcher; +use Composer\Autoload\AutoloadGenerator; /** * Creates a configured instance of composer. @@ -237,6 +239,14 @@ class Factory $composer->setDownloadManager($dm); $composer->setInstallationManager($im); + // initialize event dispatcher + $dispatcher = new EventDispatcher($composer, $io); + $composer->setEventDispatcher($dispatcher); + + // initialize autoload generator + $generator = new AutoloadGenerator($dispatcher); + $composer->setAutoloadGenerator($generator); + // add installers to the manager $this->createDefaultInstallers($im, $composer, $io); diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index bced107e8..bca65021b 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -741,15 +741,10 @@ class Installer * * @param IOInterface $io * @param Composer $composer - * @param EventDispatcher $eventDispatcher - * @param AutoloadGenerator $autoloadGenerator * @return Installer */ - public static function create(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher = null, AutoloadGenerator $autoloadGenerator = null) + public static function create(IOInterface $io, Composer $composer) { - $eventDispatcher = $eventDispatcher ?: new EventDispatcher($composer, $io); - $autoloadGenerator = $autoloadGenerator ?: new AutoloadGenerator; - return new static( $io, $composer->getConfig(), @@ -758,8 +753,8 @@ class Installer $composer->getRepositoryManager(), $composer->getLocker(), $composer->getInstallationManager(), - $eventDispatcher, - $autoloadGenerator + $composer->getEventDispatcher(), + $composer->getAutoloadGenerator() ); } diff --git a/src/Composer/Installer/InstallerInstaller.php b/src/Composer/Installer/InstallerInstaller.php index cde0030cb..dd2b63cec 100644 --- a/src/Composer/Installer/InstallerInstaller.php +++ b/src/Composer/Installer/InstallerInstaller.php @@ -85,7 +85,7 @@ class InstallerInstaller extends LibraryInstaller $extra = $package->getExtra(); $classes = is_array($extra['class']) ? $extra['class'] : array($extra['class']); - $generator = new AutoloadGenerator; + $generator = $this->composer->getAutoloadGenerator(); $map = $generator->parseAutoloads(array(array($package, $downloadPath)), new Package('dummy', '1.0.0.0', '1.0.0')); $classLoader = $generator->createLoader($map); $classLoader->register(); diff --git a/src/Composer/Script/Event.php b/src/Composer/Script/Event.php index d9be6a944..cafea2948 100644 --- a/src/Composer/Script/Event.php +++ b/src/Composer/Script/Event.php @@ -50,7 +50,7 @@ class Event * @param IOInterface $io The IOInterface object * @param boolean $devMode Whether or not we are in dev mode */ - public function __construct($name, Composer $composer, IOInterface $io, $devMode) + public function __construct($name, Composer $composer, IOInterface $io, $devMode = false) { $this->name = $name; $this->composer = $composer; diff --git a/src/Composer/Script/EventDispatcher.php b/src/Composer/Script/EventDispatcher.php index 7e923ab1e..c24ea21ae 100644 --- a/src/Composer/Script/EventDispatcher.php +++ b/src/Composer/Script/EventDispatcher.php @@ -51,6 +51,21 @@ class EventDispatcher $this->process = $process ?: new ProcessExecutor(); } + /** + * Dispatch a script event. + * + * @param string $eventName The constant in ScriptEvents + * @param Event $event + */ + public function dispatch($eventName, Event $event = null) + { + if (null == $event) { + $event = new Event($eventName, $this->composer, $this->io); + } + + $this->doDispatch($event); + } + /** * Dispatch a package event. * @@ -139,7 +154,7 @@ class EventDispatcher $this->loader->unregister(); } - $generator = new AutoloadGenerator; + $generator = $this->composer->getAutoloadGenerator(); $packages = array_merge( $this->composer->getRepositoryManager()->getLocalRepository()->getPackages(), $this->composer->getRepositoryManager()->getLocalDevRepository()->getPackages() diff --git a/src/Composer/Script/ScriptEvents.php b/src/Composer/Script/ScriptEvents.php index 9f5131345..d8971b15a 100644 --- a/src/Composer/Script/ScriptEvents.php +++ b/src/Composer/Script/ScriptEvents.php @@ -109,4 +109,13 @@ class ScriptEvents * @var string */ const POST_PACKAGE_UNINSTALL = 'post-package-uninstall'; + + /** + * The POST_AUTOLOAD_DUMP event occurs after the autoload file has been generated. + * + * The event listener method receives a Composer\Script\Event instance. + * + * @var string + */ + const POST_AUTOLOAD_DUMP = 'post-autoload-dump'; } diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index aceefa672..32d8bd7a6 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -18,6 +18,7 @@ use Composer\Util\Filesystem; use Composer\Package\AliasPackage; use Composer\Package\Package; use Composer\Test\TestCase; +use Composer\Script\ScriptEvents; class AutoloadGeneratorTest extends TestCase { @@ -28,6 +29,7 @@ class AutoloadGeneratorTest extends TestCase private $repository; private $generator; private $fs; + private $eventDispatcher; protected function setUp() { @@ -69,7 +71,11 @@ class AutoloadGeneratorTest extends TestCase })); $this->repository = $this->getMock('Composer\Repository\RepositoryInterface'); - $this->generator = new AutoloadGenerator(); + $this->eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher') + ->disableOriginalConstructor() + ->getMock(); + + $this->generator = new AutoloadGenerator($this->eventDispatcher); } protected function tearDown() @@ -614,6 +620,22 @@ EOF; $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php")); } + public function testEventIsDispatchedAfterAutoloadDump() + { + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatch') + ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false); + + $package = new Package('a', '1.0', '1.0'); + $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/'))); + + $this->repository->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue(array())); + + $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_8'); + } public function testUseGlobalIncludePath() { diff --git a/tests/Composer/Test/Installer/InstallerInstallerTest.php b/tests/Composer/Test/Installer/InstallerInstallerTest.php index bfc641029..a53ad467c 100644 --- a/tests/Composer/Test/Installer/InstallerInstallerTest.php +++ b/tests/Composer/Test/Installer/InstallerInstallerTest.php @@ -18,6 +18,7 @@ use Composer\Installer\InstallerInstaller; use Composer\Package\Loader\JsonLoader; use Composer\Package\Loader\ArrayLoader; use Composer\Package\PackageInterface; +use Composer\Autoload\AutoloadGenerator; class InstallerInstallerTest extends \PHPUnit_Framework_TestCase { @@ -26,6 +27,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase protected $im; protected $repository; protected $io; + protected $autoloadGenerator; protected function setUp() { @@ -54,12 +56,16 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase $this->io = $this->getMock('Composer\IO\IOInterface'); + $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock(); + $this->autoloadGenerator = new AutoloadGenerator($dispatcher); + $this->composer = new Composer(); $config = new Config(); $this->composer->setConfig($config); $this->composer->setDownloadManager($dm); $this->composer->setInstallationManager($this->im); $this->composer->setRepositoryManager($rm); + $this->composer->setAutoloadGenerator($this->autoloadGenerator); $config->merge(array( 'config' => array( diff --git a/tests/Composer/Test/InstallerTest.php b/tests/Composer/Test/InstallerTest.php index cca2fd95f..036572adc 100644 --- a/tests/Composer/Test/InstallerTest.php +++ b/tests/Composer/Test/InstallerTest.php @@ -52,8 +52,9 @@ class InstallerTest extends TestCase $locker = $this->getMockBuilder('Composer\Package\Locker')->disableOriginalConstructor()->getMock(); $installationManager = new InstallationManagerMock(); + $eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock(); - $autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator'); + $autoloadGenerator = $this->getMockBuilder('Composer\Autoload\AutoloadGenerator')->disableOriginalConstructor()->getMock(); $installer = new Installer($io, $config, clone $rootPackage, $downloadManager, $repositoryManager, $locker, $installationManager, $eventDispatcher, $autoloadGenerator); $result = $installer->run(); @@ -184,7 +185,8 @@ class InstallerTest extends TestCase $locker = new Locker($lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig))); $composer->setLocker($locker); - $autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator'); + $eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock(); + $autoloadGenerator = $this->getMockBuilder('Composer\Autoload\AutoloadGenerator')->disableOriginalConstructor()->getMock(); $installer = Installer::create( $io,