2012-03-03 05:35:40 +00:00
< ? 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 ;
use Composer\Autoload\AutoloadGenerator ;
use Composer\DependencyResolver\DefaultPolicy ;
use Composer\DependencyResolver\Operation\UpdateOperation ;
use Composer\DependencyResolver\Pool ;
use Composer\DependencyResolver\Request ;
use Composer\DependencyResolver\Solver ;
2012-03-18 21:43:07 +00:00
use Composer\DependencyResolver\SolverProblemsException ;
2012-03-05 23:48:07 +00:00
use Composer\Downloader\DownloadManager ;
use Composer\Installer\InstallationManager ;
2012-06-24 19:58:51 +00:00
use Composer\Config ;
2012-05-23 13:39:33 +00:00
use Composer\Installer\NoopInstaller ;
2012-03-03 05:35:40 +00:00
use Composer\IO\IOInterface ;
use Composer\Package\AliasPackage ;
use Composer\Package\Link ;
use Composer\Package\LinkConstraint\VersionConstraint ;
2012-03-05 23:48:07 +00:00
use Composer\Package\Locker ;
2012-03-03 05:35:40 +00:00
use Composer\Package\PackageInterface ;
2012-08-23 13:52:40 +00:00
use Composer\Package\RootPackageInterface ;
2012-03-03 05:35:40 +00:00
use Composer\Repository\CompositeRepository ;
2012-05-09 18:03:19 +00:00
use Composer\Repository\InstalledArrayRepository ;
2012-03-03 05:35:40 +00:00
use Composer\Repository\PlatformRepository ;
use Composer\Repository\RepositoryInterface ;
2012-03-05 23:48:07 +00:00
use Composer\Repository\RepositoryManager ;
2012-03-03 05:35:40 +00:00
use Composer\Script\EventDispatcher ;
use Composer\Script\ScriptEvents ;
2012-03-10 00:16:37 +00:00
/**
* @ author Jordi Boggiano < j . boggiano @ seld . be >
* @ author Beau Simensen < beau @ dflydev . com >
* @ author Konstantin Kudryashov < ever . zet @ gmail . com >
*/
2012-03-06 23:30:18 +00:00
class Installer
2012-03-03 05:35:40 +00:00
{
/**
2012-03-05 23:48:07 +00:00
* @ var IOInterface
*/
protected $io ;
2012-06-24 19:58:51 +00:00
/**
* @ var Config
*/
protected $config ;
2012-03-05 23:48:07 +00:00
/**
2012-08-23 13:52:40 +00:00
* @ var RootPackageInterface
2012-03-05 23:48:07 +00:00
*/
protected $package ;
/**
* @ var DownloadManager
*/
protected $downloadManager ;
/**
* @ var RepositoryManager
*/
protected $repositoryManager ;
/**
* @ var Locker
*/
protected $locker ;
/**
* @ var InstallationManager
*/
protected $installationManager ;
/**
* @ var EventDispatcher
*/
protected $eventDispatcher ;
2012-04-27 09:42:58 +00:00
/**
* @ var AutoloadGenerator
*/
protected $autoloadGenerator ;
2012-03-10 18:56:15 +00:00
protected $preferSource = false ;
2012-08-31 20:25:17 +00:00
protected $preferDist = false ;
2012-10-24 15:33:31 +00:00
protected $optimizeAutoloader = false ;
2012-04-14 09:55:57 +00:00
protected $devMode = false ;
2012-03-10 18:56:15 +00:00
protected $dryRun = false ;
protected $verbose = false ;
protected $update = false ;
2012-05-13 11:25:02 +00:00
protected $runScripts = true ;
2012-05-26 13:20:27 +00:00
protected $updateWhitelist = null ;
2012-03-10 17:08:36 +00:00
2012-04-15 15:44:47 +00:00
/**
* @ var array
*/
protected $suggestedPackages ;
2012-03-10 17:08:36 +00:00
/**
* @ var RepositoryInterface
*/
protected $additionalInstalledRepository ;
2012-03-05 23:48:07 +00:00
/**
* Constructor
2012-03-10 00:16:37 +00:00
*
2012-10-18 08:35:06 +00:00
* @ param IOInterface $io
* @ param Config $config
* @ param RootPackageInterface $package
* @ param DownloadManager $downloadManager
* @ param RepositoryManager $repositoryManager
* @ param Locker $locker
* @ param InstallationManager $installationManager
* @ param EventDispatcher $eventDispatcher
* @ param AutoloadGenerator $autoloadGenerator
2012-03-05 23:48:07 +00:00
*/
2012-08-23 13:52:40 +00:00
public function __construct ( IOInterface $io , Config $config , RootPackageInterface $package , DownloadManager $downloadManager , RepositoryManager $repositoryManager , Locker $locker , InstallationManager $installationManager , EventDispatcher $eventDispatcher , AutoloadGenerator $autoloadGenerator )
2012-03-05 23:48:07 +00:00
{
$this -> io = $io ;
2012-06-24 19:58:51 +00:00
$this -> config = $config ;
2012-03-05 23:48:07 +00:00
$this -> package = $package ;
$this -> downloadManager = $downloadManager ;
$this -> repositoryManager = $repositoryManager ;
$this -> locker = $locker ;
$this -> installationManager = $installationManager ;
$this -> eventDispatcher = $eventDispatcher ;
2012-04-27 09:42:58 +00:00
$this -> autoloadGenerator = $autoloadGenerator ;
2012-03-05 23:48:07 +00:00
}
/**
* Run installation ( or update )
2012-03-03 05:35:40 +00:00
*/
2012-03-10 17:08:36 +00:00
public function run ()
2012-03-03 05:35:40 +00:00
{
2012-03-10 17:08:36 +00:00
if ( $this -> dryRun ) {
$this -> verbose = true ;
2012-05-23 13:16:24 +00:00
$this -> runScripts = false ;
2012-05-23 13:39:33 +00:00
$this -> installationManager -> addInstaller ( new NoopInstaller );
2012-10-22 13:50:40 +00:00
$this -> mockLocalRepositories ( $this -> repositoryManager );
2012-03-03 05:35:40 +00:00
}
2012-03-10 17:08:36 +00:00
if ( $this -> preferSource ) {
2012-03-05 23:48:07 +00:00
$this -> downloadManager -> setPreferSource ( true );
2012-03-03 05:35:40 +00:00
}
2012-08-31 20:25:17 +00:00
if ( $this -> preferDist ) {
$this -> downloadManager -> setPreferDist ( true );
}
2012-03-03 05:35:40 +00:00
// create installed repo, this contains all local packages + platform packages (php & extensions)
2012-05-28 16:57:59 +00:00
$installedRootPackage = clone $this -> package ;
$installedRootPackage -> setRequires ( array ());
$installedRootPackage -> setDevRequires ( array ());
2012-06-05 15:41:50 +00:00
$platformRepo = new PlatformRepository ();
2012-04-27 09:42:58 +00:00
$repos = array_merge (
$this -> repositoryManager -> getLocalRepositories (),
array (
2012-05-28 16:57:59 +00:00
new InstalledArrayRepository ( array ( $installedRootPackage )),
2012-06-05 15:41:50 +00:00
$platformRepo ,
2012-04-27 09:42:58 +00:00
)
);
2012-04-14 13:45:25 +00:00
$installedRepo = new CompositeRepository ( $repos );
2012-03-10 17:08:36 +00:00
if ( $this -> additionalInstalledRepository ) {
$installedRepo -> addRepository ( $this -> additionalInstalledRepository );
2012-03-03 05:35:40 +00:00
}
2012-08-22 13:39:16 +00:00
$aliases = $this -> getRootAliases ();
$this -> aliasPlatformPackages ( $platformRepo , $aliases );
2012-03-03 05:35:40 +00:00
2012-05-23 13:16:24 +00:00
if ( $this -> runScripts ) {
2012-04-14 13:45:25 +00:00
// dispatch pre event
2012-03-10 17:08:36 +00:00
$eventName = $this -> update ? ScriptEvents :: PRE_UPDATE_CMD : ScriptEvents :: PRE_INSTALL_CMD ;
2012-03-05 23:48:07 +00:00
$this -> eventDispatcher -> dispatchCommandEvent ( $eventName );
2012-03-03 05:35:40 +00:00
}
2012-04-15 15:44:47 +00:00
$this -> suggestedPackages = array ();
2012-04-15 17:05:16 +00:00
if ( ! $this -> doInstall ( $this -> repositoryManager -> getLocalRepository (), $installedRepo , $aliases )) {
2012-04-15 15:44:47 +00:00
return false ;
}
2012-04-14 13:45:25 +00:00
if ( $this -> devMode ) {
2012-04-15 17:05:16 +00:00
if ( ! $this -> doInstall ( $this -> repositoryManager -> getLocalDevRepository (), $installedRepo , $aliases , true )) {
2012-04-15 15:44:47 +00:00
return false ;
}
2012-04-14 13:45:25 +00:00
}
2012-04-19 19:55:35 +00:00
// output suggestions
2012-04-15 15:44:47 +00:00
foreach ( $this -> suggestedPackages as $suggestion ) {
2012-09-05 19:00:24 +00:00
$target = $suggestion [ 'target' ];
if ( $installedRepo -> filterPackages ( function ( PackageInterface $package ) use ( $target ) {
if ( in_array ( $target , $package -> getNames ())) {
return false ;
}
})) {
2012-04-27 12:40:08 +00:00
$this -> io -> write ( $suggestion [ 'source' ] . ' suggests installing ' . $suggestion [ 'target' ] . ' (' . $suggestion [ 'reason' ] . ')' );
}
2012-04-14 13:45:25 +00:00
}
if ( ! $this -> dryRun ) {
// write lock
if ( $this -> update || ! $this -> locker -> isLocked ()) {
$updatedLock = $this -> locker -> setLockData (
$this -> repositoryManager -> getLocalRepository () -> getPackages (),
2012-04-15 17:05:50 +00:00
$this -> devMode ? $this -> repositoryManager -> getLocalDevRepository () -> getPackages () : null ,
2012-05-11 15:20:10 +00:00
$aliases ,
$this -> package -> getMinimumStability (),
$this -> package -> getStabilityFlags ()
2012-04-14 13:45:25 +00:00
);
if ( $updatedLock ) {
$this -> io -> write ( '<info>Writing lock file</info>' );
}
}
// write autoloader
$this -> io -> write ( '<info>Generating autoload files</info>' );
$localRepos = new CompositeRepository ( $this -> repositoryManager -> getLocalRepositories ());
2012-10-24 15:33:31 +00:00
$this -> autoloadGenerator -> dump ( $this -> config , $localRepos , $this -> package , $this -> installationManager , 'composer' , $this -> optimizeAutoloader );
2012-04-14 13:45:25 +00:00
2012-05-13 11:25:02 +00:00
if ( $this -> runScripts ) {
2012-04-16 17:45:06 +00:00
// dispatch post event
$eventName = $this -> update ? ScriptEvents :: POST_UPDATE_CMD : ScriptEvents :: POST_INSTALL_CMD ;
$this -> eventDispatcher -> dispatchCommandEvent ( $eventName );
}
2012-04-14 13:45:25 +00:00
}
return true ;
}
2012-04-15 17:05:16 +00:00
protected function doInstall ( $localRepo , $installedRepo , $aliases , $devMode = false )
2012-04-14 13:45:25 +00:00
{
2012-05-11 15:20:10 +00:00
$minimumStability = $this -> package -> getMinimumStability ();
$stabilityFlags = $this -> package -> getStabilityFlags ();
2012-04-24 08:49:49 +00:00
// initialize locker to create aliased packages
2012-09-14 14:43:56 +00:00
$installFromLock = false ;
2012-04-24 08:49:49 +00:00
if ( ! $this -> update && $this -> locker -> isLocked ( $devMode )) {
2012-09-14 14:43:56 +00:00
$installFromLock = true ;
$lockedRepository = $this -> locker -> getLockedRepository ( $devMode );
2012-05-11 15:20:10 +00:00
$minimumStability = $this -> locker -> getMinimumStability ();
$stabilityFlags = $this -> locker -> getStabilityFlags ();
2012-04-24 08:49:49 +00:00
}
2012-05-28 10:02:15 +00:00
$this -> whitelistUpdateDependencies (
$localRepo ,
$devMode ,
$this -> package -> getRequires (),
2012-07-01 16:03:01 +00:00
$this -> package -> getDevRequires ()
);
2012-05-27 23:58:54 +00:00
2012-08-16 23:42:05 +00:00
$this -> io -> write ( '<info>Loading composer repositories with package information</info>' );
2012-04-15 17:05:16 +00:00
// creating repository pool
2012-05-11 15:20:10 +00:00
$pool = new Pool ( $minimumStability , $stabilityFlags );
2012-08-22 12:20:43 +00:00
$pool -> addRepository ( $installedRepo , $aliases );
2012-09-14 14:43:56 +00:00
if ( $installFromLock ) {
2012-09-14 15:42:12 +00:00
$pool -> addRepository ( $lockedRepository , $aliases );
2012-09-14 14:43:56 +00:00
}
2012-09-14 15:42:12 +00:00
2012-09-14 15:49:03 +00:00
if ( ! $installFromLock || ! $this -> locker -> isCompleteFormat ( $devMode )) {
2012-09-14 15:42:12 +00:00
$repositories = $this -> repositoryManager -> getRepositories ();
foreach ( $repositories as $repository ) {
$pool -> addRepository ( $repository , $aliases );
}
2012-04-15 17:05:16 +00:00
}
2012-03-03 05:35:40 +00:00
// creating requirements request
$request = new Request ( $pool );
2012-04-27 09:42:58 +00:00
$constraint = new VersionConstraint ( '=' , $this -> package -> getVersion ());
2012-10-22 08:27:12 +00:00
$constraint -> setPrettyString ( $this -> package -> getPrettyVersion ());
2012-04-27 09:42:58 +00:00
$request -> install ( $this -> package -> getName (), $constraint );
2012-03-10 17:08:36 +00:00
if ( $this -> update ) {
2012-04-14 13:45:25 +00:00
$this -> io -> write ( '<info>Updating ' . ( $devMode ? 'dev ' : '' ) . 'dependencies</info>' );
2012-03-03 05:35:40 +00:00
$request -> updateAll ();
2012-04-14 13:45:25 +00:00
$links = $devMode ? $this -> package -> getDevRequires () : $this -> package -> getRequires ();
2012-03-03 05:35:40 +00:00
foreach ( $links as $link ) {
$request -> install ( $link -> getTarget (), $link -> getConstraint ());
}
2012-09-14 14:43:56 +00:00
} elseif ( $installFromLock ) {
2012-04-14 13:45:25 +00:00
$this -> io -> write ( '<info>Installing ' . ( $devMode ? 'dev ' : '' ) . 'dependencies from lock file</info>' );
2012-03-03 05:35:40 +00:00
2012-04-14 13:45:25 +00:00
if ( ! $this -> locker -> isFresh () && ! $devMode ) {
2012-11-10 20:38:19 +00:00
$this -> io -> write ( '<warning>Warning: The lock file is not up to date with the latest changes in composer.json, you may be getting outdated dependencies, run update to update them.</warning>' );
2012-03-03 05:35:40 +00:00
}
2012-09-14 14:43:56 +00:00
foreach ( $lockedRepository -> getPackages () as $package ) {
2012-03-03 05:35:40 +00:00
$version = $package -> getVersion ();
2012-08-22 12:20:43 +00:00
if ( isset ( $aliases [ $package -> getName ()][ $version ])) {
$version = $aliases [ $package -> getName ()][ $version ][ 'alias_normalized' ];
2012-03-03 05:35:40 +00:00
}
$constraint = new VersionConstraint ( '=' , $version );
2012-10-22 08:27:12 +00:00
$constraint -> setPrettyString ( $package -> getPrettyVersion ());
2012-03-03 05:35:40 +00:00
$request -> install ( $package -> getName (), $constraint );
}
} else {
2012-04-14 13:45:25 +00:00
$this -> io -> write ( '<info>Installing ' . ( $devMode ? 'dev ' : '' ) . 'dependencies</info>' );
2012-03-03 05:35:40 +00:00
2012-04-14 13:45:25 +00:00
$links = $devMode ? $this -> package -> getDevRequires () : $this -> package -> getRequires ();
2012-03-03 05:35:40 +00:00
foreach ( $links as $link ) {
$request -> install ( $link -> getTarget (), $link -> getConstraint ());
}
}
2012-04-27 19:24:07 +00:00
// fix the version of all installed packages (+ platform) that are not
// in the current local repo to prevent rogue updates (e.g. non-dev
// updating when in dev)
2012-04-14 13:45:25 +00:00
foreach ( $installedRepo -> getPackages () as $package ) {
2012-07-01 16:03:01 +00:00
if ( $package -> getRepository () === $localRepo ) {
2012-04-14 13:45:25 +00:00
continue ;
}
$constraint = new VersionConstraint ( '=' , $package -> getVersion ());
2012-10-22 08:27:12 +00:00
$constraint -> setPrettyString ( $package -> getPrettyVersion ());
2012-04-14 13:45:25 +00:00
$request -> install ( $package -> getName (), $constraint );
}
2012-07-01 16:03:01 +00:00
// if the updateWhitelist is enabled, packages not in it are also fixed
// to the version specified in the lock, or their currently installed version
if ( $this -> update && $this -> updateWhitelist ) {
if ( $this -> locker -> isLocked ( $devMode )) {
2012-09-14 14:43:56 +00:00
$currentPackages = $this -> locker -> getLockedRepository ( $devMode ) -> getPackages ();
2012-07-01 16:03:01 +00:00
} else {
$currentPackages = $installedRepo -> getPackages ();
}
// collect links from composer as well as installed packages
$candidates = array ();
foreach ( $links as $link ) {
$candidates [ $link -> getTarget ()] = true ;
}
foreach ( $localRepo -> getPackages () as $package ) {
$candidates [ $package -> getName ()] = true ;
}
// fix them to the version in lock (or currently installed) if they are not updateable
foreach ( $candidates as $candidate => $dummy ) {
foreach ( $currentPackages as $curPackage ) {
if ( $curPackage -> getName () === $candidate ) {
if ( $this -> isUpdateable ( $curPackage )) {
break ;
}
$constraint = new VersionConstraint ( '=' , $curPackage -> getVersion ());
$request -> install ( $curPackage -> getName (), $constraint );
}
}
}
}
2012-03-03 05:35:40 +00:00
// prepare solver
2012-03-10 00:16:37 +00:00
$policy = new DefaultPolicy ();
$solver = new Solver ( $policy , $pool , $installedRepo );
2012-03-03 05:35:40 +00:00
// solve dependencies
2012-03-18 21:43:07 +00:00
try {
$operations = $solver -> solve ( $request );
} catch ( SolverProblemsException $e ) {
2012-07-15 04:06:44 +00:00
$this -> io -> write ( '<error>Your requirements could not be resolved to an installable set of packages.</error>' );
2012-03-18 21:43:07 +00:00
$this -> io -> write ( $e -> getMessage ());
return false ;
}
2012-03-03 05:35:40 +00:00
2012-10-22 13:55:29 +00:00
if ( $devMode ) {
// remove bogus operations that the solver creates for stuff that was force-updated in the non-dev pass
// TODO this should not be necessary ideally, but it seems to work around the problem quite well
foreach ( $operations as $index => $op ) {
if ( 'update' === $op -> getJobType () && $op -> getInitialPackage () -> getUniqueName () === $op -> getTargetPackage () -> getUniqueName ()
&& $op -> getInitialPackage () -> getSourceReference () === $op -> getTargetPackage () -> getSourceReference ()
&& $op -> getInitialPackage () -> getDistReference () === $op -> getTargetPackage () -> getDistReference ()
) {
unset ( $operations [ $index ]);
}
}
}
2012-04-02 19:46:05 +00:00
// force dev packages to be updated if we update or install from a (potentially new) lock
2012-05-16 15:14:25 +00:00
foreach ( $localRepo -> getPackages () as $package ) {
// skip non-dev packages
if ( ! $package -> isDev ()) {
continue ;
}
2012-08-24 10:06:29 +00:00
if ( $package instanceof AliasPackage ) {
continue ;
}
2012-05-16 15:14:25 +00:00
// skip packages that will be updated/uninstalled
foreach ( $operations as $operation ) {
if (( 'update' === $operation -> getJobType () && $operation -> getInitialPackage () -> equals ( $package ))
|| ( 'uninstall' === $operation -> getJobType () && $operation -> getPackage () -> equals ( $package ))
) {
continue 2 ;
2012-03-03 05:35:40 +00:00
}
2012-05-16 15:14:25 +00:00
}
2012-03-03 05:35:40 +00:00
2012-05-16 15:14:25 +00:00
// force update to locked version if it does not match the installed version
if ( $installFromLock ) {
2012-09-14 14:43:56 +00:00
foreach ( $lockedRepository -> findPackages ( $package -> getName ()) as $lockedPackage ) {
if (
$lockedPackage -> isDev ()
2012-09-27 16:36:55 +00:00
&& (
( $lockedPackage -> getSourceReference () && $lockedPackage -> getSourceReference () !== $package -> getSourceReference ())
|| ( $lockedPackage -> getDistReference () && $lockedPackage -> getDistReference () !== $package -> getDistReference ())
)
2012-09-14 14:43:56 +00:00
) {
2012-09-20 23:52:19 +00:00
$operations [] = new UpdateOperation ( $package , $lockedPackage );
2012-09-14 14:43:56 +00:00
2012-05-16 15:14:25 +00:00
break ;
2012-03-03 05:35:40 +00:00
}
}
2012-05-16 15:14:25 +00:00
} else {
2012-04-02 19:46:05 +00:00
// force update to latest on update
if ( $this -> update ) {
2012-05-27 21:21:10 +00:00
// skip package if the whitelist is enabled and it is not in it
2012-05-27 22:11:47 +00:00
if ( $this -> updateWhitelist && ! $this -> isUpdateable ( $package )) {
2012-05-26 13:20:27 +00:00
continue ;
}
2012-10-24 15:06:42 +00:00
// find similar packages (name/version) in all repositories
2012-08-24 00:29:03 +00:00
$matches = $pool -> whatProvides ( $package -> getName (), new VersionConstraint ( '=' , $package -> getVersion ()));
2012-10-24 15:06:42 +00:00
foreach ( $matches as $index => $match ) {
2012-08-24 10:06:29 +00:00
// skip local packages
if ( ! in_array ( $match -> getRepository (), $repositories , true )) {
2012-10-24 15:06:42 +00:00
unset ( $matches [ $index ]);
2012-08-24 10:06:29 +00:00
continue ;
2012-08-24 00:29:03 +00:00
}
2012-08-24 10:06:29 +00:00
2012-08-27 10:03:07 +00:00
// skip providers/replacers
if ( $match -> getName () !== $package -> getName ()) {
2012-10-24 15:06:42 +00:00
unset ( $matches [ $index ]);
2012-08-27 10:03:07 +00:00
continue ;
}
2012-10-24 15:06:42 +00:00
$matches [ $index ] = $match -> getId ();
2012-08-24 00:29:03 +00:00
}
2012-10-24 15:06:42 +00:00
// select prefered package according to policy rules
2012-10-25 16:18:02 +00:00
if ( $matches && $matches = $policy -> selectPreferedPackages ( $pool , array (), $matches )) {
2012-10-24 15:06:42 +00:00
$newPackage = $pool -> literalToPackage ( $matches [ 0 ]);
if ( $newPackage && $newPackage -> getSourceReference () !== $package -> getSourceReference ()) {
$operations [] = new UpdateOperation ( $package , $newPackage );
}
2012-04-02 19:46:05 +00:00
}
2012-05-16 15:14:25 +00:00
}
// force installed package to update to referenced version if it does not match the installed version
$references = $this -> package -> getReferences ();
if ( isset ( $references [ $package -> getName ()]) && $references [ $package -> getName ()] !== $package -> getSourceReference ()) {
// changing the source ref to update to will be handled in the operations loop below
$operations [] = new UpdateOperation ( $package , clone $package );
2012-03-03 05:35:40 +00:00
}
}
}
// execute operations
if ( ! $operations ) {
2012-04-14 13:45:25 +00:00
$this -> io -> write ( 'Nothing to install or update' );
2012-03-03 05:35:40 +00:00
}
foreach ( $operations as $operation ) {
2012-04-14 10:07:49 +00:00
// collect suggestions
if ( 'install' === $operation -> getJobType ()) {
foreach ( $operation -> getPackage () -> getSuggests () as $target => $reason ) {
2012-04-15 15:44:47 +00:00
$this -> suggestedPackages [] = array (
2012-04-14 10:07:49 +00:00
'source' => $operation -> getPackage () -> getPrettyName (),
'target' => $target ,
'reason' => $reason ,
);
}
}
2012-05-23 13:16:24 +00:00
$event = 'Composer\Script\ScriptEvents::PRE_PACKAGE_' . strtoupper ( $operation -> getJobType ());
if ( defined ( $event ) && $this -> runScripts ) {
$this -> eventDispatcher -> dispatchPackageEvent ( constant ( $event ), $operation );
}
2012-03-03 05:35:40 +00:00
2012-09-14 14:43:56 +00:00
// not installing from lock, force dev packages' references if they're in root package refs
if ( ! $installFromLock ) {
2012-05-23 13:16:24 +00:00
$package = null ;
if ( 'update' === $operation -> getJobType ()) {
$package = $operation -> getTargetPackage ();
} elseif ( 'install' === $operation -> getJobType ()) {
$package = $operation -> getPackage ();
}
if ( $package && $package -> isDev ()) {
$references = $this -> package -> getReferences ();
if ( isset ( $references [ $package -> getName ()])) {
$package -> setSourceReference ( $references [ $package -> getName ()]);
2012-09-27 18:22:08 +00:00
$package -> setDistReference ( $references [ $package -> getName ()]);
2012-05-16 15:14:25 +00:00
}
2012-03-03 05:35:40 +00:00
}
2012-05-23 13:16:24 +00:00
}
2012-05-16 15:14:25 +00:00
2012-08-18 14:01:44 +00:00
// output alias operations in verbose mode, or all ops in dry run
if ( $this -> dryRun || ( $this -> verbose && false !== strpos ( $operation -> getJobType (), 'Alias' ))) {
$this -> io -> write ( ' - ' . $operation );
2012-05-23 14:04:28 +00:00
}
2012-05-23 13:39:33 +00:00
$this -> installationManager -> execute ( $localRepo , $operation );
2012-03-03 05:35:40 +00:00
2012-05-23 13:16:24 +00:00
$event = 'Composer\Script\ScriptEvents::POST_PACKAGE_' . strtoupper ( $operation -> getJobType ());
if ( defined ( $event ) && $this -> runScripts ) {
$this -> eventDispatcher -> dispatchPackageEvent ( constant ( $event ), $operation );
}
2012-03-12 11:24:11 +00:00
2012-05-23 13:16:24 +00:00
if ( ! $this -> dryRun ) {
2012-03-12 11:24:11 +00:00
$localRepo -> write ();
2012-03-03 05:35:40 +00:00
}
}
2012-04-15 15:44:47 +00:00
return true ;
2012-04-14 13:45:25 +00:00
}
2012-08-22 13:39:16 +00:00
private function getRootAliases ()
2012-04-14 13:45:25 +00:00
{
if ( ! $this -> update && $this -> locker -> isLocked ()) {
$aliases = $this -> locker -> getAliases ();
} else {
$aliases = $this -> package -> getAliases ();
2012-04-14 10:07:49 +00:00
}
2012-08-22 12:20:43 +00:00
$normalizedAliases = array ();
2012-04-14 13:45:25 +00:00
foreach ( $aliases as $alias ) {
2012-08-22 13:39:16 +00:00
$normalizedAliases [ $alias [ 'package' ]][ $alias [ 'version' ]] = array (
'alias' => $alias [ 'alias' ],
'alias_normalized' => $alias [ 'alias_normalized' ]
);
2012-03-03 05:35:40 +00:00
}
2012-03-18 21:43:07 +00:00
2012-08-22 12:20:43 +00:00
return $normalizedAliases ;
2012-03-03 05:35:40 +00:00
}
2012-03-05 23:48:07 +00:00
2012-08-22 13:39:16 +00:00
private function aliasPlatformPackages ( PlatformRepository $platformRepo , $aliases )
{
foreach ( $aliases as $package => $versions ) {
foreach ( $versions as $version => $alias ) {
$packages = $platformRepo -> findPackages ( $package , $version );
foreach ( $packages as $package ) {
$package -> setAlias ( $alias [ 'alias_normalized' ]);
$package -> setPrettyAlias ( $alias [ 'alias' ]);
$aliasPackage = new AliasPackage ( $package , $alias [ 'alias_normalized' ], $alias [ 'alias' ]);
$aliasPackage -> setRootPackageAlias ( true );
$platformRepo -> addPackage ( $aliasPackage );
}
}
}
}
2012-05-27 22:11:47 +00:00
private function isUpdateable ( PackageInterface $package )
{
if ( ! $this -> updateWhitelist ) {
throw new \LogicException ( 'isUpdateable should only be called when a whitelist is present' );
}
2012-05-27 23:58:54 +00:00
return isset ( $this -> updateWhitelist [ $package -> getName ()]);
}
/**
* Adds all dependencies of the update whitelist to the whitelist , too .
*
2012-05-28 10:02:15 +00:00
* Packages which are listed as requirements in the root package will be
* skipped including their dependencies , unless they are listed in the
* update whitelist themselves .
*
2012-05-27 23:58:54 +00:00
* @ param RepositoryInterface $localRepo
2012-05-28 14:38:52 +00:00
* @ param boolean $devMode
* @ param array $rootRequires An array of links to packages in require of the root package
* @ param array $rootDevRequires An array of links to packages in require - dev of the root package
2012-05-27 23:58:54 +00:00
*/
2012-05-28 10:02:15 +00:00
private function whitelistUpdateDependencies ( $localRepo , $devMode , array $rootRequires , array $rootDevRequires )
2012-05-27 23:58:54 +00:00
{
if ( ! $this -> updateWhitelist ) {
return ;
2012-05-27 22:11:47 +00:00
}
2012-05-28 10:02:15 +00:00
if ( $devMode ) {
$rootRequires = array_merge ( $rootRequires , $rootDevRequires );
}
$skipPackages = array ();
foreach ( $rootRequires as $require ) {
$skipPackages [ $require -> getTarget ()] = true ;
}
2012-05-27 23:58:54 +00:00
$pool = new Pool ;
$pool -> addRepository ( $localRepo );
$seen = array ();
foreach ( $this -> updateWhitelist as $packageName => $void ) {
$packageQueue = new \SplQueue ;
foreach ( $pool -> whatProvides ( $packageName ) as $depPackage ) {
$packageQueue -> enqueue ( $depPackage );
2012-05-27 22:11:47 +00:00
}
2012-05-27 23:58:54 +00:00
while ( ! $packageQueue -> isEmpty ()) {
$package = $packageQueue -> dequeue ();
if ( isset ( $seen [ $package -> getId ()])) {
continue ;
}
$seen [ $package -> getId ()] = true ;
$this -> updateWhitelist [ $package -> getName ()] = true ;
$requires = $package -> getRequires ();
if ( $devMode ) {
$requires = array_merge ( $requires , $package -> getDevRequires ());
}
foreach ( $requires as $require ) {
$requirePackages = $pool -> whatProvides ( $require -> getTarget ());
foreach ( $requirePackages as $requirePackage ) {
2012-05-28 10:02:15 +00:00
if ( isset ( $skipPackages [ $requirePackage -> getName ()])) {
continue ;
}
2012-05-27 23:58:54 +00:00
$packageQueue -> enqueue ( $requirePackage );
}
}
}
}
2012-05-27 22:11:47 +00:00
}
2012-10-22 13:50:40 +00:00
/**
* Replace local repositories with InstalledArrayRepository instances
*
* This is to prevent any accidental modification of the existing repos on disk
*
* @ param RepositoryManager $rm
*/
private function mockLocalRepositories ( RepositoryManager $rm )
{
$packages = array_map ( function ( $p ) {
return clone $p ;
}, $rm -> getLocalRepository () -> getPackages ());
foreach ( $packages as $key => $package ) {
if ( $package instanceof AliasPackage ) {
unset ( $packages [ $key ]);
}
}
$rm -> setLocalRepository (
new InstalledArrayRepository ( $packages )
);
$packages = array_map ( function ( $p ) {
return clone $p ;
}, $rm -> getLocalDevRepository () -> getPackages ());
foreach ( $packages as $key => $package ) {
if ( $package instanceof AliasPackage ) {
unset ( $packages [ $key ]);
}
}
$rm -> setLocalDevRepository (
new InstalledArrayRepository ( $packages )
);
}
2012-03-05 23:48:07 +00:00
/**
2012-03-06 23:30:18 +00:00
* Create Installer
2012-03-10 00:16:37 +00:00
*
2012-05-22 10:07:08 +00:00
* @ param IOInterface $io
* @ param Composer $composer
* @ param EventDispatcher $eventDispatcher
* @ param AutoloadGenerator $autoloadGenerator
2012-03-06 23:30:18 +00:00
* @ return Installer
2012-03-05 23:48:07 +00:00
*/
2012-05-22 15:13:15 +00:00
public static function create ( IOInterface $io , Composer $composer , EventDispatcher $eventDispatcher = null , AutoloadGenerator $autoloadGenerator = null )
2012-03-05 23:48:07 +00:00
{
2012-03-10 00:16:37 +00:00
$eventDispatcher = $eventDispatcher ? : new EventDispatcher ( $composer , $io );
2012-04-27 09:42:58 +00:00
$autoloadGenerator = $autoloadGenerator ? : new AutoloadGenerator ;
2012-03-10 00:16:37 +00:00
2012-03-05 23:48:07 +00:00
return new static (
$io ,
2012-06-24 19:58:51 +00:00
$composer -> getConfig (),
2012-03-05 23:48:07 +00:00
$composer -> getPackage (),
$composer -> getDownloadManager (),
$composer -> getRepositoryManager (),
$composer -> getLocker (),
$composer -> getInstallationManager (),
2012-04-27 09:42:58 +00:00
$eventDispatcher ,
$autoloadGenerator
2012-03-05 23:48:07 +00:00
);
}
2012-03-10 17:08:36 +00:00
public function setAdditionalInstalledRepository ( RepositoryInterface $additionalInstalledRepository )
{
$this -> additionalInstalledRepository = $additionalInstalledRepository ;
return $this ;
}
/**
2012-10-24 23:14:04 +00:00
* Whether to run in drymode or not
2012-03-10 17:08:36 +00:00
*
2012-05-22 10:07:08 +00:00
* @ param boolean $dryRun
2012-03-10 17:08:36 +00:00
* @ return Installer
*/
2012-04-14 09:55:57 +00:00
public function setDryRun ( $dryRun = true )
2012-03-10 17:08:36 +00:00
{
2012-03-10 19:14:54 +00:00
$this -> dryRun = ( boolean ) $dryRun ;
2012-03-10 17:08:36 +00:00
return $this ;
}
/**
2012-04-14 09:55:57 +00:00
* prefer source installation
2012-03-10 17:08:36 +00:00
*
2012-05-22 10:07:08 +00:00
* @ param boolean $preferSource
2012-03-10 17:08:36 +00:00
* @ return Installer
*/
2012-04-14 09:55:57 +00:00
public function setPreferSource ( $preferSource = true )
2012-03-10 17:08:36 +00:00
{
2012-04-14 09:55:57 +00:00
$this -> preferSource = ( boolean ) $preferSource ;
2012-03-10 17:08:36 +00:00
return $this ;
}
2012-08-31 20:25:17 +00:00
/**
* prefer dist installation
*
* @ param boolean $preferDist
* @ return Installer
*/
public function setPreferDist ( $preferDist = true )
{
$this -> preferDist = ( boolean ) $preferDist ;
return $this ;
}
2012-10-23 11:41:17 +00:00
/**
2012-10-24 23:14:04 +00:00
* Whether or not generated autoloader are optimized
2012-10-23 11:41:17 +00:00
*
2012-10-24 15:33:31 +00:00
* @ param bool $optimizeAutoloader
2012-10-23 11:41:17 +00:00
* @ return Installer
*/
2012-10-24 15:33:31 +00:00
public function setOptimizeAutoloader ( $optimizeAutoloader = false )
2012-10-23 11:41:17 +00:00
{
2012-10-24 15:33:31 +00:00
$this -> optimizeAutoloader = ( boolean ) $optimizeAutoloader ;
2012-10-23 11:41:17 +00:00
return $this ;
}
2012-03-10 17:08:36 +00:00
/**
2012-04-14 09:55:57 +00:00
* update packages
2012-03-10 17:08:36 +00:00
*
2012-05-22 10:07:08 +00:00
* @ param boolean $update
2012-03-10 17:08:36 +00:00
* @ return Installer
*/
2012-04-14 09:55:57 +00:00
public function setUpdate ( $update = true )
2012-03-10 17:08:36 +00:00
{
2012-04-14 09:55:57 +00:00
$this -> update = ( boolean ) $update ;
2012-03-10 17:08:36 +00:00
return $this ;
}
/**
2012-04-14 09:55:57 +00:00
* enables dev packages
2012-03-10 17:08:36 +00:00
*
2012-05-23 09:11:19 +00:00
* @ param boolean $devMode
2012-03-10 17:08:36 +00:00
* @ return Installer
*/
2012-04-14 09:55:57 +00:00
public function setDevMode ( $devMode = true )
2012-03-10 17:08:36 +00:00
{
2012-04-14 09:55:57 +00:00
$this -> devMode = ( boolean ) $devMode ;
2012-03-10 17:08:36 +00:00
return $this ;
}
2012-04-16 17:45:06 +00:00
/**
2012-05-13 11:25:02 +00:00
* set whether to run scripts or not
2012-04-16 17:45:06 +00:00
*
2012-05-27 23:58:49 +00:00
* @ param boolean $runScripts
2012-04-16 17:45:06 +00:00
* @ return Installer
*/
2012-05-13 11:25:02 +00:00
public function setRunScripts ( $runScripts = true )
2012-04-16 17:45:06 +00:00
{
2012-05-13 11:25:02 +00:00
$this -> runScripts = ( boolean ) $runScripts ;
2012-04-16 17:45:06 +00:00
return $this ;
}
2012-06-24 19:58:51 +00:00
/**
* set the config instance
*
* @ param Config $config
* @ return Installer
*/
public function setConfig ( Config $config )
{
$this -> config = $config ;
return $this ;
}
2012-03-10 17:08:36 +00:00
/**
* run in verbose mode
*
2012-05-22 10:07:08 +00:00
* @ param boolean $verbose
2012-03-10 17:08:36 +00:00
* @ return Installer
*/
2012-04-14 09:55:57 +00:00
public function setVerbose ( $verbose = true )
2012-03-10 17:08:36 +00:00
{
2012-03-10 19:14:54 +00:00
$this -> verbose = ( boolean ) $verbose ;
2012-03-10 17:08:36 +00:00
return $this ;
}
2012-05-26 13:20:27 +00:00
/**
* restrict the update operation to a few packages , all other packages
* that are already installed will be kept at their current version
*
* @ param array $packages
* @ return Installer
*/
public function setUpdateWhitelist ( array $packages )
{
2012-05-27 23:58:54 +00:00
$this -> updateWhitelist = array_flip ( array_map ( 'strtolower' , $packages ));
2012-05-26 13:20:27 +00:00
return $this ;
}
2012-07-21 14:51:40 +00:00
/**
* Disables custom installers .
*
* Call this if you want to ensure that third - party code never gets
* executed . The default is to automatically install , and execute
* custom third - party installers .
2012-11-01 15:22:37 +00:00
*
* @ return Installer
2012-07-21 14:51:40 +00:00
*/
public function disableCustomInstallers ()
{
$this -> installationManager -> disableCustomInstallers ();
2012-11-01 15:22:37 +00:00
return $this ;
2012-07-21 14:51:40 +00:00
}
2012-03-03 05:35:40 +00:00
}