Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Clean up event dispatching code and make package events extend instal…
…ler events
  • Loading branch information
Seldaek committed Feb 23, 2015
1 parent 235b0cf commit 3efed22
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 131 deletions.
4 changes: 2 additions & 2 deletions src/Composer/Command/CreateProjectCommand.php
Expand Up @@ -149,7 +149,7 @@ public function installProject(IOInterface $io, Config $config, $packageName, $d

if ($noScripts === false) {
// dispatch event
$composer->getEventDispatcher()->dispatchCommandEvent(ScriptEvents::POST_ROOT_PACKAGE_INSTALL, $installDevPackages);
$composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_ROOT_PACKAGE_INSTALL, $installDevPackages);
}

$rootPackageConfig = $composer->getConfig();
Expand Down Expand Up @@ -217,7 +217,7 @@ public function installProject(IOInterface $io, Config $config, $packageName, $d

if ($noScripts === false) {
// dispatch event
$composer->getEventDispatcher()->dispatchCommandEvent(ScriptEvents::POST_CREATE_PROJECT_CMD, $installDevPackages);
$composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_CREATE_PROJECT_CMD, $installDevPackages);
}

chdir($oldCwd);
Expand Down
18 changes: 4 additions & 14 deletions src/Composer/Command/RunScriptCommand.php
Expand Up @@ -27,25 +27,19 @@ class RunScriptCommand extends Command
/**
* @var array Array with command events
*/
protected $commandEvents = array(
protected $scriptEvents = array(
ScriptEvents::PRE_INSTALL_CMD,
ScriptEvents::POST_INSTALL_CMD,
ScriptEvents::PRE_UPDATE_CMD,
ScriptEvents::POST_UPDATE_CMD,
ScriptEvents::PRE_STATUS_CMD,
ScriptEvents::POST_STATUS_CMD,
ScriptEvents::POST_ROOT_PACKAGE_INSTALL,
ScriptEvents::POST_CREATE_PROJECT_CMD
);

/**
* @var array Array with script events
*/
protected $scriptEvents = array(
ScriptEvents::POST_CREATE_PROJECT_CMD,
ScriptEvents::PRE_ARCHIVE_CMD,
ScriptEvents::POST_ARCHIVE_CMD,
ScriptEvents::PRE_AUTOLOAD_DUMP,
ScriptEvents::POST_AUTOLOAD_DUMP
ScriptEvents::POST_AUTOLOAD_DUMP,
);

protected function configure()
Expand Down Expand Up @@ -78,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$script = $input->getArgument('script');
if (!in_array($script, $this->commandEvents) && !in_array($script, $this->scriptEvents)) {
if (!in_array($script, $this->scriptEvents)) {
if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script));
}
Expand All @@ -98,10 +92,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

$args = $input->getArgument('args');

if (in_array($script, $this->commandEvents)) {
return $composer->getEventDispatcher()->dispatchCommandEvent($script, $input->getOption('dev') || !$input->getOption('no-dev'), $args);
}

return $composer->getEventDispatcher()->dispatchScript($script, $input->getOption('dev') || !$input->getOption('no-dev'), $args);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/StatusCommand.php
Expand Up @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$im = $composer->getInstallationManager();

// Dispatch pre-status-command
$composer->getEventDispatcher()->dispatchCommandEvent(ScriptEvents::PRE_STATUS_CMD, true);
$composer->getEventDispatcher()->dispatchScript(ScriptEvents::PRE_STATUS_CMD, true);

$errors = array();

Expand Down Expand Up @@ -98,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

// Dispatch post-status-command
$composer->getEventDispatcher()->dispatchCommandEvent(ScriptEvents::POST_STATUS_CMD, true);
$composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_STATUS_CMD, true);

return $errors ? 1 : 0;
}
Expand Down
51 changes: 26 additions & 25 deletions src/Composer/EventDispatcher/EventDispatcher.php
Expand Up @@ -21,7 +21,6 @@
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\Repository\CompositeRepository;
use Composer\Script;
use Composer\Script\CommandEvent;
use Composer\Script\PackageEvent;
use Composer\Util\ProcessExecutor;

Expand Down Expand Up @@ -95,36 +94,28 @@ public function dispatchScript($eventName, $devMode = false, $additionalArgs = a
/**
* Dispatch a package event.
*
* @param string $eventName The constant in ScriptEvents
* @param boolean $devMode Whether or not we are in dev mode
* @param OperationInterface $operation The package being installed/updated/removed
* @return int return code of the executed script if any, for php scripts a false return
* value is changed to 1, anything else to 0
*/
public function dispatchPackageEvent($eventName, $devMode, OperationInterface $operation)
{
return $this->doDispatch(new PackageEvent($eventName, $this->composer, $this->io, $devMode, $operation));
}

/**
* Dispatch a command event.
* @param string $eventName The constant in PackageEvents
* @param bool $devMode Whether or not we are in dev mode
* @param PolicyInterface $policy The policy
* @param Pool $pool The pool
* @param CompositeRepository $installedRepo The installed repository
* @param Request $request The request
* @param array $operations The list of operations
* @param OperationInterface $operation The package being installed/updated/removed
*
* @param string $eventName The constant in ScriptEvents
* @param boolean $devMode Whether or not we are in dev mode
* @param array $additionalArgs Arguments passed by the user
* @param array $flags Optional flags to pass data not as argument
* @return int return code of the executed script if any, for php scripts a false return
* value is changed to 1, anything else to 0
* @return int return code of the executed script if any, for php scripts a false return
* value is changed to 1, anything else to 0
*/
public function dispatchCommandEvent($eventName, $devMode, $additionalArgs = array(), $flags = array())
public function dispatchPackageEvent($eventName, $devMode, PolicyInterface $policy, Pool $pool, CompositeRepository $installedRepo, Request $request, array $operations, OperationInterface $operation)
{
return $this->doDispatch(new CommandEvent($eventName, $this->composer, $this->io, $devMode, $additionalArgs, $flags));
return $this->doDispatch(new PackageEvent($eventName, $this->composer, $this->io, $devMode, $policy, $pool, $installedRepo, $request, $operations, $operation));
}

/**
* Dispatch a installer event.
*
* @param string $eventName The constant in InstallerEvents
* @param bool $devMode Whether or not we are in dev mode
* @param PolicyInterface $policy The policy
* @param Pool $pool The pool
* @param CompositeRepository $installedRepo The installed repository
Expand All @@ -134,9 +125,9 @@ public function dispatchCommandEvent($eventName, $devMode, $additionalArgs = arr
* @return int return code of the executed script if any, for php scripts a false return
* value is changed to 1, anything else to 0
*/
public function dispatchInstallerEvent($eventName, PolicyInterface $policy, Pool $pool, CompositeRepository $installedRepo, Request $request, array $operations = array())
public function dispatchInstallerEvent($eventName, $devMode, PolicyInterface $policy, Pool $pool, CompositeRepository $installedRepo, Request $request, array $operations = array())
{
return $this->doDispatch(new InstallerEvent($eventName, $this->composer, $this->io, $policy, $pool, $installedRepo, $request, $operations));
return $this->doDispatch(new InstallerEvent($eventName, $this->composer, $this->io, $devMode, $policy, $pool, $installedRepo, $request, $operations));
}

/**
Expand Down Expand Up @@ -232,8 +223,18 @@ protected function checkListenerExpectedEvent($target, Event $event)

$expected = $typehint->getName();

// BC support
if (!$event instanceof $expected && $expected === 'Composer\Script\CommandEvent') {
$event = new CommandEvent($event->getName(), $event->getComposer(), $event->getIO(), $event->isDevMode(), $event->getArguments());
$event = new \Composer\Script\CommandEvent(
$event->getName(), $event->getComposer(), $event->getIO(), $event->isDevMode(), $event->getArguments()
);
}
if (!$event instanceof $expected && $expected === 'Composer\Script\PackageEvent') {
$event = new \Composer\Script\PackageEvent(
$event->getName(), $event->getComposer(), $event->getIO(), $event->isDevMode(),
$event->getPolicy(), $event->getPool(), $event->getInstalledRepo(), $event->getRequest(),
$event->getOperations(), $event->getOperation()
);
}

return $event;
Expand Down
20 changes: 10 additions & 10 deletions src/Composer/Installer.php
Expand Up @@ -192,7 +192,7 @@ public function run()
if ($this->runScripts) {
// dispatch pre event
$eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
$this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
$this->eventDispatcher->dispatchScript($eventName, $this->devMode);
}

$this->downloadManager->setPreferSource($this->preferSource);
Expand Down Expand Up @@ -289,10 +289,10 @@ public function run()
$request->install($link->getTarget(), $link->getConstraint());
}

$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, $policy, $pool, $installedRepo, $request);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request);
$solver = new Solver($policy, $pool, $installedRepo);
$ops = $solver->solve($request, $this->ignorePlatformReqs);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $policy, $pool, $installedRepo, $request, $ops);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request, $ops);
foreach ($ops as $op) {
if ($op->getJobType() === 'uninstall') {
$devPackages[] = $op->getPackage();
Expand Down Expand Up @@ -334,7 +334,7 @@ public function run()
if ($this->runScripts) {
// dispatch post event
$eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
$this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
$this->eventDispatcher->dispatchScript($eventName, $this->devMode);
}

$vendorDir = $this->config->get('vendor-dir');
Expand Down Expand Up @@ -498,11 +498,11 @@ protected function doInstall($localRepo, $installedRepo, $platformRepo, $aliases
$this->processDevPackages($localRepo, $pool, $policy, $repositories, $lockedRepository, $installFromLock, 'force-links');

// solve dependencies
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, $policy, $pool, $installedRepo, $request);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, $this->devMode, $policy, $pool, $installedRepo, $request);
$solver = new Solver($policy, $pool, $installedRepo);
try {
$operations = $solver->solve($request, $this->ignorePlatformReqs);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $policy, $pool, $installedRepo, $request, $operations);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $this->devMode, $policy, $pool, $installedRepo, $request, $operations);
} catch (SolverProblemsException $e) {
$this->io->write('<error>Your requirements could not be resolved to an installable set of packages.</error>');
$this->io->write($e->getMessage());
Expand Down Expand Up @@ -562,9 +562,9 @@ protected function doInstall($localRepo, $installedRepo, $platformRepo, $aliases
}
}

$event = 'Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType());
$event = 'Composer\Installer\PackageEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType());
if (defined($event) && $this->runScripts) {
$this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $operation);
$this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $policy, $pool, $installedRepo, $request, $operations, $operation);
}

// output non-alias ops in dry run, output alias ops in debug verbosity
Expand Down Expand Up @@ -595,9 +595,9 @@ protected function doInstall($localRepo, $installedRepo, $platformRepo, $aliases
}
}

$event = 'Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType());
$event = 'Composer\Installer\PackageEvents::POST_PACKAGE_'.strtoupper($operation->getJobType());
if (defined($event) && $this->runScripts) {
$this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $operation);
$this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $policy, $pool, $installedRepo, $request, $operations, $operation);
}

if (!$this->dryRun) {
Expand Down
17 changes: 16 additions & 1 deletion src/Composer/Installer/InstallerEvent.php
Expand Up @@ -38,6 +38,11 @@ class InstallerEvent extends Event
*/
private $io;

/**
* @var bool
*/
private $devMode;

/**
* @var PolicyInterface
*/
Expand Down Expand Up @@ -69,18 +74,20 @@ class InstallerEvent extends Event
* @param string $eventName
* @param Composer $composer
* @param IOInterface $io
* @param bool $devMode
* @param PolicyInterface $policy
* @param Pool $pool
* @param CompositeRepository $installedRepo
* @param Request $request
* @param OperationInterface[] $operations
*/
public function __construct($eventName, Composer $composer, IOInterface $io, PolicyInterface $policy, Pool $pool, CompositeRepository $installedRepo, Request $request, array $operations = array())
public function __construct($eventName, Composer $composer, IOInterface $io, $devMode, PolicyInterface $policy, Pool $pool, CompositeRepository $installedRepo, Request $request, array $operations = array())
{
parent::__construct($eventName);

$this->composer = $composer;
$this->io = $io;
$this->devMode = $devMode;
$this->policy = $policy;
$this->pool = $pool;
$this->installedRepo = $installedRepo;
Expand All @@ -104,6 +111,14 @@ public function getIO()
return $this->io;
}

/**
* @return bool
*/
public function isDevMode()
{
return $this->devMode;
}

/**
* @return PolicyInterface
*/
Expand Down
69 changes: 69 additions & 0 deletions src/Composer/Installer/PackageEvent.php
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Composer\Installer;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\DependencyResolver\Operation\OperationInterface;

/**
* The Package Event.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class PackageEvent extends InstallerEvent
{
/**
* @var OperationInterface The package instance
*/
private $operation;

/**
* Constructor.
*
* @param string $eventName
* @param Composer $composer
* @param IOInterface $io
* @param bool $devMode
* @param PolicyInterface $policy
* @param Pool $pool
* @param CompositeRepository $installedRepo
* @param Request $request
* @param OperationInterface[] $operations
* @param OperationInterface $operation
*/
public function __construct($eventName, Composer $composer, IOInterface $io, $devMode, PolicyInterface $policy, Pool $pool, CompositeRepository $installedRepo, Request $request, array $operations, OperationInterface $operation)
{
parent::__construct($eventName);

$this->composer = $composer;
$this->io = $io;
$this->devMode = $devMode;
$this->policy = $policy;
$this->pool = $pool;
$this->installedRepo = $installedRepo;
$this->request = $request;
$this->operations = $operations;
$this->operation = $operation;
}

/**
* Returns the package instance.
*
* @return OperationInterface
*/
public function getOperation()
{
return $this->operation;
}
}

0 comments on commit 3efed22

Please sign in to comment.