Skip to content

Commit

Permalink
Added building alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jul 25, 2019
1 parent b1c9639 commit 7317438
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public function __construct($outputDir = null)
$this->setOutputDir($outputDir);
}

public function createAlternative($name): Builder
{
$dir = $this->outputDir . DIRECTORY_SEPARATOR . $name;
$alt = new static($dir);
foreach (['aliases', 'packages'] as $key) {
$alt->configs[$key] = $this->getConfig($key)->clone($alt);
}

return $alt;
}

public function setOutputDir($outputDir)
{
$this->outputDir = $outputDir ?: static::findOutputDir();
Expand Down Expand Up @@ -145,6 +156,16 @@ public function getConfig(string $name)
return $this->configs[$name];
}

public function getVar($name, $key)
{
$config = $this->configs[$name] ?? null;
if (empty($config)) {
return null;
}

return $config->getValues()[$key] ?? null;
}

public function getVars()
{
$vars = [];
Expand Down
32 changes: 23 additions & 9 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@
*/
class Plugin implements PluginInterface, EventSubscriberInterface
{
const EXTRA_OPTION_NAME = 'config-plugin';
const EXTRA_DEV_OPTION_NAME = 'config-plugin-dev';

/**
* @var Package[] the array of active composer packages
*/
protected $packages;

private $alternatives = [];

private $outputDir;

private $rootPackage;

/**
* @var array config name => list of files
*/
protected $files = [
'dotenv' => [],
'aliases' => [],
'defines' => [],
'params' => [],
];
Expand Down Expand Up @@ -103,7 +105,16 @@ public function onPostAutoloadDump(Event $event)
$this->reorderFiles();
$this->showDepsTree();

$this->builder->setOutputDir($this->outputDir);
$this->builder->buildAllConfigs($this->files);

$save = $this->files;
foreach ($this->alternatives as $alt => $files) {
$this->files = $save;
$builder = $this->builder->createAlternative($alt);
$this->addFiles($this->rootPackage, $files);
$builder->buildAllConfigs($this->files);
}
}

protected function initAutoload()
Expand Down Expand Up @@ -170,18 +181,21 @@ protected function orderFiles(array $files): array
*/
protected function processPackage(Package $package)
{
$extra = $package->getExtra();
$files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null;
$files = $package->getFiles();
$this->originalFiles[$package->getPrettyName()] = $files;

if (is_array($files)) {
if (!empty($files)) {
$this->addFiles($package, $files);
}
if ($package->isRoot()) {
$this->rootPackage = $package;
$this->loadDotEnv($package);
if (!empty($extra[self::EXTRA_DEV_OPTION_NAME])) {
$this->addFiles($package, $extra[self::EXTRA_DEV_OPTION_NAME]);
$devFiles = $package->getDevFiles();
if (!empty($devFiles)) {
$this->addFiles($package, $devFiles);
}
$this->alternatives = $package->getAlternatives();
$this->outputDir = $package->getOutputDir();
}

$aliases = $package->collectAliases();
Expand Down

0 comments on commit 7317438

Please sign in to comment.