Skip to content

Commit

Permalink
Initialize the plugin only when the solver sat is used
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed May 17, 2019
1 parent d067b84 commit 341d972
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
61 changes: 51 additions & 10 deletions Foxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\InstallerEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
Expand All @@ -39,11 +40,36 @@ class Foxy implements PluginInterface, EventSubscriberInterface
{
const REQUIRED_COMPOSER_VERSION = '1.5.0';

/**
* @var Config
*/
protected $config;

/**
* @var AssetManagerInterface
*/
protected $assetManager;

/**
* @var AssetFallback
*/
protected $assetFallback;

/**
* @var ComposerFallback
*/
protected $composerFallback;

/**
* @var SolverInterface
*/
protected $solver;

/**
* @var bool
*/
protected $initialized = false;

/**
* The list of the classes of asset managers.
*/
Expand Down Expand Up @@ -80,6 +106,9 @@ class Foxy implements PluginInterface, EventSubscriberInterface
public static function getSubscribedEvents()
{
return array(
InstallerEvents::PRE_DEPENDENCIES_SOLVING => array(
array('init', 100),
),
ScriptEvents::POST_INSTALL_CMD => array(
array('solveAssets', 100),
),
Expand All @@ -95,21 +124,33 @@ public static function getSubscribedEvents()
public function activate(Composer $composer, IOInterface $io)
{
ComposerUtil::validateVersion(static::REQUIRED_COMPOSER_VERSION, Composer::VERSION);

$input = ConsoleUtil::getInput($io);
$config = ConfigBuilder::build($composer, self::$defaultConfig, $io);
$executor = new ProcessExecutor($io);
$fs = new Filesystem($executor);
$assetManager = $this->getAssetManager($io, $config, $executor, $fs);
$assetFallback = new AssetFallback($io, $config, $assetManager->getPackageName(), $fs);
$composerFallback = new ComposerFallback($composer, $io, $config, $input, $fs);
$this->solver = new Solver($assetManager, $config, $fs, $composerFallback);

$assetFallback->save();
$composerFallback->save();
$assetManager->setFallback($assetFallback);
$this->config = ConfigBuilder::build($composer, self::$defaultConfig, $io);
$this->assetManager = $this->getAssetManager($io, $this->config, $executor, $fs);
$this->assetFallback = new AssetFallback($io, $this->config, $this->assetManager->getPackageName(), $fs);
$this->composerFallback = new ComposerFallback($composer, $io, $this->config, $input, $fs);
$this->solver = new Solver($this->assetManager, $this->config, $fs, $this->composerFallback);

$this->assetManager->setFallback($this->assetFallback);
}

/**
* Init the plugin.
*/
public function init()
{
if (!$this->initialized) {
$this->initialized = true;
$this->assetFallback->save();
$this->composerFallback->save();

if ($config->get('enabled')) {
$assetManager->validate();
if ($this->config->get('enabled')) {
$this->assetManager->validate();
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/FoxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ protected function setUp()

public function testGetSubscribedEvents()
{
$this->assertCount(2, Foxy::getSubscribedEvents());
$this->assertCount(3, Foxy::getSubscribedEvents());
}

public function testActivate()
{
$foxy = new Foxy();
$foxy->activate($this->composer, $this->io);
$foxy->init();
$this->assertTrue(true);
}

Expand Down

0 comments on commit 341d972

Please sign in to comment.