Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
moved Fixtures to Fixture
Browse files Browse the repository at this point in the history
refactored some classes and added const
added tests and fixed some
cs fixes and phpstan fixes
  • Loading branch information
prisis committed Aug 8, 2018
1 parent 34d7adb commit dc3db53
Show file tree
Hide file tree
Showing 49 changed files with 602 additions and 416 deletions.
101 changes: 51 additions & 50 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
use Composer\Script\ScriptEvents;
use FilesystemIterator;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
use Narrowspark\Automatic\Common\Package;
use Narrowspark\Automatic\Common\Traits\ExpandTargetDirTrait;
use Narrowspark\Automatic\Common\Traits\GetGenericPropertyReaderTrait;
use Narrowspark\Automatic\Common\Util;
use Narrowspark\Automatic\Installer\ConfiguratorInstaller;
use Narrowspark\Automatic\Installer\InstallationManager;
use Narrowspark\Automatic\Installer\QuestionInstallationManager;
use Narrowspark\Automatic\Installer\SkeletonInstaller;
use Narrowspark\Automatic\Prefetcher\ParallelDownloader;
Expand All @@ -54,6 +54,11 @@ class Automatic implements PluginInterface, EventSubscriberInterface
*/
public const LOCK_CLASSMAP = 'classmap';

/**
* @var string
*/
public const LOCK_PACKAGES = 'packages';

/**
* @var string
*/
Expand Down Expand Up @@ -87,13 +92,6 @@ class Automatic implements PluginInterface, EventSubscriberInterface
*/
private $operations = [];

/**
* The composer skeletons.
*
* @var array
*/
private $skeletons = [];

/**
* @var array
*/
Expand Down Expand Up @@ -149,8 +147,6 @@ public function activate(Composer $composer, IOInterface $io): void

$this->container = new Container($composer, $io);

$this->container->get(Lock::class)->add(self::LOCK_CLASSMAP, []);

/** @var \Composer\Installer\InstallationManager $installationManager */
$installationManager = $this->container->get(Composer::class)->getInstallationManager();
$installationManager->addInstaller($this->container->get(ConfiguratorInstaller::class));
Expand Down Expand Up @@ -213,8 +209,6 @@ public function record(PackageEvent $event): void

if ($operation instanceof InstallOperation && $operation->getPackage()->getName() === self::PACKAGE_NAME) {
\array_unshift($this->operations, $operation);
} elseif ($operation instanceof InstallOperation && $operation->getPackage()->getType() === SkeletonInstaller::TYPE) {
$this->skeletons[] = $operation;
} else {
$this->operations[] = $operation;
}
Expand Down Expand Up @@ -255,21 +249,14 @@ public function onPostCreateProject(Event $event): void
$lock->read();

if ($lock->has(SkeletonInstaller::LOCK_KEY) && $io->isInteractive()) {
$skeletonGenerator = new SkeletonGenerator(
$io,
$this->container->get(InstallationManager::class),
$lock,
$this->container->get('vendorPath'),
$this->skeletons,
$this->container->get('composer-extra')
);
/** @var \Narrowspark\Automatic\SkeletonGenerator $skeletonGenerator */
$skeletonGenerator = $this->container->get(SkeletonGenerator::class);

$skeletonGenerator->run();

$skeletonGenerator->remove();
}

$lock->clear();

/** @var \Composer\Json\JsonFile $json */
/** @var \Composer\Json\JsonManipulator $manipulator */
[$json, $manipulator] = Util::getComposerJsonFileAndManipulator();
Expand Down Expand Up @@ -334,12 +321,11 @@ public function onPostUpdate(Event $event, array $operations = []): void
\count($packages) > 1 ? 's' : ''
));

$configurators = (array) $lock->get(ConfiguratorInstaller::LOCK_KEY);
$configuratorsClassmap = (array) $lock->get(self::LOCK_CLASSMAP);

foreach ($configurators as $packageName => $classList) {
foreach ((array) $lock->get(ConfiguratorInstaller::LOCK_KEY) as $packageName => $classList) {
foreach ($configuratorsClassmap[$packageName] as $path) {
require_once \str_replace('%vendor_path%', $this->container->get('vendorPath'), $path);
includeFile(\str_replace('%vendor_path%', $this->container->get('vendor-dir'), $path));
}

foreach ($classList as $class) {
Expand All @@ -353,15 +339,17 @@ public function onPostUpdate(Event $event, array $operations = []): void

/** @var \Narrowspark\Automatic\Common\Contract\Package $package */
foreach ($packages as $package) {
if (isset($automaticOptions['dont-discover']) && \array_key_exists($package->getName(), $automaticOptions['dont-discover'])) {
$io->write(\sprintf('<info>Package "%s" was ignored.</info>', $package->getName()));
$prettyName = $package->getPrettyName();

if (isset($automaticOptions['dont-discover']) && \array_key_exists($prettyName, $automaticOptions['dont-discover'])) {
$io->write(\sprintf('<info>Package "%s" was ignored.</info>', $prettyName));

return;
}

if ($allowInstall === false && $package->getOperation() === 'install') {
if ($allowInstall === false && $package->getOperation() === PackageContract::INSTALL_OPERATION) {
$answer = $io->askAndValidate(
QuestionFactory::getPackageQuestion($package->getUrl()),
QuestionFactory::getPackageQuestion($prettyName, $package->getUrl()),
[QuestionFactory::class, 'validatePackageQuestionAnswer'],
null,
'n'
Expand Down Expand Up @@ -622,7 +610,7 @@ private function doActionOnPackageOperation(PackageContract $package): void
$packageConfigurator = $this->container->get(PackageConfigurator::class);

if ($package->hasConfig(PackageConfigurator::TYPE)) {
foreach ($package->getConfig(PackageConfigurator::TYPE) as $name => $configurator) {
foreach ((array) $package->getConfig(PackageConfigurator::TYPE) as $name => $configurator) {
$packageConfigurator->add($name, $configurator);
}
}
Expand Down Expand Up @@ -651,30 +639,33 @@ private function doInstall(PackageContract $package, PackageConfigurator $packag
$this->container->get(Configurator::class)->configure($package);
$packageConfigurator->configure($package);

$options = $package->getConfigs();
$questionInstallationManager = $this->container->get(QuestionInstallationManager::class);

if ($package->hasConfig('extra-dependency')) {
$extraDependency = $package->getConfig('extra-dependency');
$options = \array_merge(
$options,
['selected-question-packages' => $questionInstallationManager->getPackagesToInstall()]
);

foreach ($questionInstallationManager->install($package, $extraDependency) as $operation) {
if ($package->hasConfig(QuestionInstallationManager::TYPE)) {
foreach ($questionInstallationManager->install($package, $package->getConfig(QuestionInstallationManager::TYPE)) as $operation) {
$this->doInstall($operation, $packageConfigurator);
}

$package->setSelectedQuestionableRequirements($questionInstallationManager->getPackagesToInstall());
}

if ($package->hasConfig('post-install-output')) {
foreach ($package->getConfig('post-install-output') as $line) {
foreach ((array) $package->getConfig('post-install-output') as $line) {
$this->postInstallOutput[] = self::expandTargetDir($this->container->get('composer-extra'), $line);
}

$this->postInstallOutput[] = '';
}

$this->container->get(Lock::class)->add($package->getName(), $options);
$lock = $this->container->get(Lock::class);

$lock->add(
self::LOCK_PACKAGES,
\array_merge(
(array) $lock->get(self::LOCK_PACKAGES),
[$package->getName() => $package->toArray()]
)
);
}

/**
Expand All @@ -697,20 +688,18 @@ private function doUninstall(PackageContract $package, PackageConfigurator $pack
/** @var \Narrowspark\Automatic\Lock $lock */
$lock = $this->container->get(Lock::class);

if ($package->hasConfig('extra-dependency')) {
$extraDependencies = [];
if ($package->hasConfig(QuestionInstallationManager::TYPE)) {
$questionableRequirements = [];

foreach ($lock->read() as $packageName => $data) {
if (isset($data['extraDependencyOf']) && $data['extraDependencyOf'] === $package->getName()) {
$extraDependencies[$packageName] = $data['version'];
foreach ((array) $lock->get(self::LOCK_PACKAGES) as $packageName => $data) {
$lockPackage = Package::createFromLock($packageName, $data);

foreach ((array) $data['require'] as $name => $version) {
$extraDependencies[$name] = $version;
}
if ($lockPackage->isQuestionableRequirement() && $lockPackage->getParentName() === $package->getName()) {
$questionableRequirements[] = $lockPackage;
}
}

foreach ($this->container->get(QuestionInstallationManager::class)->uninstall($package, $extraDependencies) as $operation) {
foreach ($this->container->get(QuestionInstallationManager::class)->uninstall($package, $questionableRequirements) as $operation) {
$this->doUninstall($operation, $packageConfigurator);
}
}
Expand Down Expand Up @@ -740,3 +729,15 @@ private function getErrorMessage(): ?string
return null;
}
}

/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*
* @param mixed $file
*/
function includeFile($file)
{
include $file;
}
4 changes: 2 additions & 2 deletions src/Automatic/Configurator/ComposerScriptsConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function configure(PackageContract $package): void
{
$autoScripts = $this->getComposerContentAndAutoScripts();

$autoScripts = \array_merge($autoScripts, $package->getConfig('composer-scripts'));
$autoScripts = \array_merge($autoScripts, (array) $package->getConfig(self::getName()));

$this->manipulateAndWrite($autoScripts);
}
Expand All @@ -64,7 +64,7 @@ public function unconfigure(PackageContract $package): void
{
$autoScripts = $this->getComposerContentAndAutoScripts();

foreach (\array_keys($package->getConfig('composer-scripts')) as $cmd) {
foreach (\array_keys((array) $package->getConfig(self::getName())) as $cmd) {
unset($autoScripts[$cmd]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Automatic/Configurator/CopyFromPackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function configure(PackageContract $package): void
{
$this->write('Copying files');

foreach ($package->getConfig('copy') as $from => $to) {
foreach ((array) $package->getConfig(self::getName()) as $from => $to) {
$target = self::expandTargetDir($this->options, $to);

try {
Expand All @@ -50,7 +50,7 @@ public function unconfigure(PackageContract $package): void
{
$this->write('Removing files');

foreach ($package->getConfig('copy') as $source) {
foreach ((array) $package->getConfig(self::getName()) as $source) {
$source = self::expandTargetDir($this->options, $source);

try {
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 @@ -30,7 +30,7 @@ public function configure(PackageContract $package): void

$data = '';

foreach ($package->getConfig('env') as $key => $value) {
foreach ((array) $package->getConfig(self::getName()) as $key => $value) {
if ($key[0] === '#' && \is_numeric(\mb_substr($key, 1))) {
$data .= '# ' . $value . "\n";

Expand Down
4 changes: 2 additions & 2 deletions src/Automatic/Configurator/GitIgnoreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function configure(PackageContract $package): void

$data = '';

foreach ($package->getConfig('gitignore') as $value) {
foreach ((array) $package->getConfig(self::getName()) as $value) {
$value = self::expandTargetDir($this->options, $value);
$data .= "${value}\n";
}
Expand All @@ -45,7 +45,7 @@ public function unconfigure(PackageContract $package): void
{
$file = getcwd() . '/.gitignore';

// @codeCoverageIgnoreStart
/** @codeCoverageIgnoreStart */
if (! \file_exists($file)) {
return;
}
Expand Down
19 changes: 15 additions & 4 deletions src/Automatic/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ final class Container
/**
* The array of closures defining each entry of the container.
*
* @var array<string, Closure>
* @var array<string, \Closure>
*/
private $callbacks = [];
private $callbacks;

/**
* The array of entries once they have been instantiated.
Expand All @@ -48,6 +48,8 @@ final class Container
*/
public function __construct(Composer $composer, IOInterface $io)
{
$genericPropertyReader = $this->getGenericPropertyReader();

$this->callbacks = [
Composer::class => static function () use ($composer) {
return $composer;
Expand All @@ -69,8 +71,8 @@ public function __construct(Composer $composer, IOInterface $io)
$container->get(Composer::class)->getPackage()->getExtra()
);
},
InputInterface::class => static function (Container $container) {
return $this->getGenericPropertyReader()($container->get(IOInterface::class), 'input');
InputInterface::class => static function (Container $container) use ($genericPropertyReader) {
return $genericPropertyReader($container->get(IOInterface::class), 'input');
},
Lock::class => static function () {
return new Lock(Util::getAutomaticLockFile());
Expand Down Expand Up @@ -158,6 +160,15 @@ public function __construct(Composer $composer, IOInterface $io)
$container->get('composer-extra')
);
},
SkeletonGenerator::class => static function (Container $container) {
return new SkeletonGenerator(
$container->get(IOInterface::class),
$container->get(InstallationManager::class),
$container->get(Lock::class),
$container->get('vendor-dir'),
$container->get('composer-extra')
);
},
];
}

Expand Down
29 changes: 23 additions & 6 deletions src/Automatic/Installer/AbstractInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $

$this->removeFromLock($package, static::LOCK_KEY);

$lockKeyClassmapArray = (array) $this->lock->get(Automatic::LOCK_CLASSMAP);
$name = $package->getName();
$lockClassmap = (array) $this->lock->get(Automatic::LOCK_CLASSMAP);
$name = $package->getName();

if (isset($lockKeyClassmapArray[$name])) {
unset($lockKeyClassmapArray[$name]);
if (isset($lockClassmap[$name])) {
unset($lockClassmap[$name]);
}

$this->lock->add(Automatic::LOCK_CLASSMAP, $lockKeyClassmapArray);
$this->lock->add(Automatic::LOCK_CLASSMAP, $lockClassmap);
}

/**
Expand Down Expand Up @@ -158,7 +158,24 @@ protected function findClasses(array $autoload, PackageInterface $package): ?arr
*
* @return bool FALSE if saving to lock failed, TRUE if anything is alright
*/
abstract protected function saveToLockFile(array $autoload, PackageInterface $package, string $key): bool;
protected function saveToLockFile(array $autoload, PackageInterface $package, string $key): bool
{
$classes = $this->findClasses($autoload, $package);

if ($classes === null) {
return false;
}

$this->lock->add(
$key,
\array_merge(
(array) $this->lock->get($key),
[$package->getName() => $classes]
)
);

return true;
}

/**
* Remove values from the automatic lock file.
Expand Down
Loading

0 comments on commit dc3db53

Please sign in to comment.