Skip to content

Commit

Permalink
Replace pre/post-dependencies-solving by a pre-operations-exec event …
Browse files Browse the repository at this point in the history
…happening only on install from lock
  • Loading branch information
Seldaek committed Feb 12, 2020
1 parent 835a915 commit d52ce3c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 125 deletions.
4 changes: 2 additions & 2 deletions doc/articles/scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Composer fires the following named events during its execution process:

### Installer Events

- **pre-dependencies-solving**: occurs before the dependencies are resolved.
- **post-dependencies-solving**: occurs after the dependencies have been resolved.
- **pre-operations-exec**: occurs before the install/upgrade/.. operations
are executed when installing a lock file.

### Package Events

Expand Down
15 changes: 6 additions & 9 deletions src/Composer/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,17 @@ public function dispatchPackageEvent($eventName, $devMode, RepositoryInterface $
/**
* 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 RepositorySet $repositorySet The repository set
* @param RepositoryInterface $localRepo The installed repository
* @param Request $request The request
* @param array $operations The list of operations
* @param string $eventName The constant in InstallerEvents
* @param bool $devMode Whether or not we are in dev mode
* @param bool $executeOperations True if operations will be executed, false in --dry-run
* @param Transaction $transaction The transaction contains the list of operations
*
* @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, $devMode, RepositorySet $repositorySet, Pool $pool, Request $request, PolicyInterface $policy, Transaction $transaction = null)
public function dispatchInstallerEvent($eventName, $devMode, $executeOperations, Transaction $transaction)
{
return $this->doDispatch(new InstallerEvent($eventName, $this->composer, $this->io, $devMode, $repositorySet, $pool, $request, $policy, $transaction));
return $this->doDispatch(new InstallerEvent($eventName, $this->composer, $this->io, $devMode, $executeOperations, $transaction));
}

/**
Expand Down
29 changes: 10 additions & 19 deletions src/Composer/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ protected function doUpdate(RepositoryInterface $localRepo, $doInstall)
}

$pool = $repositorySet->createPool($request, $this->eventDispatcher);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, $this->devMode, $repositorySet, $pool, $request, $policy);

// solve dependencies
$solver = new Solver($policy, $pool, $this->io, $repositorySet);
Expand All @@ -400,9 +399,6 @@ protected function doUpdate(RepositoryInterface $localRepo, $doInstall)
return max(1, $e->getCode());
}

// TODO should we warn people / error if plugins in vendor folder do not match contents of lock file before update?
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $this->devMode, $repositorySet, $pool, $request, $policy, $lockTransaction);

$this->io->writeError("Analyzed ".count($pool)." packages to resolve dependencies", true, IOInterface::VERBOSE);
$this->io->writeError("Analyzed ".$ruleSetSize." rules to resolve dependencies", true, IOInterface::VERBOSE);

Expand Down Expand Up @@ -524,11 +520,9 @@ protected function extractDevPackages(LockTransaction $lockTransaction, $platfor

$pool = $repositorySet->createPool($request, $this->eventDispatcher);

$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $repositorySet, $pool, $request, $policy);
$solver = new Solver($policy, $pool, $this->io, $repositorySet);
try {
$nonDevLockTransaction = $solver->solve($request, $this->ignorePlatformReqs);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $repositorySet, $pool, $request, $policy, $nonDevLockTransaction);
$solver = null;
} catch (SolverProblemsException $e) {
$this->io->writeError('<error>Unable to find a compatible set of packages based on your non-dev requirements alone.</error>', true, IOInterface::QUIET);
Expand All @@ -547,22 +541,22 @@ protected function extractDevPackages(LockTransaction $lockTransaction, $platfor
*/
protected function doInstall(RepositoryInterface $localRepo, $alreadySolved = false)
{
$platformRepo = $this->createPlatformRepo(false);
$lockedRepository = $this->locker->getLockedRepository($this->devMode);

// creating repository set
$policy = $this->createPolicy(false);
// use aliases from lock file only, so empty root aliases here
$repositorySet = $this->createRepositorySet(false, $platformRepo, array(), $lockedRepository);
$repositorySet->addRepository($lockedRepository);

$this->io->writeError('<info>Installing dependencies from lock file'.($this->devMode ? ' (including require-dev)' : '').'</info>');

$lockedRepository = $this->locker->getLockedRepository($this->devMode);

// verify that the lock file works with the current platform repository
// we can skip this part if we're doing this as the second step after an update
if (!$alreadySolved) {
$this->io->writeError('<info>Verifying lock file contents can be installed on current platform.</info>');

$platformRepo = $this->createPlatformRepo(false);
// creating repository set
$policy = $this->createPolicy(false);
// use aliases from lock file only, so empty root aliases here
$repositorySet = $this->createRepositorySet(false, $platformRepo, array(), $lockedRepository);
$repositorySet->addRepository($lockedRepository);

// creating requirements request
$request = $this->createRequest($this->fixedRootPackage, $platformRepo, $lockedRepository);

Expand All @@ -579,7 +573,6 @@ protected function doInstall(RepositoryInterface $localRepo, $alreadySolved = fa
}

$pool = $repositorySet->createPool($request, $this->eventDispatcher);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, $this->devMode, $repositorySet, $pool, $request, $policy);

// solve dependencies
$solver = new Solver($policy, $pool, $this->io, $repositorySet);
Expand All @@ -599,13 +592,11 @@ protected function doInstall(RepositoryInterface $localRepo, $alreadySolved = fa

return max(1, $e->getCode());
}

// TODO should we warn people / error if plugins in vendor folder do not match contents of lock file before update?
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $this->devMode, $repositorySet, $pool, $request, $policy, $lockTransaction);
}

// TODO in how far do we need to do anything here to ensure dev packages being updated to latest in lock without version change are treated correctly?
$localRepoTransaction = new LocalRepoTransaction($lockedRepository, $localRepo);
$this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_OPERATIONS_EXEC, $this->devMode, $this->executeOperations, $localRepoTransaction);

if (!$localRepoTransaction->getOperations()) {
$this->io->writeError('Nothing to install, update or remove');
Expand Down
68 changes: 9 additions & 59 deletions src/Composer/Installer/InstallerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
use Composer\IO\IOInterface;
use Composer\Repository\RepositorySet;

/**
* An event for all installer.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
class InstallerEvent extends Event
{
/**
Expand All @@ -44,27 +39,12 @@ class InstallerEvent extends Event
private $devMode;

/**
* @var RepositorySet
*/
private $repositorySet;

/**
* @var Pool
*/
private $pool;

/**
* @var Request
*/
private $request;

/**
* @var PolicyInterface
* @var bool
*/
private $policy;
private $executeOperations;

/**
* @var Transaction|null
* @var Transaction
*/
private $transaction;

Expand All @@ -75,23 +55,17 @@ class InstallerEvent extends Event
* @param Composer $composer
* @param IOInterface $io
* @param bool $devMode
* @param RepositorySet $repositorySet
* @param Pool $pool
* @param Request $request
* @param PolicyInterface $policy
* @param bool $executeOperations
* @param Transaction $transaction
*/
public function __construct($eventName, Composer $composer, IOInterface $io, $devMode, RepositorySet $repositorySet, Pool $pool, Request $request, PolicyInterface $policy, Transaction $transaction = null)
public function __construct($eventName, Composer $composer, IOInterface $io, $devMode, $executeOperations, Transaction $transaction)
{
parent::__construct($eventName);

$this->composer = $composer;
$this->io = $io;
$this->devMode = $devMode;
$this->repositorySet = $repositorySet;
$this->pool = $pool;
$this->request = $request;
$this->policy = $policy;
$this->executeOperations = $executeOperations;
$this->transaction = $transaction;
}

Expand Down Expand Up @@ -120,35 +94,11 @@ public function isDevMode()
}

/**
* @return PolicyInterface
*/
public function getPolicy()
{
return $this->policy;
}

/**
* @return RepositorySet
*/
public function getRepositorySet()
{
return $this->repositorySet;
}

/**
* @return Pool
*/
public function getPool()
{
return $this->pool;
}

/**
* @return Request
* @return bool
*/
public function getRequest()
public function isExecutingOperations()
{
return $this->request;
return $this->executeOperations;
}

/**
Expand Down
25 changes: 4 additions & 21 deletions src/Composer/Installer/InstallerEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,15 @@

namespace Composer\Installer;

/**
* The Installer Events.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
class InstallerEvents
{
/**
* The PRE_DEPENDENCIES_SOLVING event occurs as a installer begins
* resolve operations.
*
* The event listener method receives a
* Composer\Installer\InstallerEvent instance.
*
* @var string
*/
const PRE_DEPENDENCIES_SOLVING = 'pre-dependencies-solving';

/**
* The POST_DEPENDENCIES_SOLVING event occurs as a installer after
* resolve operations.
* The PRE_OPERATIONS_EXEC event occurs before the lock file gets
* installed and operations are executed.
*
* The event listener method receives a
* Composer\Installer\InstallerEvent instance.
* The event listener method receives an Composer\Installer\InstallerEvent instance.
*
* @var string
*/
const POST_DEPENDENCIES_SOLVING = 'post-dependencies-solving';
const PRE_OPERATIONS_EXEC = 'pre-operations-exec';
}
7 changes: 1 addition & 6 deletions tests/Composer/Test/EventDispatcher/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,9 @@ public function testDispatcherInstallerEvents()
->method('getListeners')
->will($this->returnValue(array()));

$policy = $this->getMockBuilder('Composer\DependencyResolver\PolicyInterface')->getMock();
$repositorySet = $this->getMockBuilder('Composer\Repository\RepositorySet')->disableOriginalConstructor()->getMock();
$pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->disableOriginalConstructor()->getMock();
$transaction = $this->getMockBuilder('Composer\DependencyResolver\LockTransaction')->disableOriginalConstructor()->getMock();
$request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();

$dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, true, $repositorySet, $pool, $request, $policy);
$dispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, true, $repositorySet, $pool, $request, $policy, $transaction);
$dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_OPERATIONS_EXEC, true, true, $transaction);
}

public static function call()
Expand Down
11 changes: 2 additions & 9 deletions tests/Composer/Test/Installer/InstallerEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,14 @@ public function testGetter()
{
$composer = $this->getMockBuilder('Composer\Composer')->getMock();
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$policy = $this->getMockBuilder('Composer\DependencyResolver\PolicyInterface')->getMock();
$repositorySet = $this->getMockBuilder('Composer\Repository\RepositorySet')->disableOriginalConstructor()->getMock();
$pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->disableOriginalConstructor()->getMock();
$transaction = $this->getMockBuilder('Composer\DependencyResolver\LockTransaction')->disableOriginalConstructor()->getMock();
$request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
$event = new InstallerEvent('EVENT_NAME', $composer, $io, true, $repositorySet, $pool, $request, $policy, $transaction);
$event = new InstallerEvent('EVENT_NAME', $composer, $io, true, true, $transaction);

$this->assertSame('EVENT_NAME', $event->getName());
$this->assertInstanceOf('Composer\Composer', $event->getComposer());
$this->assertInstanceOf('Composer\IO\IOInterface', $event->getIO());
$this->assertTrue($event->isDevMode());
$this->assertInstanceOf('Composer\DependencyResolver\PolicyInterface', $event->getPolicy());
$this->assertInstanceOf('Composer\Repository\RepositorySet', $event->getRepositorySet());
$this->assertInstanceOf('Composer\DependencyResolver\Pool', $event->getPool());
$this->assertInstanceOf('Composer\DependencyResolver\Request', $event->getRequest());
$this->assertTrue($event->isExecutingOperations());
$this->assertInstanceOf('Composer\DependencyResolver\Transaction', $event->getTransaction());
}
}

0 comments on commit d52ce3c

Please sign in to comment.