Skip to content

Commit

Permalink
Optimize the config of plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Feb 18, 2017
1 parent 9b630ce commit d3b1295
Show file tree
Hide file tree
Showing 19 changed files with 324 additions and 125 deletions.
22 changes: 21 additions & 1 deletion Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Composer\Installer\PackageEvent;
use Composer\Package\PackageInterface;
use Fxp\Composer\AssetPlugin\Assets;
use Fxp\Composer\AssetPlugin\Config\Config;
use Fxp\Composer\AssetPlugin\FxpAssetPlugin;
use Fxp\Composer\AssetPlugin\Installer\IgnoreFactory;

/**
Expand All @@ -39,10 +41,28 @@ public static function deleteIgnoredFiles(PackageEvent $event)
}

$section = static::getIgnoreConfigSection();
$manager = IgnoreFactory::create($event->getComposer(), $package, null, $section);
$manager = IgnoreFactory::create(static::getConfig($event), $event->getComposer(), $package, null, $section);
$manager->cleanup();
}

/**
* Get the plugin config.
*
* @param PackageEvent $event
*
* @return Config
*/
public static function getConfig(PackageEvent $event)
{
foreach ($event->getComposer()->getPluginManager()->getPlugins() as $plugin) {
if ($plugin instanceof FxpAssetPlugin) {
return $plugin->getConfig();
}
}

throw new \RuntimeException('The fxp composer asset plugin is not found');
}

/**
* Get the root config section of igore file patterns for each package.
*
Expand Down
63 changes: 63 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Fxp Composer Asset Plugin package.
*
* (c) François Pluchino <francois.pluchino@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fxp\Composer\AssetPlugin\Config;

/**
* Helper of package config.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
final class Config
{
/**
* @var array
*/
private $config;

/**
* Constructor.
*
* @param array $config The config
*/
public function __construct(array $config)
{
$this->config = $config;
}

/**
* Get the array config value.
*
* @param string $key The config key
* @param array $default The default value
*
* @return array
*/
public function getArray($key, array $default = array())
{
return $this->get($key, $default);
}

/**
* Get the config value.
*
* @param string $key The config key
* @param mixed|null $default The default value
*
* @return mixed|null
*/
public function get($key, $default = null)
{
return array_key_exists($key, $this->config)
? $this->config[$key]
: $default;
}
}
60 changes: 20 additions & 40 deletions Util/Config.php → Config/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
* file that was distributed with this source code.
*/

namespace Fxp\Composer\AssetPlugin\Util;
namespace Fxp\Composer\AssetPlugin\Config;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Package\RootPackageInterface;

/**
* Helper of package config.
* Plugin Config builder.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
abstract class Config
abstract class ConfigBuilder
{
/**
* List of the deprecated options.
Expand Down Expand Up @@ -60,54 +61,33 @@ public static function validate(IOInterface $io, RootPackageInterface $package,
}

/**
* Get the array config value.
* Build the config of plugin.
*
* @param RootPackageInterface $package The root package
* @param string $key The config key
* @param array $default The default value
* @param Composer $composer The composer
*
* @return array
*/
public static function getArray(RootPackageInterface $package, $key, array $default = array())
{
return (array) static::get($package, $key, $default);
}

/**
* Get the config value.
*
* @param RootPackageInterface $package The root package
* @param string $key The config key
* @param mixed|null $default The default value
*
* @return mixed|null
* @return Config
*/
public static function get(RootPackageInterface $package, $key, $default = null)
public static function build(Composer $composer)
{
$config = self::injectDeprecatedConfig(self::getConfigBase($package), (array) $package->getExtra(), $key);
$config = self::injectDeprecatedConfig(self::getConfigBase($composer), (array) $composer->getPackage()->getExtra());

return array_key_exists($key, $config)
? $config[$key]
: $default;
return new Config($config);
}

/**
* Inject the deprecated keys in config if the config keys are not present.
*
* @param array $config The
* @param array $extra
* @param string $key
* @param array $config The config
* @param array $extra The root package extra section
*
* @return array
*/
private static function injectDeprecatedConfig(array $config, array $extra, $key)
private static function injectDeprecatedConfig(array $config, array $extra)
{
$deprecatedKey = isset(self::$deprecatedOptions[$key])
? self::$deprecatedOptions[$key]
: 'asset-'.$key;

if (array_key_exists($deprecatedKey, $extra) && !array_key_exists($key, $config)) {
$config[$key] = $extra[$deprecatedKey];
foreach (self::$deprecatedOptions as $key => $deprecatedKey) {
if (array_key_exists($deprecatedKey, $extra) && !array_key_exists($key, $config)) {
$config[$key] = $extra[$deprecatedKey];
}
}

return $config;
Expand All @@ -116,13 +96,13 @@ private static function injectDeprecatedConfig(array $config, array $extra, $key
/**
* Get the base of data.
*
* @param RootPackageInterface $package The config data
* @param Composer $composer The compser
*
* @return array
*/
private static function getConfigBase($package)
private static function getConfigBase(Composer $composer)
{
$config = $package->getConfig();
$config = $composer->getPackage()->getConfig();

return isset($config['fxp-asset']) && is_array($config['fxp-asset'])
? $config['fxp-asset']
Expand Down
29 changes: 23 additions & 6 deletions FxpAssetPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Repository\InstalledFilesystemRepository;
use Fxp\Composer\AssetPlugin\Config\Config;
use Fxp\Composer\AssetPlugin\Config\ConfigBuilder;
use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager;
use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter;
use Fxp\Composer\AssetPlugin\Util\AssetPlugin;
use Fxp\Composer\AssetPlugin\Util\Config;

/**
* Composer plugin.
Expand All @@ -32,6 +33,11 @@
*/
class FxpAssetPlugin implements PluginInterface, EventSubscriberInterface
{
/**
* @var Config
*/
protected $config;

/**
* @var Composer
*/
Expand Down Expand Up @@ -74,17 +80,18 @@ public function activate(Composer $composer, IOInterface $io)
{
/* @var InstalledFilesystemRepository $installedRepository */
$installedRepository = $composer->getRepositoryManager()->getLocalRepository();
$this->config = ConfigBuilder::build($composer);
$this->composer = $composer;
$this->io = $io;
$this->packageFilter = new VcsPackageFilter($composer->getPackage(), $composer->getInstallationManager(), $installedRepository);
$this->packageFilter = new VcsPackageFilter($this->config, $composer->getPackage(), $composer->getInstallationManager(), $installedRepository);
$this->assetRepositoryManager = new AssetRepositoryManager($io, $composer->getRepositoryManager(), $this->packageFilter);

AssetPlugin::addRegistryRepositories($this->assetRepositoryManager, $this->packageFilter, $composer->getPackage());
AssetPlugin::addRegistryRepositories($this->assetRepositoryManager, $this->packageFilter, $this->config);
AssetPlugin::setVcsTypeRepositories($composer->getRepositoryManager());

$this->assetRepositoryManager->addRepositories(Config::getArray($composer->getPackage(), 'repositories'));
$this->assetRepositoryManager->addRepositories($this->config->getArray('repositories'));

AssetPlugin::addInstallers($composer, $io);
AssetPlugin::addInstallers($this->config, $composer, $io);
}

/**
Expand All @@ -94,7 +101,7 @@ public function activate(Composer $composer, IOInterface $io)
*/
public function onPluginCommand(CommandEvent $event)
{
Config::validate($this->io, $this->composer->getPackage(), $event->getCommandName());
ConfigBuilder::validate($this->io, $this->composer->getPackage(), $event->getCommandName());

if (!in_array($event->getCommandName(), array('install', 'update'))) {
$this->packageFilter->setEnabled(false);
Expand All @@ -110,4 +117,14 @@ public function onPreDependenciesSolving(InstallerEvent $event)
{
$this->assetRepositoryManager->setPool($event->getPool());
}

/**
* Get the plugin config.
*
* @return Config
*/
public function getConfig()
{
return $this->config;
}
}
19 changes: 13 additions & 6 deletions Installer/AssetInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
use Fxp\Composer\AssetPlugin\Config\Config;
use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;
use Fxp\Composer\AssetPlugin\Util\AssetPlugin;
use Fxp\Composer\AssetPlugin\Util\Config;

/**
* Installer for asset packages.
Expand All @@ -28,19 +28,26 @@
*/
class AssetInstaller extends LibraryInstaller
{
/**
* @var Config
*/
private $config;

/**
* Constructor.
*
* @param Config $config
* @param IOInterface $io
* @param Composer $composer
* @param AssetTypeInterface $assetType
* @param Filesystem $filesystem
*/
public function __construct(IOInterface $io, Composer $composer, AssetTypeInterface $assetType, Filesystem $filesystem = null)
public function __construct(Config $config, IOInterface $io, Composer $composer, AssetTypeInterface $assetType, Filesystem $filesystem = null)
{
parent::__construct($io, $composer, $assetType->getComposerType(), $filesystem);

$paths = Config::getArray($composer->getPackage(), 'installer-paths');
$this->config = $config;
$paths = $this->config->getArray('installer-paths');

if (!empty($paths[$this->type])) {
$this->vendorDir = rtrim($paths[$this->type], '/');
Expand Down Expand Up @@ -84,7 +91,7 @@ protected function getPackageBasePath(PackageInterface $package)
*/
protected function installCode(PackageInterface $package)
{
$package = AssetPlugin::addMainFiles($this->composer, $package);
$package = AssetPlugin::addMainFiles($this->config, $package);

parent::installCode($package);

Expand All @@ -96,7 +103,7 @@ protected function installCode(PackageInterface $package)
*/
protected function updateCode(PackageInterface $initial, PackageInterface $target)
{
$target = AssetPlugin::addMainFiles($this->composer, $target);
$target = AssetPlugin::addMainFiles($this->config, $target);

parent::updateCode($initial, $target);

Expand All @@ -110,7 +117,7 @@ protected function updateCode(PackageInterface $initial, PackageInterface $targe
*/
protected function deleteIgnoredFiles(PackageInterface $package)
{
$manager = IgnoreFactory::create($this->composer, $package, $this->getInstallPath($package));
$manager = IgnoreFactory::create($this->config, $this->composer, $package, $this->getInstallPath($package));

if ($manager->isEnabled() && !$manager->hasPattern()) {
$this->addIgnorePatterns($manager, $package);
Expand Down
7 changes: 4 additions & 3 deletions Installer/IgnoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Composer\Composer;
use Composer\Package\PackageInterface;
use Fxp\Composer\AssetPlugin\Util\Config;
use Fxp\Composer\AssetPlugin\Config\Config;

/**
* Factory of ignore manager patterns.
Expand All @@ -25,18 +25,19 @@ class IgnoreFactory
/**
* Create a ignore manager.
*
* @param Config $config The plugin config
* @param Composer $composer The composer instance
* @param PackageInterface $package The package instance
* @param string|null $installDir The custom installation directory
* @param string|null $section The config section of ignore patterns
*
* @return IgnoreManager
*/
public static function create(Composer $composer, PackageInterface $package, $installDir = null, $section = 'ignore-files')
public static function create(Config $config, Composer $composer, PackageInterface $package, $installDir = null, $section = 'ignore-files')
{
$installDir = static::getInstallDir($composer, $package, $installDir);
$manager = new IgnoreManager($installDir);
$config = Config::getArray($composer->getPackage(), $section);
$config = $config->getArray($section);

foreach ($config as $packageName => $patterns) {
if ($packageName === $package->getName()) {
Expand Down
Loading

0 comments on commit d3b1295

Please sign in to comment.