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

Commit

Permalink
added reset call if configurator was called in onPostAutoloadDump (#85)
Browse files Browse the repository at this point in the history
| Q               | A
| --------------- | ---
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | -
| License         | MIT
| Doc PR          | -
  • Loading branch information
prisis committed Sep 29, 2018
1 parent f61d4b1 commit 25f231c
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 65 deletions.
30 changes: 24 additions & 6 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Narrowspark\Automatic\Common\Traits\ExpandTargetDirTrait;
use Narrowspark\Automatic\Common\Traits\GetGenericPropertyReaderTrait;
use Narrowspark\Automatic\Common\Util;
use Narrowspark\Automatic\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Contract\Container as ContainerContract;
use Narrowspark\Automatic\Installer\ConfiguratorInstaller;
use Narrowspark\Automatic\Installer\InstallationManager;
Expand Down Expand Up @@ -96,6 +97,13 @@ class Automatic implements PluginInterface, EventSubscriberInterface
*/
private static $activated = true;

/**
* Check if the Configurators a loaded.
*
* @var bool
*/
private static $configuratorsLoaded = false;

/**
* Check if composer.lock should be updated.
*
Expand Down Expand Up @@ -470,16 +478,24 @@ public function onPostUninstall(PackageEvent $event): void
*
* Load configurators from "automatic-configurator".
*
* @param \Composer\Script\Event $event
*
* @throws \ReflectionException
*
* @return void
*/
public function onPostAutoloadDump(): void
public function onPostAutoloadDump(Event $event): void
{
$lock = $this->container->get(Lock::class);
$vendorDir = $this->container->get('vendor-dir');
$configurator = $this->container->get(Configurator::class);
$classMap = (array) $lock->get(self::LOCK_CLASSMAP);
/** @var \Narrowspark\Automatic\Configurator $configurator */
$configurator = $this->container->get(ConfiguratorContract::class);

if (self::$configuratorsLoaded === true) {
$configurator->reset();
}

$lock = $this->container->get(Lock::class);
$vendorDir = $this->container->get('vendor-dir');
$classMap = (array) $lock->get(self::LOCK_CLASSMAP);

foreach ((array) $lock->get(ConfiguratorInstaller::LOCK_KEY) as $packageName => $classList) {
foreach ($classMap[$packageName] as $class => $path) {
Expand All @@ -497,6 +513,8 @@ public function onPostAutoloadDump(): void
}
}
}

self::$configuratorsLoaded = true;
}

/**
Expand Down Expand Up @@ -658,7 +676,7 @@ public function executeAutoScripts(Event $event): void
$reflectionClass = new ReflectionClass($class);

if ($reflectionClass->isInstantiable() && $reflectionClass->hasMethod('getType')) {
$scriptExecutor->addExtender($class::getType(), $class);
$scriptExecutor->add($class::getType(), $class);
}
}
}
Expand Down
30 changes: 16 additions & 14 deletions src/Automatic/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use Narrowspark\Automatic\Common\ClassFinder;
use Narrowspark\Automatic\Common\ScriptExtender\PhpScriptExtender;
use Narrowspark\Automatic\Common\Traits\GetGenericPropertyReaderTrait;
use Narrowspark\Automatic\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Contract\Container as ContainerContract;
use Narrowspark\Automatic\Contract\Exception\InvalidArgumentException;
use Narrowspark\Automatic\Contract\PackageConfigurator as PackageConfiguratorContract;
use Narrowspark\Automatic\Installer\ConfiguratorInstaller;
use Narrowspark\Automatic\Installer\SkeletonInstaller;
use Narrowspark\Automatic\Operation\Install;
Expand Down Expand Up @@ -102,20 +104,27 @@ public function __construct(Composer $composer, IOInterface $io)
$container->get(ClassFinder::class)
);
},
Configurator::class => static function (ContainerContract $container) {
ConfiguratorContract::class => static function (ContainerContract $container) {
return new Configurator(
$container->get(Composer::class),
$container->get(IOInterface::class),
$container->get('composer-extra')
);
},
PackageConfiguratorContract::class => static function (ContainerContract $container) {
return new PackageConfigurator(
$container->get(Composer::class),
$container->get(IOInterface::class),
$container->get('composer-extra')
);
},
Install::class => static function (ContainerContract $container) {
return new Install(
$container->get('vendor-dir'),
$container->get(Lock::class),
$container->get(IOInterface::class),
$container->get(Configurator::class),
$container->get(PackageConfigurator::class),
$container->get(ConfiguratorContract::class),
$container->get(PackageConfiguratorContract::class),
$container->get(ClassFinder::class)
);
},
Expand All @@ -124,8 +133,8 @@ public function __construct(Composer $composer, IOInterface $io)
$container->get('vendor-dir'),
$container->get(Lock::class),
$container->get(IOInterface::class),
$container->get(Configurator::class),
$container->get(PackageConfigurator::class),
$container->get(ConfiguratorContract::class),
$container->get(PackageConfiguratorContract::class),
$container->get(ClassFinder::class)
);
},
Expand Down Expand Up @@ -161,18 +170,11 @@ public function __construct(Composer $composer, IOInterface $io)
$container->get('composer-extra')
);

$scriptExecutor->addExtender(ScriptExtender::getType(), ScriptExtender::class);
$scriptExecutor->addExtender(PhpScriptExtender::getType(), PhpScriptExtender::class);
$scriptExecutor->add(ScriptExtender::getType(), ScriptExtender::class);
$scriptExecutor->add(PhpScriptExtender::getType(), PhpScriptExtender::class);

return $scriptExecutor;
},
PackageConfigurator::class => static function (ContainerContract $container) {
return new PackageConfigurator(
$container->get(Composer::class),
$container->get(IOInterface::class),
$container->get('composer-extra')
);
},
LegacyTagsManager::class => static function (ContainerContract $container) {
return new LegacyTagsManager($container->get(IOInterface::class));
},
Expand Down
11 changes: 11 additions & 0 deletions src/Automatic/Contract/PackageConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Narrowspark\Automatic\Contract;

interface PackageConfigurator extends Configurator
{
/**
* @var string
*/
public const TYPE = 'custom-configurators';
}
42 changes: 21 additions & 21 deletions src/Automatic/Operation/AbstractOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use Composer\IO\IOInterface;
use Narrowspark\Automatic\Common\ClassFinder;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Configurator as CommonConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
use Narrowspark\Automatic\Configurator;
use Narrowspark\Automatic\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Contract\Operation as OperationContract;
use Narrowspark\Automatic\Contract\PackageConfigurator as PackageConfiguratorContract;
use Narrowspark\Automatic\Lock;
use Narrowspark\Automatic\PackageConfigurator;

abstract class AbstractOperation implements OperationContract
{
Expand Down Expand Up @@ -37,14 +37,14 @@ abstract class AbstractOperation implements OperationContract
/**
* A configurator instance.
*
* @var \Narrowspark\Automatic\Configurator
* @var \Narrowspark\Automatic\Contract\Configurator
*/
protected $configurator;

/**
* A package configurator instance.
*
* @var \Narrowspark\Automatic\PackageConfigurator
* @var \Narrowspark\Automatic\Contract\PackageConfigurator
*/
protected $packageConfigurator;

Expand All @@ -58,19 +58,19 @@ abstract class AbstractOperation implements OperationContract
/**
* Base functions for Install and Uninstall Operation.
*
* @param string $vendorDir
* @param \Narrowspark\Automatic\Lock $lock
* @param \Composer\IO\IOInterface $io
* @param \Narrowspark\Automatic\Configurator $configurator
* @param \Narrowspark\Automatic\PackageConfigurator $packageConfigurator
* @param \Narrowspark\Automatic\Common\ClassFinder $classFinder
* @param string $vendorDir
* @param \Narrowspark\Automatic\Lock $lock
* @param \Composer\IO\IOInterface $io
* @param \Narrowspark\Automatic\Contract\Configurator $configurator
* @param \Narrowspark\Automatic\Contract\PackageConfigurator $packageConfigurator
* @param \Narrowspark\Automatic\Common\ClassFinder $classFinder
*/
public function __construct(
string $vendorDir,
Lock $lock,
IOInterface $io,
Configurator $configurator,
PackageConfigurator $packageConfigurator,
ConfiguratorContract $configurator,
PackageConfiguratorContract $packageConfigurator,
ClassFinder $classFinder
) {
$this->vendorDir = $vendorDir;
Expand All @@ -84,18 +84,18 @@ public function __construct(
/**
* Show a waring if remaining configurators are found in package config.
*
* @param \Narrowspark\Automatic\Common\Contract\Package $package
* @param \Narrowspark\Automatic\PackageConfigurator $packageConfigurator
* @param \Narrowspark\Automatic\Configurator $configurator
* @param \Narrowspark\Automatic\Common\Contract\Package $package
* @param \Narrowspark\Automatic\Contract\PackageConfigurator $packageConfigurator
* @param \Narrowspark\Automatic\Contract\Configurator $configurator
*
* @return void
*/
protected function showWarningOnRemainingConfigurators(
PackageContract $package,
PackageConfigurator $packageConfigurator,
Configurator $configurator
PackageConfiguratorContract $packageConfigurator,
ConfiguratorContract $configurator
): void {
$packageConfigurators = \array_keys((array) $package->getConfig(ConfiguratorContract::TYPE));
$packageConfigurators = \array_keys((array) $package->getConfig(CommonConfiguratorContract::TYPE));

foreach (\array_keys($configurator->getConfigurators()) as $key => $value) {
if (isset($packageConfigurators[$key])) {
Expand Down Expand Up @@ -129,9 +129,9 @@ protected function showWarningOnRemainingConfigurators(
*/
protected function addPackageConfigurators(PackageContract $package): void
{
if ($package->hasConfig(PackageConfigurator::TYPE)) {
if ($package->hasConfig(PackageConfiguratorContract::TYPE)) {
/** @var \Narrowspark\Automatic\Common\Configurator\AbstractConfigurator $class */
foreach ((array) $package->getConfig(PackageConfigurator::TYPE) as $name => $class) {
foreach ((array) $package->getConfig(PackageConfiguratorContract::TYPE) as $class) {
$reflectionClass = new \ReflectionClass($class);

if ($reflectionClass->isInstantiable() && $reflectionClass->hasMethod('getName')) {
Expand Down
8 changes: 2 additions & 6 deletions src/Automatic/PackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
namespace Narrowspark\Automatic;

use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Contract\PackageConfigurator as PackageConfiguratorContract;

final class PackageConfigurator extends AbstractConfigurator
final class PackageConfigurator extends AbstractConfigurator implements PackageConfiguratorContract
{
/**
* @var string
*/
public const TYPE = 'custom-configurators';

/**
* Get a package configurator.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Automatic/ScriptExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct(Composer $composer, IOInterface $io, ProcessExecutor
*
* @return void
*/
public function addExtender(string $name, string $extender): void
public function add(string $name, string $extender): void
{
if (isset($this->extenders[$name])) {
throw new InvalidArgumentException(\sprintf('Script executor with the name [%s] already exists.', $name));
Expand Down
Loading

0 comments on commit 25f231c

Please sign in to comment.