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

Commit

Permalink
abstracted some code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbannert committed Aug 9, 2018
1 parent dc3db53 commit a3d1b56
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 384 deletions.
140 changes: 140 additions & 0 deletions src/Automatic/AbstractConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
namespace Narrowspark\Automatic;

use Composer\Composer;
use Composer\IO\IOInterface;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Exception\InvalidArgumentException;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;

abstract class AbstractConfigurator
{
/**
* All registered automatic configurators.
*
* @var array
*/
protected $configurators = [];

/**
* A composer instance.
*
* @var \Composer\Composer
*/
protected $composer;

/**
* The composer io implementation.
*
* @var \Composer\IO\IOInterface
*/
protected $io;

/**
* A array of project options.
*
* @var array
*/
protected $options;

/**
* Create a new Configurator class.
*
* @param \Composer\Composer $composer
* @param \Composer\IO\IOInterface $io
* @param array $options
*/
public function __construct(Composer $composer, IOInterface $io, array $options)
{
$this->composer = $composer;
$this->io = $io;
$this->options = $options;
}

/**
* Add a new automatic configurator.
*
* @param string $name
* @param string $configurator
*
* @throws \Narrowspark\Automatic\Common\Contract\Exception\InvalidArgumentException
*
* @return void
*/
public function add(string $name, string $configurator): void
{
if ($this->has($name)) {
throw new InvalidArgumentException(\sprintf('Configurator with the name [%s] already exists.', $name));
}

if (! \is_subclass_of($configurator, ConfiguratorContract::class)) {
throw new InvalidArgumentException(\sprintf('Configurator class [%s] must extend the class [%s].', $configurator, ConfiguratorContract::class));
}

$this->configurators[$name] = $configurator;
}

/**
* Check if configurator is registered.
*
* @param string $name
*
* @return bool
*/
public function has(string $name): bool
{
return isset($this->configurators[$name]);
}

/**
* Configure the application after the package settings.
*
* @param \Narrowspark\Automatic\Common\Contract\Package $package
*
* @return void
*/
public function configure(PackageContract $package): void
{
foreach (\array_keys($this->configurators) as $key) {
if ($package->hasConfig($key)) {
$this->get($key)->configure($package);
}
}
}

/**
* Unconfigure the application after the package settings.
*
* @param \Narrowspark\Automatic\Common\Contract\Package $package
*
* @return void
*/
public function unconfigure(PackageContract $package): void
{
foreach (\array_keys($this->configurators) as $key) {
if ($package->hasConfig($key)) {
$this->get($key)->unconfigure($package);
}
}
}

/**
* Clear all configurators.
*
* @return void
*/
public function clear(): void
{
$this->configurators = [];
}

/**
* Get a configurator.
*
* @param string $key
*
* @return \Narrowspark\Automatic\Common\Contract\Configurator
*/
abstract protected function get(string $key): ConfiguratorContract;
}
52 changes: 35 additions & 17 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public function onPostUpdate(Event $event, array $operations = []): void
includeFile(\str_replace('%vendor_path%', $this->container->get('vendor-dir'), $path));
}

/** @var \Narrowspark\Automatic\Common\Configurator\AbstractConfigurator $class */
foreach ($classList as $class) {
$reflectionClass = new ReflectionClass($class);

Expand Down Expand Up @@ -615,11 +616,19 @@ private function doActionOnPackageOperation(PackageContract $package): void
}
}

$io = $this->container->get(IOInterface::class);

if ($package->getOperation() === 'install') {
$io->writeError(\sprintf(' - Configuring %s', $package->getName()));

$this->doInstall($package, $packageConfigurator);
} elseif ($package->getOperation() === 'uninstall') {
$io->writeError(\sprintf(' - Unconfiguring %s', $package->getName()));

$this->doUninstall($package, $packageConfigurator);
}

$packageConfigurator->clear();
}

/**
Expand All @@ -634,19 +643,18 @@ private function doActionOnPackageOperation(PackageContract $package): void
*/
private function doInstall(PackageContract $package, PackageConfigurator $packageConfigurator): void
{
$this->container->get(IOInterface::class)->writeError(\sprintf(' - Configuring %s', $package->getName()));

$this->container->get(Configurator::class)->configure($package);
$packageConfigurator->configure($package);

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

if ($package->hasConfig(QuestionInstallationManager::TYPE)) {
foreach ($questionInstallationManager->install($package, $package->getConfig(QuestionInstallationManager::TYPE)) as $operation) {
/** @var \Narrowspark\Automatic\Installer\QuestionInstallationManager $questionInstallationManager */
$questionInstallationManager = $this->container->get(QuestionInstallationManager::class);

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

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

if ($package->hasConfig('post-install-output')) {
Expand All @@ -657,15 +665,7 @@ private function doInstall(PackageContract $package, PackageConfigurator $packag
$this->postInstallOutput[] = '';
}

$lock = $this->container->get(Lock::class);

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

/**
Expand All @@ -680,8 +680,6 @@ private function doInstall(PackageContract $package, PackageConfigurator $packag
*/
private function doUninstall(PackageContract $package, PackageConfigurator $packageConfigurator): void
{
$this->container->get(IOInterface::class)->writeError(\sprintf(' - Unconfiguring %s', $package->getName()));

$this->container->get(Configurator::class)->unconfigure($package);
$packageConfigurator->unconfigure($package);

Expand Down Expand Up @@ -728,6 +726,26 @@ private function getErrorMessage(): ?string

return null;
}

/**
* Write the package operation to the lock file.
*
* @param \Narrowspark\Automatic\Common\Contract\Package $package
*
* @return void
*/
private function writePackageOperationToLock(PackageContract $package): void
{
$lock = $this->container->get(Lock::class);

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

/**
Expand Down
113 changes: 8 additions & 105 deletions src/Automatic/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
declare(strict_types=1);
namespace Narrowspark\Automatic;

use Composer\Composer;
use Composer\IO\IOInterface;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Exception\InvalidArgumentException;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
use Narrowspark\Automatic\Configurator\ComposerScriptsConfigurator;
use Narrowspark\Automatic\Configurator\CopyFromPackageConfigurator;
use Narrowspark\Automatic\Configurator\EnvConfigurator;
use Narrowspark\Automatic\Configurator\GitIgnoreConfigurator;

final class Configurator
final class Configurator extends AbstractConfigurator
{
/**
* All registered automatic configurators.
*
* @var array
*/
private static $configurators = [
protected $configurators = [
'composer-scripts' => ComposerScriptsConfigurator::class,
'copy' => CopyFromPackageConfigurator::class,
'env' => EnvConfigurator::class,
Expand All @@ -34,105 +30,12 @@ final class Configurator
private $cache = [];

/**
* A composer instance.
*
* @var \Composer\Composer
* {@inheritdoc}
*/
private $composer;

/**
* The composer io implementation.
*
* @var \Composer\IO\IOInterface
*/
private $io;

/**
* A array of project options.
*
* @var array
*/
private $options;

/**
* Create a new Configurator class.
*
* @param \Composer\Composer $composer
* @param \Composer\IO\IOInterface $io
* @param array $options
*/
public function __construct(Composer $composer, IOInterface $io, array $options)
{
$this->composer = $composer;
$this->io = $io;
$this->options = $options;
}

/**
* Add a new automatic configurator.
*
* @param string $name
* @param string $configurator
*
* @throws \Narrowspark\Automatic\Common\Contract\Exception\InvalidArgumentException
*
* @return void
*/
public function add(string $name, string $configurator): void
public function clear(): void
{
if ($this->has($name)) {
throw new InvalidArgumentException(\sprintf('Configurator with the name "%s" already exists.', $name));
}

if (! \is_subclass_of($configurator, ConfiguratorContract::class)) {
throw new InvalidArgumentException(\sprintf('Configurator class "%s" must extend the class "%s".', $configurator, ConfiguratorContract::class));
}

self::$configurators[$name] = $configurator;
}

/**
* Check if configurator is registered.
*
* @param string $name
*
* @return bool
*/
public function has(string $name): bool
{
return isset(self::$configurators[$name]);
}

/**
* Configure the application after the package settings.
*
* @param \Narrowspark\Automatic\Common\Contract\Package $package
*
* @return void
*/
public function configure(PackageContract $package): void
{
foreach (\array_keys(self::$configurators) as $key) {
if ($package->hasConfig($key)) {
$this->get($key)->configure($package);
}
}
}

/**
* Unconfigure the application after the package settings.
*
* @param \Narrowspark\Automatic\Common\Contract\Package $package
*
* @return void
*/
public function unconfigure(PackageContract $package): void
{
foreach (\array_keys(self::$configurators) as $key) {
if ($package->hasConfig($key)) {
$this->get($key)->unconfigure($package);
}
}
$this->configurators = [];
$this->cache = [];
}

/**
Expand All @@ -142,13 +45,13 @@ public function unconfigure(PackageContract $package): void
*
* @return \Narrowspark\Automatic\Common\Contract\Configurator
*/
private function get(string $key): ConfiguratorContract
protected function get(string $key): ConfiguratorContract
{
if (isset($this->cache[$key])) {
return $this->cache[$key];
}

$class = self::$configurators[$key];
$class = $this->configurators[$key];

return $this->cache[$key] = new $class($this->composer, $this->io, $this->options);
}
Expand Down
Loading

0 comments on commit a3d1b56

Please sign in to comment.