1
0
Fork 0

Merge remote-tracking branch 'ronnylt/script-event-post-dump-autoload'

Conflicts:
	tests/Composer/Test/Autoload/AutoloadGeneratorTest.php
pull/1615/merge
Jordi Boggiano 2013-02-22 18:42:29 +01:00
commit 8d55b9cced
12 changed files with 132 additions and 16 deletions

View File

@ -18,6 +18,8 @@ use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface; use Composer\Repository\RepositoryInterface;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Script\EventDispatcher;
use Composer\Script\ScriptEvents;
/** /**
* @author Igor Wiedler <igor@wiedler.ch> * @author Igor Wiedler <igor@wiedler.ch>
@ -25,6 +27,16 @@ use Composer\Util\Filesystem;
*/ */
class AutoloadGenerator 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 = '') public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
{ {
$filesystem = new Filesystem(); $filesystem = new Filesystem();
@ -174,6 +186,8 @@ EOF;
file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); 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)); 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'); copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
$this->eventDispatcher->dispatch(ScriptEvents::POST_AUTOLOAD_DUMP);
} }
public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages) public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)

View File

@ -49,7 +49,6 @@ EOT
$package = $composer->getPackage(); $package = $composer->getPackage();
$config = $composer->getConfig(); $config = $composer->getConfig();
$generator = new AutoloadGenerator(); $composer->getAutoloadGenerator()->dump($config, $localRepos, $package, $installationManager, 'composer', $input->getOption('optimize'));
$generator->dump($config, $localRepos, $package, $installationManager, 'composer', $input->getOption('optimize'));
} }
} }

View File

@ -17,6 +17,8 @@ use Composer\Package\Locker;
use Composer\Repository\RepositoryManager; use Composer\Repository\RepositoryManager;
use Composer\Installer\InstallationManager; use Composer\Installer\InstallationManager;
use Composer\Downloader\DownloadManager; use Composer\Downloader\DownloadManager;
use Composer\Script\EventDispatcher;
use Composer\Autoload\AutoloadGenerator;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -56,6 +58,16 @@ class Composer
*/ */
private $config; private $config;
/**
* @var Script\EventDispatcher
*/
private $eventDispatcher;
/**
* @var Autoload\AutoloadGenerator
*/
private $autoloadGenerator;
/** /**
* @param Package\RootPackageInterface $package * @param Package\RootPackageInterface $package
* @return void * @return void
@ -152,4 +164,36 @@ class Composer
{ {
return $this->installationManager; 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;
}
} }

View File

@ -20,6 +20,8 @@ use Composer\Repository\RepositoryManager;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Composer\Util\RemoteFilesystem; use Composer\Util\RemoteFilesystem;
use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Composer\Script\EventDispatcher;
use Composer\Autoload\AutoloadGenerator;
/** /**
* Creates a configured instance of composer. * Creates a configured instance of composer.
@ -237,6 +239,14 @@ class Factory
$composer->setDownloadManager($dm); $composer->setDownloadManager($dm);
$composer->setInstallationManager($im); $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 // add installers to the manager
$this->createDefaultInstallers($im, $composer, $io); $this->createDefaultInstallers($im, $composer, $io);

View File

@ -741,15 +741,10 @@ class Installer
* *
* @param IOInterface $io * @param IOInterface $io
* @param Composer $composer * @param Composer $composer
* @param EventDispatcher $eventDispatcher
* @param AutoloadGenerator $autoloadGenerator
* @return Installer * @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( return new static(
$io, $io,
$composer->getConfig(), $composer->getConfig(),
@ -758,8 +753,8 @@ class Installer
$composer->getRepositoryManager(), $composer->getRepositoryManager(),
$composer->getLocker(), $composer->getLocker(),
$composer->getInstallationManager(), $composer->getInstallationManager(),
$eventDispatcher, $composer->getEventDispatcher(),
$autoloadGenerator $composer->getAutoloadGenerator()
); );
} }

View File

@ -85,7 +85,7 @@ class InstallerInstaller extends LibraryInstaller
$extra = $package->getExtra(); $extra = $package->getExtra();
$classes = is_array($extra['class']) ? $extra['class'] : array($extra['class']); $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')); $map = $generator->parseAutoloads(array(array($package, $downloadPath)), new Package('dummy', '1.0.0.0', '1.0.0'));
$classLoader = $generator->createLoader($map); $classLoader = $generator->createLoader($map);
$classLoader->register(); $classLoader->register();

View File

@ -50,7 +50,7 @@ class Event
* @param IOInterface $io The IOInterface object * @param IOInterface $io The IOInterface object
* @param boolean $devMode Whether or not we are in dev mode * @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->name = $name;
$this->composer = $composer; $this->composer = $composer;

View File

@ -51,6 +51,21 @@ class EventDispatcher
$this->process = $process ?: new ProcessExecutor(); $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. * Dispatch a package event.
* *
@ -139,7 +154,7 @@ class EventDispatcher
$this->loader->unregister(); $this->loader->unregister();
} }
$generator = new AutoloadGenerator; $generator = $this->composer->getAutoloadGenerator();
$packages = array_merge( $packages = array_merge(
$this->composer->getRepositoryManager()->getLocalRepository()->getPackages(), $this->composer->getRepositoryManager()->getLocalRepository()->getPackages(),
$this->composer->getRepositoryManager()->getLocalDevRepository()->getPackages() $this->composer->getRepositoryManager()->getLocalDevRepository()->getPackages()

View File

@ -109,4 +109,13 @@ class ScriptEvents
* @var string * @var string
*/ */
const POST_PACKAGE_UNINSTALL = 'post-package-uninstall'; 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';
} }

View File

@ -18,6 +18,7 @@ use Composer\Util\Filesystem;
use Composer\Package\AliasPackage; use Composer\Package\AliasPackage;
use Composer\Package\Package; use Composer\Package\Package;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Script\ScriptEvents;
class AutoloadGeneratorTest extends TestCase class AutoloadGeneratorTest extends TestCase
{ {
@ -28,6 +29,7 @@ class AutoloadGeneratorTest extends TestCase
private $repository; private $repository;
private $generator; private $generator;
private $fs; private $fs;
private $eventDispatcher;
protected function setUp() protected function setUp()
{ {
@ -69,7 +71,11 @@ class AutoloadGeneratorTest extends TestCase
})); }));
$this->repository = $this->getMock('Composer\Repository\RepositoryInterface'); $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() protected function tearDown()
@ -614,6 +620,22 @@ EOF;
$this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php")); $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() public function testUseGlobalIncludePath()
{ {

View File

@ -18,6 +18,7 @@ use Composer\Installer\InstallerInstaller;
use Composer\Package\Loader\JsonLoader; use Composer\Package\Loader\JsonLoader;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Autoload\AutoloadGenerator;
class InstallerInstallerTest extends \PHPUnit_Framework_TestCase class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
{ {
@ -26,6 +27,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
protected $im; protected $im;
protected $repository; protected $repository;
protected $io; protected $io;
protected $autoloadGenerator;
protected function setUp() protected function setUp()
{ {
@ -54,12 +56,16 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
$this->io = $this->getMock('Composer\IO\IOInterface'); $this->io = $this->getMock('Composer\IO\IOInterface');
$dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock();
$this->autoloadGenerator = new AutoloadGenerator($dispatcher);
$this->composer = new Composer(); $this->composer = new Composer();
$config = new Config(); $config = new Config();
$this->composer->setConfig($config); $this->composer->setConfig($config);
$this->composer->setDownloadManager($dm); $this->composer->setDownloadManager($dm);
$this->composer->setInstallationManager($this->im); $this->composer->setInstallationManager($this->im);
$this->composer->setRepositoryManager($rm); $this->composer->setRepositoryManager($rm);
$this->composer->setAutoloadGenerator($this->autoloadGenerator);
$config->merge(array( $config->merge(array(
'config' => array( 'config' => array(

View File

@ -52,8 +52,9 @@ class InstallerTest extends TestCase
$locker = $this->getMockBuilder('Composer\Package\Locker')->disableOriginalConstructor()->getMock(); $locker = $this->getMockBuilder('Composer\Package\Locker')->disableOriginalConstructor()->getMock();
$installationManager = new InstallationManagerMock(); $installationManager = new InstallationManagerMock();
$eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock(); $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); $installer = new Installer($io, $config, clone $rootPackage, $downloadManager, $repositoryManager, $locker, $installationManager, $eventDispatcher, $autoloadGenerator);
$result = $installer->run(); $result = $installer->run();
@ -184,7 +185,8 @@ class InstallerTest extends TestCase
$locker = new Locker($lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig))); $locker = new Locker($lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig)));
$composer->setLocker($locker); $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( $installer = Installer::create(
$io, $io,