1
0
Fork 0

Added post-autoload-dump script event.

pull/1456/head
Ronny López 2013-01-06 20:34:52 +01:00
parent c17aac04a9
commit e5cbf83185
11 changed files with 128 additions and 15 deletions

View File

@ -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 <igor@wiedler.ch>
@ -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();
@ -173,6 +185,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));
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
$this->eventDispatcher->dispatch(ScriptEvents::POST_AUTOLOAD_DUMP, false);
}
public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)

View File

@ -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'));
}
}

View File

@ -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 <j.boggiano@seld.be>
@ -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;
}
}

View File

@ -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.
@ -236,6 +238,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);

View File

@ -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()
);
}

View File

@ -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();

View File

@ -51,6 +51,17 @@ class EventDispatcher
$this->process = $process ?: new ProcessExecutor();
}
/**
* Dispatch a script event.
*
* @param string $eventName The constant in ScriptEvents
* @param boolean $devMode Whether or not we are in dev mode
*/
public function dispatch($eventName, $devMode)
{
$this->doDispatch(new Event($eventName, $this->composer, $this->io, $devMode));
}
/**
* Dispatch a package event.
*
@ -139,7 +150,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()

View File

@ -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';
}

View File

@ -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()
{
@ -60,7 +62,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()
@ -571,6 +577,23 @@ 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');
}
private function createClassFile($basedir)
{
if (!is_dir($basedir.'/composersrc')) {

View File

@ -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(

View File

@ -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,