Skip to content

Commit

Permalink
fixes issue narrowspark#96
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed May 6, 2019
1 parent aa853e0 commit e4cfa2f
Show file tree
Hide file tree
Showing 27 changed files with 192 additions and 150 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
],
"require": {
"php": "^7.1",
"ext-mbstring": "*",
"ext-json": "*",
"ext-tokenizer": "*",
"composer-plugin-api": "^1.0"
Expand Down
121 changes: 4 additions & 117 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
use Composer\Console\Application;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\DependencyResolver\Pool;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Factory;
use Composer\Installer;
use Composer\Installer\InstallerEvent;
use Composer\Installer\InstallerEvents;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
Expand All @@ -24,10 +22,7 @@
use Composer\Package\Locker;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\PreFileDownloadEvent;
use Composer\Repository\ComposerRepository as BaseComposerRepository;
use Composer\Repository\RepositoryFactory;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositoryManager;
use Composer\Script\Event;
use Composer\Script\ScriptEvents as ComposerScriptEvents;
Expand All @@ -45,7 +40,7 @@
use Narrowspark\Automatic\Operation\Install;
use Narrowspark\Automatic\Operation\Uninstall;
use Narrowspark\Automatic\Prefetcher\ParallelDownloader;
use Narrowspark\Automatic\Prefetcher\Prefetcher;
use Narrowspark\Automatic\Prefetcher\PrefetcherTrait;
use Narrowspark\Automatic\Prefetcher\TruncatedComposerRepository;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
Expand All @@ -57,6 +52,7 @@ class Automatic implements PluginInterface, EventSubscriberInterface
{
use ExpandTargetDirTrait;
use GetGenericPropertyReaderTrait;
use PrefetcherTrait;

/**
* @var string
Expand Down Expand Up @@ -201,7 +197,7 @@ public function activate(Composer $composer, IOInterface $io): void
// that way, we are sure to use all files from the same version.
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(\dirname(__DIR__, 1), FilesystemIterator::SKIP_DOTS)) as $file) {
/** @var \SplFileInfo $file */
if (\mb_substr($file->getFilename(), -4) === '.php') {
if (\substr($file->getFilename(), -4) === '.php') {
require_once $file;
}
}
Expand Down Expand Up @@ -689,115 +685,6 @@ public function executeAutoScripts(Event $event): void
}
}

/**
* Populate the provider cache.
*
* @param \Composer\Installer\InstallerEvent $event
*
* @return void
*/
public function onPreDependenciesSolving(InstallerEvent $event): void
{
$listed = [];
$packages = [];
$pool = $event->getPool();
$pool = \Closure::bind(function () {
foreach ($this->providerRepos as $k => $repo) {
$this->providerRepos[$k] = new class($repo) extends BaseComposerRepository {
/**
* A repository implementation.
*
* @var \Composer\Repository\RepositoryInterface
*/
private $repo;

/**
* @param \Composer\Repository\RepositoryInterface $repo
*/
public function __construct(RepositoryInterface $repo)
{
$this->repo = $repo;
}

/**
* {@inheritdoc}
*/
public function whatProvides(Pool $pool, $name, $bypassFilters = false)
{
$packages = [];

if (! \method_exists($this->repo, 'whatProvides')) {
return $packages;
}

foreach ($this->repo->whatProvides($pool, $name, $bypassFilters) as $k => $p) {
$packages[$k] = clone $p;
}

return $packages;
}
};
}

return $this;
}, clone $pool, $pool)();

foreach ($event->getRequest()->getJobs() as $job) {
if ($job['cmd'] !== 'install' || \mb_strpos($job['packageName'], '/') === false) {
continue;
}

$listed[$job['packageName']] = true;
$packages[] = [$job['packageName'], $job['constraint']];
}

$this->container->get(ParallelDownloader::class)->download($packages, function ($packageName, $constraint) use (&$listed, &$packages, $pool): void {
/** @var \Composer\Package\PackageInterface $package */
foreach ($pool->whatProvides($packageName, $constraint, true) as $package) {
/** @var \Composer\Package\Link $link */
foreach (\array_merge($package->getRequires(), $package->getConflicts(), $package->getReplaces()) as $link) {
if (isset($listed[$link->getTarget()]) || \mb_strpos($link->getTarget(), '/') === false) {
continue;
}

$listed[$link->getTarget()] = true;
$packages[] = [$link->getTarget(), $link->getConstraint()];
}
}
});
}

/**
* Wrapper for the fetchAllFromOperations function.
*
* @see \Narrowspark\Automatic\Prefetcher\Prefetcher::fetchAllFromOperations()
*
* @param \Composer\Installer\InstallerEvent|\Composer\Installer\PackageEvent $event
*
* @return void
*/
public function populateFilesCacheDir($event): void
{
$this->container->get(Prefetcher::class)->fetchAllFromOperations($event);
}

/**
* Adds the parallel downloader to composer.
*
* @param \Composer\Plugin\PreFileDownloadEvent $event
*
* @return void
*/
public function onFileDownload(PreFileDownloadEvent $event): void
{
/** @var \Narrowspark\Automatic\Prefetcher\ParallelDownloader $rfs */
$rfs = $this->container->get(ParallelDownloader::class);

if ($event->getRemoteFilesystem() !== $rfs) {
$event->setRemoteFilesystem($rfs->setNextOptions($event->getRemoteFilesystem()->getOptions()));
}
}

/**
* Check if package is in require-dev.
* When Composer runs with --no-dev, ignore uninstall operations on packages from require-dev.
Expand Down Expand Up @@ -838,7 +725,7 @@ private function addLegacyTags(IOInterface $io, array $requires, LegacyTagsManag
continue;
}

if (\mb_strpos($name, '/') === false) {
if (\strpos($name, '/') === false) {
$io->writeError(\sprintf('Constrain [%s] skipped, package name [%s] without a slash is not supported', $version, $name));

continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Automatic/Configurator/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function configure(PackageContract $package): void
$data = '';

foreach ((array) $package->getConfig(ConfiguratorContract::TYPE, self::getName()) as $key => $value) {
if (\mb_strpos($key, '#') === 0 && \is_numeric(\mb_substr($key, 1))) {
if (\strpos($key, '#') === 0 && \is_numeric(\substr($key, 1))) {
$data .= '# ' . $value . \PHP_EOL;

continue;
Expand Down
6 changes: 4 additions & 2 deletions src/Automatic/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ public function __construct(Composer $composer, IOInterface $io)
$container->get(IOInterface::class),
$container->get(Composer::class),
$container->get(Lock::class),
$container->get(ClassFinder::class)
$container->get(ClassFinder::class),
$container
);
},
SkeletonInstaller::class => static function (ContainerContract $container) {
return new SkeletonInstaller(
$container->get(IOInterface::class),
$container->get(Composer::class),
$container->get(Lock::class),
$container->get(ClassFinder::class)
$container->get(ClassFinder::class),
$container
);
},
ConfiguratorContract::class => static function (ContainerContract $container) {
Expand Down
39 changes: 37 additions & 2 deletions src/Automatic/Installer/AbstractInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
namespace Narrowspark\Automatic\Installer;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\InstallerEvents;
use Composer\Installer\LibraryInstaller;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginEvents;
use Composer\Repository\InstalledRepositoryInterface;
use Narrowspark\Automatic\Automatic;
use Narrowspark\Automatic\Common\ClassFinder;
use Narrowspark\Automatic\Common\Contract\Exception\UnexpectedValueException;
use Narrowspark\Automatic\Lock;
use Narrowspark\Automatic\Prefetcher\PrefetcherTrait;
use Narrowspark\Automatic\Contract\Container as ContainerContract;

abstract class AbstractInstaller extends LibraryInstaller
abstract class AbstractInstaller extends LibraryInstaller implements EventSubscriberInterface
{
use PrefetcherTrait;

/**
* @var string
*/
Expand All @@ -38,16 +46,28 @@ abstract class AbstractInstaller extends LibraryInstaller
*/
protected $loader;

/**
* A Container instance.
*
* @var \Narrowspark\Automatic\Contract\Container
*/
protected $container;

/**
* Create a new Installer instance.
*
* @param \Composer\IO\IOInterface $io
* @param \Composer\Composer $composer
* @param \Narrowspark\Automatic\Lock $lock
* @param \Narrowspark\Automatic\Common\ClassFinder $loader
* @param \Narrowspark\Automatic\Contract\Container $container
*/
public function __construct(IOInterface $io, Composer $composer, Lock $lock, ClassFinder $loader)
public function __construct(IOInterface $io, Composer $composer, Lock $lock, ClassFinder $loader, ContainerContract $container)
{
$this->container = $container;

$composer->getEventDispatcher()->addSubscriber($this);

parent::__construct($io, $composer, static::TYPE);

$this->lock = $lock;
Expand Down Expand Up @@ -115,6 +135,21 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $
$this->lock->remove(Automatic::LOCK_CLASSMAP, $package->getName());
}

/**
* @return array|void
*/
public static function getSubscribedEvents(): array
{
return [
InstallerEvents::PRE_DEPENDENCIES_SOLVING => [['onPreDependenciesSolving', \PHP_INT_MAX]],
InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir', \PHP_INT_MAX]],
PackageEvents::PRE_PACKAGE_INSTALL => [['populateFilesCacheDir', ~\PHP_INT_MAX]],
PackageEvents::PRE_PACKAGE_UPDATE => [['populateFilesCacheDir', ~\PHP_INT_MAX]],
PluginEvents::PRE_FILE_DOWNLOAD => 'onFileDownload',

];
}

/**
* Finds all class in given namespace.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Automatic/Installer/InstallationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function install(array $requires, array $devRequires = []): void
$rootPackages = [];

foreach ($this->getRootRequires() as $link) {
$rootPackages[\mb_strtolower($link->getTarget())] = (string) $link->getConstraint();
$rootPackages[\strtolower($link->getTarget())] = (string) $link->getConstraint();
}

$requiresToInstall = $this->preparePackagesToInstall($requires, $rootPackages);
Expand Down Expand Up @@ -124,7 +124,7 @@ protected function preparePackagesToInstall(array $requires, array $rootPackages
// Check if package is currently installed, if so, use installed constraint.
if (isset($toInstall[$packageName])) {
$version = $toInstall[$packageName];
$constraint = \mb_strpos($version, 'dev') === false ? '^' . $version : $version;
$constraint = \strpos($version, 'dev') === false ? '^' . $version : $version;

$toInstall[$packageName] = $constraint;

Expand Down
2 changes: 1 addition & 1 deletion src/Automatic/LegacyTagsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function hasProvider(string $file): bool
foreach ($this->legacyTags as $name => $constraint) {
[$namespace, $packageName] = \explode('/', $name, 2);

if (\mb_strpos($file, \sprintf('provider-%s$', $namespace)) !== false) {
if (\strpos($file, \sprintf('provider-%s$', $namespace)) !== false) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Automatic/Operation/AbstractOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected function findClassesInAutomaticFolder(PackageContract $package, string
if (\count($composerAutoload) !== 0) {
$classes = $this->classFinder->setComposerAutoload($name, $composerAutoload)
->setFilter(function (\SplFileInfo $fileInfo) use ($name) {
return \mb_strpos((string) \mb_strstr($fileInfo->getPathname(), $name), \DIRECTORY_SEPARATOR . 'Automatic' . \DIRECTORY_SEPARATOR) !== false;
return \strpos((string) \strstr($fileInfo->getPathname(), $name), \DIRECTORY_SEPARATOR . 'Automatic' . \DIRECTORY_SEPARATOR) !== false;
})
->find()
->getAll();
Expand Down
4 changes: 2 additions & 2 deletions src/Automatic/Operation/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function getPackageVersion(PackageInterface $package): string
$version = $package->getPrettyVersion();
$extra = $package->getExtra();

if (isset($extra['branch-alias']) && \mb_strpos($version, 'dev-') === 0) {
if (isset($extra['branch-alias']) && \strpos($version, 'dev-') === 0) {
$branchAliases = $extra['branch-alias'];

if (
Expand Down Expand Up @@ -131,7 +131,7 @@ private function createAutomaticPackage(PackageInterface $composerPackage, strin
foreach ($composerPackage->getRequires() as $link) {
$target = $link->getTarget();

if ($target === 'php' || \mb_strpos($target, 'ext-') === 0) {
if ($target === 'php' || \strpos($target, 'ext-') === 0) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Automatic/Prefetcher/CurlDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function get(string $url, $context, ?string $file): array
} else {
$headers[] = 'Connection: keep-alive';

if (\mb_strpos($url, 'https://') === 0 && \defined('CURL_VERSION_HTTP2') && \defined('CURL_HTTP_VERSION_2_0') && (\CURL_VERSION_HTTP2 & \curl_version()['features'])) {
if (\strpos($url, 'https://') === 0 && \defined('CURL_VERSION_HTTP2') && \defined('CURL_HTTP_VERSION_2_0') && (\CURL_VERSION_HTTP2 & \curl_version()['features'])) {
\curl_setopt($ch, \CURLOPT_HTTP_VERSION, \CURL_HTTP_VERSION_2_0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Automatic/Prefetcher/ParallelDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function download(array &$nextArgs, callable $nextCallback, bool $quiet =
$this->io->writeError('<warning>Enable the "cURL" PHP extension for faster downloads</warning>');
}

$note = \DIRECTORY_SEPARATOR === '\\' ? '' : (\mb_stripos(\PHP_OS, 'darwin') !== false ? '🎵' : '🎶');
$note = \DIRECTORY_SEPARATOR === '\\' ? '' : (\stripos(\PHP_OS, 'darwin') !== false ? '🎵' : '🎶');
$note .= $this->downloader !== null ? (\DIRECTORY_SEPARATOR !== '\\' ? ' 💨' : '') : '';

$this->io->writeError('');
Expand Down
2 changes: 1 addition & 1 deletion src/Automatic/Prefetcher/Prefetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function prefetchComposerRepositories(RemoteFilesystem $remoteFilesystem)

if ($pluginManager instanceof PluginManager) {
foreach ($pluginManager->getPlugins() as $plugin) {
if (\mb_strpos(\get_class($plugin), PrestissimoPlugin::class) === 0) {
if (\strpos(\get_class($plugin), PrestissimoPlugin::class) === 0) {
if (\method_exists($remoteFilesystem, 'getRemoteContents')) {
$plugin->disable();
} else {
Expand Down
Loading

0 comments on commit e4cfa2f

Please sign in to comment.