Skip to content

Commit

Permalink
Run components individually
Browse files Browse the repository at this point in the history
  • Loading branch information
chevli committed Sep 14, 2016
1 parent 013ee84 commit f05d3ca
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 76 deletions.
28 changes: 25 additions & 3 deletions Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,28 @@ public function __construct(

protected function configure()
{
$environmentOption = new InputOption(
'env',
'e',
InputOption::VALUE_REQUIRED,
'Specify environment configuration'
);

$componentOption = new InputOption(
'component',
'c',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Test',
array()
);

$this
->setName('configurator:run')
->setDescription('Run configurator components')
->setDefinition(
new InputDefinition(array(
new InputOption('env', 'e', InputOption::VALUE_REQUIRED, 'Specify environment configuration')
$environmentOption,
$componentOption
))
);
}
Expand All @@ -67,11 +83,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
try {

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<comment>Starting Configurator</comment>');
}

$environment = $input->getOption('env');
$components = $input->getOption('component');

$logLevel = OutputInterface::VERBOSITY_NORMAL;
$verbose = $input->getOption('verbose');
Expand All @@ -85,10 +102,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$this->processor->setEnvironment($environment);

foreach($components as $component) {
$this->processor->addComponent($component);
}

$this->processor->getLogger()->setLogLevel($logLevel);
$this->processor->run();

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<comment>Finished Configurator</comment>');
}

Expand Down
181 changes: 109 additions & 72 deletions Model/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,12 @@ public function getLogger()

/**
* @param string $componentName
* @return Processor
*/
public function addComponent($componentName)
{
try {
if (!$this->isValidComponent($componentName)) {
throw new ComponentException(
sprintf('%s component does not appear to be a valid component.', $componentName)
);
}

$componentClass = $this->configInterface->getComponentByName($componentName);
$this->components[$componentName] = new $componentClass($this->log);
} catch (ComponentException $e) {
throw $e;
}
$this->components[$componentName] = $componentName;
return $this;
}

/**
Expand Down Expand Up @@ -107,83 +98,123 @@ public function run()
// If the components list is empty, then the user would want to run all components in the master.yaml
if (empty($this->components)) {

try {
$this->runAllComponents();
return;
}

// Read master yaml
$masterPath = BP . '/app/etc/master.yaml';
if (!file_exists($masterPath)) {
throw new ComponentException("Master YAML does not exist. Please create one in $masterPath");
}
$this->log->logComment(sprintf("Found Master YAML"));
$yamlContents = file_get_contents($masterPath);
$yaml = new Parser();
$master = $yaml->parse($yamlContents);
$this->runIndividualComponents();
}

//print_r($master);
private function runIndividualComponents()
{
try {

// Validate master yaml
$this->validateMasterYaml($master);
// Get the master yaml
$master = $this->getMasterYaml();

// Loop through components and run them individually in the master.yaml order
foreach ($master as $componentAlias => $componentConfig) {
// Loop through the components
foreach ($this->components as $componentAlias) {

$this->log->logComment(sprintf("Loading component %s", $componentAlias));
// Get the config for the component from the master yaml array
$masterConfig = $master[$componentAlias];

$componentClass = $this->configInterface->getComponentByName($componentAlias);
// Run that component
$this->runComponent($componentAlias, $masterConfig);
}
} catch (ComponentException $e) {
$this->log->logError($e->getMessage());
}
}

/* @var ComponentAbstract $component */
$component = $this->objectManager->create($componentClass);
foreach ($componentConfig['sources'] as $source) {
$component->setSource($source)->process();
}
private function runAllComponents()
{
try {

// Check if there are environment specific nodes placed
if (!isset($componentConfig['env'])) {
// Get the master yaml
$master = $this->getMasterYaml();

// If not, continue to next component
$this->log->logComment(
sprintf("No environment node for '%s' component", $component->getComponentName())
);
continue;
}
// Loop through components and run them individually in the master.yaml order
foreach ($master as $componentAlias => $componentConfig) {

// Check if there is a node for this particular environment
if (!isset($componentConfig['env'][$this->getEnvironment()])) {

// If not, continue to next component
$this->log->logComment(
sprintf(
"No '%s' environment specific node for '%s' component",
$this->getEnvironment(),
$component->getComponentName()
)
);
continue;
}
// Run the component in question
$this->runComponent($componentAlias, $componentConfig);
}
} catch (ComponentException $e) {
$this->log->logError($e->getMessage());
}
}

// Check if there are sources for the environment
if (!isset($componentConfig['env'][$this->getEnvironment()]['sources'])) {

// If not continue
$this->log->logComment(
sprintf(
"No '%s' environment specific sources for '%s' component",
$this->getEnvironment(),
$component->getComponentName()
)
);
continue;
}
private function runComponent($componentAlias, $componentConfig)
{
$this->log->logComment(sprintf("Loading component %s", $componentAlias));

$componentClass = $this->configInterface->getComponentByName($componentAlias);

}
/* @var ComponentAbstract $component */
$component = $this->objectManager->create($componentClass);
foreach ($componentConfig['sources'] as $source) {
$component->setSource($source)->process();
}

// Check if there are environment specific nodes placed
if (!isset($componentConfig['env'])) {

} catch (ComponentException $e) {
$this->log->logError($e->getMessage());
}
// If not, continue to next component
$this->log->logComment(
sprintf("No environment node for '%s' component", $component->getComponentName())
);
return;
}

// Check if there is a node for this particular environment
if (!isset($componentConfig['env'][$this->getEnvironment()])) {

// If not, continue to next component
$this->log->logComment(
sprintf(
"No '%s' environment specific node for '%s' component",
$this->getEnvironment(),
$component->getComponentName()
)
);
return;
}

// Check if there are sources for the environment
if (!isset($componentConfig['env'][$this->getEnvironment()]['sources'])) {

// If not continue
$this->log->logComment(
sprintf(
"No '%s' environment specific sources for '%s' component",
$this->getEnvironment(),
$component->getComponentName()
)
);
return;
}

}

/**
* @return array
*/
private function getMasterYaml()
{
// Read master yaml
$masterPath = BP . '/app/etc/master.yaml';
if (!file_exists($masterPath)) {
throw new ComponentException("Master YAML does not exist. Please create one in $masterPath");
}
$this->log->logComment(sprintf("Found Master YAML"));
$yamlContents = file_get_contents($masterPath);
$yaml = new Parser();
$master = $yaml->parse($yamlContents);

// Validate master yaml
$this->validateMasterYaml($master);

return $master;
}

/**
Expand All @@ -198,6 +229,12 @@ private function isValidComponent($componentName)
$this->log->logQuestion(sprintf("Does the %s component exist?", $componentName));
}
$componentClass = $this->configInterface->getComponentByName($componentName);

if (!$componentClass) {
$this->log->logError(sprintf("The %s component has no class name.", $componentName));
return false;
}

$this->log->logComment(sprintf("The %s component has %s class name.", $componentName, $componentClass));
$component = $this->objectManager->create($componentClass);
if ($component instanceof ComponentAbstract) {
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ php vendor/bin/phpcpd vendor/ctidigital/magento2-configurator/Model/ vendor/ctid
php vendor/bin/phpunit --coverage-clover build/logs/clover.xml vendor/ctidigital/magento2-configurator/Test/Unit/
```

## Getting Started
1. Create a `master.yaml` file in `<mage_root>/app/etc/`. (see `Samples/master.yaml`)
2. Enable Modules `CtiDigital_Configurator`,`FireGento_FastSimpleImport`.
3. Run `bin/magento configurator:run --env="<environment>"`

### Usage

* Listing available components `bin/magento configurator:list`
* Running individual components `bin/magento configurator:run --env="<environment>" --components="config"`
* Extra logs `bin/magento configurator:run --env="<environment>" -v`

## Roadmap for components to do

| Component | Code Written | Tests Written | Sample Files |
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"satooshi/php-coveralls": "^1.0",
"magento/mtf": "dev-develop"
},
"version": "0.2.0-dev",
"version": "0.3.0-dev",
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
Expand Down

0 comments on commit f05d3ca

Please sign in to comment.