Skip to content

Commit

Permalink
Merge pull request #123 from lemberg/feature/83-config-updater
Browse files Browse the repository at this point in the history
Add configuration update manager
  • Loading branch information
T2L committed Jan 15, 2020
2 parents 0b12f9b + 8e886b5 commit 0d0ebab
Show file tree
Hide file tree
Showing 26 changed files with 814 additions and 237 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Draft Environment 3.x.x

- [GH-83](https://github.com/lemberg/draft-environment/issues/83) - Added configuration update manager
- [GH-92](https://github.com/lemberg/draft-environment/issues/92) - Added mod_expires and mod_headers for Apache2
- [GH-117](https://github.com/lemberg/draft-environment/issues/117) - Replaced Configurer with Composer event handler
- [GH-94](https://github.com/lemberg/draft-environment/issues/94) - Converted project to a composer-plugin. Clean up Draft Environment config files upon package uninstallation
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<exclude name="Drupal.Commenting.FunctionComment.MissingParamComment"/>
<!--Ignore "Description for the @return value is missing". -->
<exclude name="Drupal.Commenting.FunctionComment.MissingReturnComment"/>
<!--Ignore "Expected "%s" but found "%s" for parameter type". -->
<exclude name="Drupal.Commenting.FunctionComment.IncorrectParamVarName"/>
</rule>

</ruleset>
12 changes: 10 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ parameters:
- tests
ergebnis:
classesAllowedToBeExtended:
- Lemberg\Draft\Environment\Config\Manager\AbstractConfigManager
- Lemberg\Draft\Environment\Config\Install\Step\AbstractInstallStep
- Lemberg\Draft\Environment\Config\Update\Step\AbstractUpdateStep
ignoreErrors:
-
message: '#Method Lemberg\\Draft\\Environment\\Config\\Install\\Step\\AbstractInstallStep::getWeight\(\) is not final, but since the containing class is abstract, it should be.#'
path: src/Config/Install/Step/AbstractInstallStep.php
message: '#Method [A-Za-z0-9\\]+::[A-Za-z0-9\(\)]+ is not final, but since the containing class is abstract, it should be.#'
paths:
- src/Config/Install/Step/AbstractInstallStep.php
- src/Config/Update/Step/AbstractUpdateStep.php
-
message: '#Method [A-Za-z0-9\\]+::[A-Za-z0-9\(\)]+ is protected, but since the containing class is final, it can be private.#'
paths:
- src/Config/Config.php
46 changes: 35 additions & 11 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\Event;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Script\Event as ScriptEvent;
use Composer\Script\ScriptEvents;
use Lemberg\Draft\Environment\Config\InstallManager;
use Lemberg\Draft\Environment\Config\Manager\InstallManager;
use Lemberg\Draft\Environment\Config\Manager\UpdateManager;

/**
* Draft Environment application.
Expand All @@ -33,10 +35,15 @@ final class App {
private $io;

/**
* @var \Lemberg\Draft\Environment\Config\InstallManager
* @var \Lemberg\Draft\Environment\Config\Manager\InstallManager
*/
private $configInstallManager;

/**
* @var \Lemberg\Draft\Environment\Config\Manager\UpdateManager
*/
private $configUpdateManager;

/**
* Boolean indicating whether the installation process should run.
*
Expand All @@ -49,12 +56,14 @@ final class App {
*
* @param \Composer\Composer $composer
* @param \Composer\IO\IOInterface $io
* @param \Lemberg\Draft\Environment\Config\InstallManager $configInstallManager
* @param \Lemberg\Draft\Environment\Config\Manager\InstallManager $configInstallManager
* @param \Lemberg\Draft\Environment\Config\Manager\UpdateManager $configUpdateManager
*/
public function __construct(Composer $composer, IOInterface $io, InstallManager $configInstallManager) {
public function __construct(Composer $composer, IOInterface $io, InstallManager $configInstallManager, UpdateManager $configUpdateManager) {
$this->composer = $composer;
$this->io = $io;
$this->configInstallManager = $configInstallManager;
$this->configUpdateManager = $configUpdateManager;
}

/**
Expand All @@ -80,32 +89,47 @@ private function handlePackageEvent(PackageEvent $event): void {
if ($event->getName() === PackageEvents::POST_PACKAGE_INSTALL && $event->getOperation() instanceof InstallOperation) {
$this->onPostPackageInstall($event->getOperation());
}
if ($event->getName() === PackageEvents::POST_PACKAGE_UPDATE && $event->getOperation() instanceof UpdateOperation) {
$this->onPostPackageUpdate($event->getOperation());
}
if ($event->getName() === PackageEvents::PRE_PACKAGE_UNINSTALL && $event->getOperation() instanceof UninstallOperation) {
$this->onPrePackageUninstall($event->getOperation());
}
}

/**
* Pre package uninstall event callback.
* Post package install event callback.
*
* @param \Composer\DependencyResolver\Operation\UninstallOperation $operation
* @param \Composer\DependencyResolver\Operation\InstallOperation $operation
*/
private function onPrePackageUninstall(UninstallOperation $operation): void {
private function onPostPackageInstall(InstallOperation $operation): void {
// Clean up Draft Environment config files upon package uninstallation.
if ($operation->getPackage()->getName() === self::PACKAGE_NAME) {
$this->configInstallManager->uninstall();
$this->shouldRunInstallation = TRUE;
}
}

/**
* Post package update event callback.
*
* @param \Composer\DependencyResolver\Operation\UpdateOperation $operation
*/
private function onPostPackageUpdate(UpdateOperation $operation): void {
// Clean up Draft Environment config files upon package uninstallation.
if ($operation->getTargetPackage()->getName() === self::PACKAGE_NAME) {
$this->configUpdateManager->update();
}
}

/**
* Pre package uninstall event callback.
*
* @param \Composer\DependencyResolver\Operation\InstallOperation $operation
* @param \Composer\DependencyResolver\Operation\UninstallOperation $operation
*/
private function onPostPackageInstall(InstallOperation $operation): void {
private function onPrePackageUninstall(UninstallOperation $operation): void {
// Clean up Draft Environment config files upon package uninstallation.
if ($operation->getPackage()->getName() === self::PACKAGE_NAME) {
$this->shouldRunInstallation = TRUE;
$this->configInstallManager->uninstall();
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/Composer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
use Composer\Script\ScriptEvents;
use Lemberg\Draft\Environment\App;
use Lemberg\Draft\Environment\Config\Config;
use Lemberg\Draft\Environment\Config\InstallManager;
use Lemberg\Draft\Environment\Config\Manager\InstallManager;
use Lemberg\Draft\Environment\Config\Manager\UpdateManager;

/**
* Composer plugin for configuring Draft Environment.
Expand Down Expand Up @@ -42,7 +43,8 @@ public function activate(Composer $composer, IOInterface $io): void {

$config = new Config($sourceDirectory, $targetDirectory);
$configInstallManager = new InstallManager($composer, $io, $config);
$this->setApp(new App($composer, $io, $configInstallManager));
$configUpdateManager = new UpdateManager($composer, $io, $config);
$this->setApp(new App($composer, $io, $configInstallManager, $configUpdateManager));
}

/**
Expand All @@ -53,6 +55,7 @@ public function activate(Composer $composer, IOInterface $io): void {
public static function getSubscribedEvents(): array {
return [
PackageEvents::POST_PACKAGE_INSTALL => 'onComposerEvent',
PackageEvents::POST_PACKAGE_UPDATE => 'onComposerEvent',
PackageEvents::PRE_PACKAGE_UNINSTALL => 'onComposerEvent',
ScriptEvents::POST_INSTALL_CMD => 'onComposerEvent',
ScriptEvents::POST_UPDATE_CMD => 'onComposerEvent',
Expand Down
8 changes: 5 additions & 3 deletions src/Config/AbstractStepInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@

use Composer\Composer;
use Composer\IO\IOInterface;
use Lemberg\Draft\Environment\Config\Manager\ManagerInterface;
use Lemberg\Draft\Environment\Messanger\MessangerInterface;

/**
* Abstract installation/uninstall step.
*/
interface AbstractStepInterface {
interface AbstractStepInterface extends MessangerInterface {

/**
* @param \Composer\Composer $composer
* @param \Composer\IO\IOInterface $io
* @param \Lemberg\Draft\Environment\Config\Config $config
* @param \Lemberg\Draft\Environment\Config\Manager\ManagerInterface $configManager
*/
public function __construct(Composer $composer, IOInterface $io, Config $config);
public function __construct(Composer $composer, IOInterface $io, ManagerInterface $configManager);

/**
* Returns the weight of the step. Lighter steps will be executed sooner.
Expand Down
52 changes: 52 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace Lemberg\Draft\Environment\Config;

use Consolidation\Comments\Comments;
use Lemberg\Draft\Environment\Helper\FileReaderTrait;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;

/**
* Draft Environment configuration helper.
*/
final class Config {

use FileReaderTrait;

public const SOURCE_CONFIG_FILENAME = 'default.vm-settings.yml';
public const SOURCE_VM_FILENAME = 'Vagrantfile.proxy';
public const SOURCE_CONFIGURATION_FILENAMES = [
Expand Down Expand Up @@ -51,6 +59,7 @@ final class Config {
public function __construct(string $sourceDirectory, string $targetDirectory) {
$this->sourceDirectory = $sourceDirectory;
$this->targetDirectory = $targetDirectory;
$this->initFileSystem();
}

/**
Expand Down Expand Up @@ -124,4 +133,47 @@ public function getTargetConfigFilepath(string $filename): string {
throw new \InvalidArgumentException(sprintf("Non-existing Draft Environment target configuration filename '%s' has been passed.", $filename));
}

/**
* Reads and returns raw configuration from a given source file.
*
* @param string $source
*
* @return string
*/
public function readConfigFromTheFile(string $source): string {
return $this->readFile('config', $source);
}

/**
* Reads, parses and returns configuration from a given source file.
*
* @param string $source
*
* @return array<string, array>
*/
public function readAndParseConfigFromTheFile(string $source): array {
$content = $this->readConfigFromTheFile($source);
$parser = new Parser();
return $parser->parse($content);
}

/**
* Dumps and writes configuration to a given file.
*
* @param string $source
* @param string $target
* @param array<int|string,array> $config
*/
public function writeConfigToTheFile(string $source, string $target, array $config): void {
$yaml = new Dumper(2);
$alteredContent = $yaml->dump($config, PHP_INT_MAX, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);

$originalContent = $this->readConfigFromTheFile($source);

$commentManager = new Comments();
$commentManager->collect(explode("\n", $originalContent));
$alteredWithComments = $commentManager->inject(explode("\n", $alteredContent));
$this->writeFile($target, implode("\n", $alteredWithComments));
}

}
22 changes: 22 additions & 0 deletions src/Config/ConfigAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Lemberg\Draft\Environment\Config;

/**
* Implements Config getter and setter.
*/
interface ConfigAwareInterface {

/**
* @return \Lemberg\Draft\Environment\Config\Config
*/
public function getConfig(): Config;

/**
* @param \Lemberg\Draft\Environment\Config\Config $config
*/
public function setConfig(Config $config): void;

}
3 changes: 1 addition & 2 deletions src/Config/ConfigAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace Lemberg\Draft\Environment\Config;

/**
*
* @author roman
* Implements Config getter and setter.
*/
trait ConfigAwareTrait {

Expand Down
17 changes: 8 additions & 9 deletions src/Config/Install/Step/AbstractInstallStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@

use Composer\Composer;
use Composer\IO\IOInterface;
use Lemberg\Draft\Environment\Config\Config;
use Lemberg\Draft\Environment\Config\ConfigAwareTrait;
use Lemberg\Draft\Environment\Config\AbstractStepInterface;
use Lemberg\Draft\Environment\Config\Manager\ManagerInterface;
use Lemberg\Draft\Environment\Helper\FileReaderTrait;
use Lemberg\Draft\Environment\Messanger\MessangerTrait;
use Symfony\Component\Filesystem\Filesystem;

/**
* Default implementation of the installation/uninstall step.
*/
abstract class AbstractInstallStep implements AbstractStepInterface {

use ConfigAwareTrait;
use FileReaderTrait;
use MessangerTrait;

/**
Expand All @@ -31,18 +30,18 @@ abstract class AbstractInstallStep implements AbstractStepInterface {
protected $io;

/**
* @var \Symfony\Component\Filesystem\Filesystem
* @var \Lemberg\Draft\Environment\Config\Manager\ManagerInterface
*/
protected $fs;
protected $configInstallManager;

/**
* {@inheritdoc}
*/
final public function __construct(Composer $composer, IOInterface $io, Config $config) {
final public function __construct(Composer $composer, IOInterface $io, ManagerInterface $configManager) {
$this->composer = $composer;
$this->io = $io;
$this->fs = new Filesystem();
$this->setConfig($config);
$this->configInstallManager = $configManager;
$this->initFileSystem();
}

/**
Expand Down

0 comments on commit 0d0ebab

Please sign in to comment.