This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
danielbannert
committed
Aug 9, 2018
1 parent
dc3db53
commit a3d1b56
Showing
12 changed files
with
389 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.