Skip to content

Commit

Permalink
Allow to configure additional CLI commands from workflow config file
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Aug 14, 2020
1 parent 3d53299 commit e071f83
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
12 changes: 7 additions & 5 deletions bin/ocmcg.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@

$helperSet->set(new Console\Config($resolver), Config::class);

$application->addCommands(
[
new Console\WorkflowCommand(),
]
$configClass = $helperSet->get(Config::class)->resolver()->resolve(
$helperSet->get(Console\WorkflowContext::class)->context()
);
$consoleCommands = $configClass->consoleCommands();
$consoleCommands[] = new Console\WorkflowCommand();

$application->addCommands($consoleCommands);

unset($workflowContext, $config, $argvInput);
unset($workflowContext, $config, $configClass, $argvInput);

$application->run();
21 changes: 21 additions & 0 deletions src/Config/ArrayConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace OpenCodeModeling\CodeGenerator\Config;

use OpenCodeModeling\CodeGenerator\Workflow\Description;
use Symfony\Component\Console\Command\Command;

final class ArrayConfig implements Component
{
Expand All @@ -19,6 +20,11 @@ final class ArrayConfig implements Component
**/
private $config;

/**
* @var Command[]
**/
private $consoleCommands = [];

public function __construct(Description ...$config)
{
$this->config = $config;
Expand All @@ -28,4 +34,19 @@ public function componentDescriptions(): array
{
return $this->config;
}

public function consoleCommands(): iterable
{
return $this->consoleCommands;
}

/**
* @param Command ...$consoleCommands
*/
public function addConsoleCommands(Command ...$consoleCommands): void
{
foreach ($consoleCommands as $consoleCommand) {
$this->consoleCommands[] = $consoleCommand;
}
}
}
22 changes: 22 additions & 0 deletions src/Config/ComponentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace OpenCodeModeling\CodeGenerator\Config;

use Symfony\Component\Console\Command\Command;

final class ComponentList implements ComponentCollection
{
private $position = 0;
Expand All @@ -19,6 +21,11 @@ final class ComponentList implements ComponentCollection
**/
private $config;

/**
* @var Command[]
**/
private $consoleCommands = [];

public function __construct(Component ...$config)
{
$this->config = $config;
Expand Down Expand Up @@ -48,4 +55,19 @@ public function valid()
{
return isset($this->config[$this->position]);
}

public function consoleCommands(): iterable
{
return $this->consoleCommands;
}

/**
* @param Command ...$consoleCommands
*/
public function addConsoleCommands(Command ...$consoleCommands): void
{
foreach ($consoleCommands as $consoleCommand) {
$this->consoleCommands[] = $consoleCommand;
}
}
}
8 changes: 8 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

namespace OpenCodeModeling\CodeGenerator\Config;

use Symfony\Component\Console\Command\Command;

interface Config
{
/**
* Optional list of CLI commands to register for Code Generation
*
* @return Command[]
*/
public function consoleCommands(): iterable;
}
11 changes: 10 additions & 1 deletion src/Config/FilePhpResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ final class FilePhpResolver implements Resolver
**/
private $file;

/**
* @var Config
*/
private $resolved;

public function __construct(string $file)
{
$this->file = $file;
}

public function resolve(WorkflowContext $workflowContext): Config
{
return require $this->file;
if ($this->resolved === null) {
$this->resolved = require $this->file;
}

return $this->resolved;
}
}

0 comments on commit e071f83

Please sign in to comment.