2011-09-23 23:09:51 +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\Downloader;
|
|
|
|
|
|
|
|
use Composer\Package\PackageInterface;
|
2014-02-24 17:40:33 +00:00
|
|
|
use Composer\IO\IOInterface;
|
2012-02-09 17:45:28 +00:00
|
|
|
use Composer\Util\Filesystem;
|
2019-01-17 16:12:33 +00:00
|
|
|
use React\Promise\PromiseInterface;
|
2011-09-23 23:09:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloaders manager.
|
|
|
|
*
|
|
|
|
* @author Konstantin Kudryashov <ever.zet@gmail.com>
|
|
|
|
*/
|
|
|
|
class DownloadManager
|
|
|
|
{
|
2014-02-24 17:40:33 +00:00
|
|
|
private $io;
|
2012-08-31 20:25:17 +00:00
|
|
|
private $preferDist = false;
|
2011-09-23 23:09:51 +00:00
|
|
|
private $preferSource = false;
|
2015-02-26 16:20:59 +00:00
|
|
|
private $packagePreferences = array();
|
2012-04-27 09:21:58 +00:00
|
|
|
private $filesystem;
|
2017-03-08 14:07:29 +00:00
|
|
|
private $downloaders = array();
|
2011-09-23 23:09:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes download manager.
|
|
|
|
*
|
2019-01-17 16:12:33 +00:00
|
|
|
* @param IOInterface $io The Input Output Interface
|
|
|
|
* @param bool $preferSource prefer downloading from source
|
|
|
|
* @param Filesystem|null $filesystem custom Filesystem object
|
2011-09-23 23:09:51 +00:00
|
|
|
*/
|
2014-02-24 17:40:33 +00:00
|
|
|
public function __construct(IOInterface $io, $preferSource = false, Filesystem $filesystem = null)
|
2011-09-23 23:09:51 +00:00
|
|
|
{
|
2014-02-24 17:40:33 +00:00
|
|
|
$this->io = $io;
|
2011-09-23 23:09:51 +00:00
|
|
|
$this->preferSource = $preferSource;
|
2012-04-27 09:21:58 +00:00
|
|
|
$this->filesystem = $filesystem ?: new Filesystem();
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes downloader prefer source installation over the dist.
|
|
|
|
*
|
2013-06-13 11:28:24 +00:00
|
|
|
* @param bool $preferSource prefer downloading from source
|
2013-06-13 00:05:44 +00:00
|
|
|
* @return DownloadManager
|
2011-09-23 23:09:51 +00:00
|
|
|
*/
|
2011-10-30 19:59:41 +00:00
|
|
|
public function setPreferSource($preferSource)
|
2011-09-23 23:09:51 +00:00
|
|
|
{
|
|
|
|
$this->preferSource = $preferSource;
|
2012-05-18 14:12:18 +00:00
|
|
|
|
2012-05-17 18:07:49 +00:00
|
|
|
return $this;
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 20:25:17 +00:00
|
|
|
/**
|
|
|
|
* Makes downloader prefer dist installation over the source.
|
|
|
|
*
|
2013-06-13 11:28:24 +00:00
|
|
|
* @param bool $preferDist prefer downloading from dist
|
2013-06-13 00:05:44 +00:00
|
|
|
* @return DownloadManager
|
2012-08-31 20:25:17 +00:00
|
|
|
*/
|
|
|
|
public function setPreferDist($preferDist)
|
|
|
|
{
|
|
|
|
$this->preferDist = $preferDist;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-02-26 16:20:59 +00:00
|
|
|
/**
|
|
|
|
* Sets fine tuned preference settings for package level source/dist selection.
|
|
|
|
*
|
|
|
|
* @param array $preferences array of preferences by package patterns
|
|
|
|
* @return DownloadManager
|
|
|
|
*/
|
|
|
|
public function setPreferences(array $preferences)
|
|
|
|
{
|
|
|
|
$this->packagePreferences = $preferences;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-09-23 23:09:51 +00:00
|
|
|
/**
|
|
|
|
* Sets installer downloader for a specific installation type.
|
|
|
|
*
|
2013-06-13 11:28:24 +00:00
|
|
|
* @param string $type installation type
|
|
|
|
* @param DownloaderInterface $downloader downloader instance
|
2013-06-13 00:05:44 +00:00
|
|
|
* @return DownloadManager
|
2011-09-23 23:09:51 +00:00
|
|
|
*/
|
|
|
|
public function setDownloader($type, DownloaderInterface $downloader)
|
|
|
|
{
|
2012-05-17 18:07:49 +00:00
|
|
|
$type = strtolower($type);
|
2011-09-23 23:09:51 +00:00
|
|
|
$this->downloaders[$type] = $downloader;
|
2012-05-18 14:12:18 +00:00
|
|
|
|
2012-05-17 18:07:49 +00:00
|
|
|
return $this;
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns downloader for a specific installation type.
|
|
|
|
*
|
2015-09-28 09:51:14 +00:00
|
|
|
* @param string $type installation type
|
2013-06-13 00:05:44 +00:00
|
|
|
* @throws \InvalidArgumentException if downloader for provided type is not registered
|
2015-09-28 09:51:14 +00:00
|
|
|
* @return DownloaderInterface
|
2011-09-23 23:09:51 +00:00
|
|
|
*/
|
|
|
|
public function getDownloader($type)
|
|
|
|
{
|
2012-05-17 18:07:49 +00:00
|
|
|
$type = strtolower($type);
|
2011-09-23 23:09:51 +00:00
|
|
|
if (!isset($this->downloaders[$type])) {
|
2014-01-05 08:19:20 +00:00
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown downloader type: %s. Available types: %s.', $type, implode(', ', array_keys($this->downloaders))));
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->downloaders[$type];
|
|
|
|
}
|
|
|
|
|
2011-09-29 01:11:51 +00:00
|
|
|
/**
|
|
|
|
* Returns downloader for already installed package.
|
|
|
|
*
|
2015-09-28 09:51:14 +00:00
|
|
|
* @param PackageInterface $package package instance
|
2013-06-13 00:05:44 +00:00
|
|
|
* @throws \InvalidArgumentException if package has no installation source specified
|
|
|
|
* @throws \LogicException if specific downloader used to load package with
|
2015-09-28 09:51:14 +00:00
|
|
|
* wrong type
|
|
|
|
* @return DownloaderInterface|null
|
2011-09-29 01:11:51 +00:00
|
|
|
*/
|
2019-01-17 16:12:33 +00:00
|
|
|
public function getDownloaderForPackage(PackageInterface $package)
|
2011-09-29 01:11:51 +00:00
|
|
|
{
|
|
|
|
$installationSource = $package->getInstallationSource();
|
|
|
|
|
2013-12-31 14:21:53 +00:00
|
|
|
if ('metapackage' === $package->getType()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 01:11:51 +00:00
|
|
|
if ('dist' === $installationSource) {
|
|
|
|
$downloader = $this->getDownloader($package->getDistType());
|
|
|
|
} elseif ('source' === $installationSource) {
|
|
|
|
$downloader = $this->getDownloader($package->getSourceType());
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException(
|
2019-01-17 16:12:33 +00:00
|
|
|
'Package '.$package.' does not have an installation source set'
|
2011-09-29 01:11:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($installationSource !== $downloader->getInstallationSource()) {
|
|
|
|
throw new \LogicException(sprintf(
|
2018-01-24 15:19:28 +00:00
|
|
|
'Downloader "%s" is a %s type downloader and can not be used to download %s for package %s',
|
2018-07-24 12:32:52 +00:00
|
|
|
get_class($downloader),
|
|
|
|
$downloader->getInstallationSource(),
|
|
|
|
$installationSource,
|
|
|
|
$package
|
2011-09-29 01:11:51 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $downloader;
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
public function getDownloaderType(DownloaderInterface $downloader)
|
|
|
|
{
|
|
|
|
return array_search($downloader, $this->downloaders);
|
|
|
|
}
|
|
|
|
|
2011-09-23 23:09:51 +00:00
|
|
|
/**
|
|
|
|
* Downloads package into target dir.
|
|
|
|
*
|
2019-08-29 09:37:23 +00:00
|
|
|
* @param PackageInterface $package package instance
|
|
|
|
* @param string $targetDir target dir
|
|
|
|
* @param PackageInterface|null $prevPackage previous package instance in case of updates
|
2011-09-23 23:09:51 +00:00
|
|
|
*
|
2019-01-17 16:12:33 +00:00
|
|
|
* @return PromiseInterface
|
2013-06-13 00:05:44 +00:00
|
|
|
* @throws \InvalidArgumentException if package have no urls to download from
|
2014-07-16 13:17:38 +00:00
|
|
|
* @throws \RuntimeException
|
2011-09-23 23:09:51 +00:00
|
|
|
*/
|
2019-01-17 16:12:33 +00:00
|
|
|
public function download(PackageInterface $package, $targetDir, PackageInterface $prevPackage = null)
|
2011-09-23 23:09:51 +00:00
|
|
|
{
|
2020-03-12 12:50:18 +00:00
|
|
|
$targetDir = $this->normalizeTargetDir($targetDir);
|
2019-01-17 16:12:33 +00:00
|
|
|
$this->filesystem->ensureDirectoryExists(dirname($targetDir));
|
2011-09-23 23:09:51 +00:00
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
$sources = $this->getAvailableSources($package, $prevPackage);
|
2011-09-28 22:48:17 +00:00
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
$io = $this->io;
|
|
|
|
$self = $this;
|
2012-02-09 17:45:28 +00:00
|
|
|
|
2019-08-29 09:37:23 +00:00
|
|
|
$download = function ($retry = false) use (&$sources, $io, $package, $self, $targetDir, &$download, $prevPackage) {
|
2019-01-17 16:12:33 +00:00
|
|
|
$source = array_shift($sources);
|
|
|
|
if ($retry) {
|
|
|
|
$io->writeError(' <warning>Now trying to download from ' . $source . '</warning>');
|
2014-02-25 12:34:39 +00:00
|
|
|
}
|
2014-02-24 17:40:33 +00:00
|
|
|
$package->setInstallationSource($source);
|
2019-01-17 16:12:33 +00:00
|
|
|
|
|
|
|
$downloader = $self->getDownloaderForPackage($package);
|
|
|
|
if (!$downloader) {
|
|
|
|
return \React\Promise\resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
$handleError = function ($e) use ($sources, $source, $package, $io, $download) {
|
|
|
|
if ($e instanceof \RuntimeException) {
|
|
|
|
if (!$sources) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$io->writeError(
|
|
|
|
' <warning>Failed to download '.
|
|
|
|
$package->getPrettyName().
|
|
|
|
' from ' . $source . ': '.
|
|
|
|
$e->getMessage().'</warning>'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $download(true);
|
2014-02-24 18:52:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
throw $e;
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2019-08-29 09:37:23 +00:00
|
|
|
$result = $downloader->download($package, $targetDir, $prevPackage);
|
2019-01-17 16:12:33 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $handleError($e);
|
|
|
|
}
|
|
|
|
if (!$result instanceof PromiseInterface) {
|
|
|
|
return \React\Promise\resolve($result);
|
2014-02-24 17:40:33 +00:00
|
|
|
}
|
2019-01-17 16:12:33 +00:00
|
|
|
|
|
|
|
$res = $result->then(function ($res) {
|
|
|
|
return $res;
|
|
|
|
}, $handleError);
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
};
|
|
|
|
|
|
|
|
return $download();
|
|
|
|
}
|
|
|
|
|
2019-08-29 09:37:23 +00:00
|
|
|
/**
|
|
|
|
* Prepares an operation execution
|
|
|
|
*
|
|
|
|
* @param string $type one of install/update/uninstall
|
|
|
|
* @param PackageInterface $package package instance
|
|
|
|
* @param string $targetDir target dir
|
|
|
|
* @param PackageInterface|null $prevPackage previous package instance in case of updates
|
|
|
|
*
|
|
|
|
* @return PromiseInterface|null
|
|
|
|
*/
|
|
|
|
public function prepare($type, PackageInterface $package, $targetDir, PackageInterface $prevPackage = null)
|
|
|
|
{
|
2020-03-12 12:50:18 +00:00
|
|
|
$targetDir = $this->normalizeTargetDir($targetDir);
|
2019-08-29 09:37:23 +00:00
|
|
|
$downloader = $this->getDownloaderForPackage($package);
|
|
|
|
if ($downloader) {
|
|
|
|
return $downloader->prepare($type, $package, $targetDir, $prevPackage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
/**
|
|
|
|
* Installs package into target dir.
|
|
|
|
*
|
|
|
|
* @param PackageInterface $package package instance
|
|
|
|
* @param string $targetDir target dir
|
|
|
|
*
|
2019-08-29 09:37:23 +00:00
|
|
|
* @return PromiseInterface|null
|
2019-01-17 16:12:33 +00:00
|
|
|
* @throws \InvalidArgumentException if package have no urls to download from
|
|
|
|
* @throws \RuntimeException
|
|
|
|
*/
|
|
|
|
public function install(PackageInterface $package, $targetDir)
|
|
|
|
{
|
2020-03-12 12:50:18 +00:00
|
|
|
$targetDir = $this->normalizeTargetDir($targetDir);
|
2019-01-17 16:12:33 +00:00
|
|
|
$downloader = $this->getDownloaderForPackage($package);
|
|
|
|
if ($downloader) {
|
2019-08-29 09:37:23 +00:00
|
|
|
return $downloader->install($package, $targetDir);
|
2014-02-06 09:50:06 +00:00
|
|
|
}
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates package from initial to target version.
|
|
|
|
*
|
2012-05-22 10:07:08 +00:00
|
|
|
* @param PackageInterface $initial initial package version
|
|
|
|
* @param PackageInterface $target target package version
|
|
|
|
* @param string $targetDir target dir
|
2011-09-23 23:09:51 +00:00
|
|
|
*
|
2019-08-29 09:37:23 +00:00
|
|
|
* @return PromiseInterface|null
|
2013-06-13 00:05:44 +00:00
|
|
|
* @throws \InvalidArgumentException if initial package is not installed
|
2011-09-23 23:09:51 +00:00
|
|
|
*/
|
2011-09-25 15:30:54 +00:00
|
|
|
public function update(PackageInterface $initial, PackageInterface $target, $targetDir)
|
2011-09-23 23:09:51 +00:00
|
|
|
{
|
2020-03-12 12:50:18 +00:00
|
|
|
$targetDir = $this->normalizeTargetDir($targetDir);
|
2019-01-17 16:12:33 +00:00
|
|
|
$downloader = $this->getDownloaderForPackage($target);
|
|
|
|
$initialDownloader = $this->getDownloaderForPackage($initial);
|
2014-02-06 09:50:06 +00:00
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
// no downloaders present means update from metapackage to metapackage, nothing to do
|
|
|
|
if (!$initialDownloader && !$downloader) {
|
|
|
|
return;
|
2011-09-25 15:30:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
// if we have a downloader present before, but not after, the package became a metapackage and its files should be removed
|
|
|
|
if (!$downloader) {
|
2019-08-29 09:37:23 +00:00
|
|
|
return $initialDownloader->remove($initial, $targetDir);
|
2012-04-10 09:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
$initialType = $this->getDownloaderType($initialDownloader);
|
|
|
|
$targetType = $this->getDownloaderType($downloader);
|
2011-09-25 15:30:54 +00:00
|
|
|
if ($initialType === $targetType) {
|
2016-02-04 01:16:39 +00:00
|
|
|
try {
|
2019-08-29 09:37:23 +00:00
|
|
|
return $downloader->update($initial, $target, $targetDir);
|
2016-02-13 17:43:57 +00:00
|
|
|
} catch (\RuntimeException $e) {
|
|
|
|
if (!$this->io->isInteractive()) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2016-02-13 17:48:31 +00:00
|
|
|
$this->io->writeError('<error> Update failed ('.$e->getMessage().')</error>');
|
2016-02-13 17:43:57 +00:00
|
|
|
if (!$this->io->askConfirmation(' Would you like to try reinstalling the package instead [<comment>yes</comment>]? ', true)) {
|
|
|
|
throw $e;
|
2016-02-04 01:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|
2016-02-13 17:43:57 +00:00
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
// if downloader type changed, or update failed and user asks for reinstall,
|
|
|
|
// we wipe the dir and do a new install instead of updating it
|
2019-08-29 09:37:23 +00:00
|
|
|
$promise = $initialDownloader->remove($initial, $targetDir);
|
|
|
|
if ($promise) {
|
|
|
|
$self = $this;
|
|
|
|
return $promise->then(function ($res) use ($self, $target, $targetDir) {
|
|
|
|
return $self->install($target, $targetDir);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->install($target, $targetDir);
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes package from target dir.
|
|
|
|
*
|
2012-05-22 10:07:08 +00:00
|
|
|
* @param PackageInterface $package package instance
|
|
|
|
* @param string $targetDir target dir
|
2019-08-29 09:37:23 +00:00
|
|
|
*
|
|
|
|
* @return PromiseInterface|null
|
2011-09-23 23:09:51 +00:00
|
|
|
*/
|
2011-09-25 15:30:54 +00:00
|
|
|
public function remove(PackageInterface $package, $targetDir)
|
2011-09-23 23:09:51 +00:00
|
|
|
{
|
2020-03-12 12:50:18 +00:00
|
|
|
$targetDir = $this->normalizeTargetDir($targetDir);
|
2019-01-17 16:12:33 +00:00
|
|
|
$downloader = $this->getDownloaderForPackage($package);
|
2014-02-13 11:48:12 +00:00
|
|
|
if ($downloader) {
|
2019-08-29 09:37:23 +00:00
|
|
|
return $downloader->remove($package, $targetDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up a failed operation
|
|
|
|
*
|
|
|
|
* @param string $type one of install/update/uninstall
|
|
|
|
* @param PackageInterface $package package instance
|
|
|
|
* @param string $targetDir target dir
|
|
|
|
* @param PackageInterface|null $prevPackage previous package instance in case of updates
|
|
|
|
*
|
|
|
|
* @return PromiseInterface|null
|
|
|
|
*/
|
|
|
|
public function cleanup($type, PackageInterface $package, $targetDir, PackageInterface $prevPackage = null)
|
|
|
|
{
|
2020-03-12 12:50:18 +00:00
|
|
|
$targetDir = $this->normalizeTargetDir($targetDir);
|
2019-08-29 09:37:23 +00:00
|
|
|
$downloader = $this->getDownloaderForPackage($package);
|
|
|
|
if ($downloader) {
|
|
|
|
return $downloader->cleanup($type, $package, $targetDir, $prevPackage);
|
2014-02-06 09:50:06 +00:00
|
|
|
}
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|
2015-04-18 22:25:59 +00:00
|
|
|
|
2015-04-19 00:59:51 +00:00
|
|
|
/**
|
|
|
|
* Determines the install preference of a package
|
|
|
|
*
|
|
|
|
* @param PackageInterface $package package instance
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function resolvePackageInstallPreference(PackageInterface $package)
|
2015-04-18 22:25:59 +00:00
|
|
|
{
|
|
|
|
foreach ($this->packagePreferences as $pattern => $preference) {
|
2016-02-25 19:07:09 +00:00
|
|
|
$pattern = '{^'.str_replace('\\*', '.*', preg_quote($pattern)).'$}i';
|
2015-04-18 22:25:59 +00:00
|
|
|
if (preg_match($pattern, $package->getName())) {
|
2016-02-25 19:07:09 +00:00
|
|
|
if ('dist' === $preference || (!$package->isDev() && 'auto' === $preference)) {
|
|
|
|
return 'dist';
|
2015-04-18 22:25:59 +00:00
|
|
|
}
|
2016-02-29 15:04:05 +00:00
|
|
|
|
2016-02-25 19:07:09 +00:00
|
|
|
return 'source';
|
2015-04-18 22:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 19:07:09 +00:00
|
|
|
return $package->isDev() ? 'source' : 'dist';
|
2015-04-18 22:25:59 +00:00
|
|
|
}
|
2019-01-17 16:12:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function getAvailableSources(PackageInterface $package, PackageInterface $prevPackage = null)
|
|
|
|
{
|
|
|
|
$sourceType = $package->getSourceType();
|
|
|
|
$distType = $package->getDistType();
|
|
|
|
|
|
|
|
// add source before dist by default
|
|
|
|
$sources = array();
|
|
|
|
if ($sourceType) {
|
|
|
|
$sources[] = 'source';
|
|
|
|
}
|
|
|
|
if ($distType) {
|
|
|
|
$sources[] = 'dist';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($sources)) {
|
|
|
|
throw new \InvalidArgumentException('Package '.$package.' must have a source or dist specified');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$prevPackage
|
|
|
|
// if we are updating, we want to keep the same source as the previously installed package (if available in the new one)
|
|
|
|
&& in_array($prevPackage->getInstallationSource(), $sources, true)
|
|
|
|
// unless the previous package was stable dist (by default) and the new package is dev, then we allow the new default to take over
|
|
|
|
&& !(!$prevPackage->isDev() && $prevPackage->getInstallationSource() === 'dist' && $package->isDev())
|
|
|
|
) {
|
|
|
|
$prevSource = $prevPackage->getInstallationSource();
|
|
|
|
usort($sources, function ($a, $b) use ($prevSource) {
|
|
|
|
return $a === $prevSource ? -1 : 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return $sources;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse sources in case dist is the preferred source for this package
|
|
|
|
if (!$this->preferSource && ($this->preferDist || 'dist' === $this->resolvePackageInstallPreference($package))) {
|
|
|
|
$sources = array_reverse($sources);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sources;
|
|
|
|
}
|
2020-03-12 12:50:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloaders expect a /path/to/dir without trailing slash
|
|
|
|
*
|
|
|
|
* If any Installer provides a path with a trailing slash, this can cause bugs so make sure we remove them
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function normalizeTargetDir($dir)
|
|
|
|
{
|
|
|
|
if ($dir === '\\' || $dir === '/') {
|
|
|
|
return $dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtrim($dir, '\\/');
|
|
|
|
}
|
2011-09-23 23:09:51 +00:00
|
|
|
}
|