Skip to content

Commit f05d3ca

Browse files
committed
Run components individually
1 parent 013ee84 commit f05d3ca

File tree

4 files changed

+146
-76
lines changed

4 files changed

+146
-76
lines changed

Console/Command/RunCommand.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,28 @@ public function __construct(
4747

4848
protected function configure()
4949
{
50+
$environmentOption = new InputOption(
51+
'env',
52+
'e',
53+
InputOption::VALUE_REQUIRED,
54+
'Specify environment configuration'
55+
);
56+
57+
$componentOption = new InputOption(
58+
'component',
59+
'c',
60+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
61+
'Test',
62+
array()
63+
);
64+
5065
$this
5166
->setName('configurator:run')
5267
->setDescription('Run configurator components')
5368
->setDefinition(
5469
new InputDefinition(array(
55-
new InputOption('env', 'e', InputOption::VALUE_REQUIRED, 'Specify environment configuration')
70+
$environmentOption,
71+
$componentOption
5672
))
5773
);
5874
}
@@ -67,11 +83,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
6783
{
6884
try {
6985

70-
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
86+
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
7187
$output->writeln('<comment>Starting Configurator</comment>');
7288
}
7389

7490
$environment = $input->getOption('env');
91+
$components = $input->getOption('component');
7592

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

87104
$this->processor->setEnvironment($environment);
105+
106+
foreach($components as $component) {
107+
$this->processor->addComponent($component);
108+
}
109+
88110
$this->processor->getLogger()->setLogLevel($logLevel);
89111
$this->processor->run();
90112

91-
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
113+
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
92114
$output->writeln('<comment>Finished Configurator</comment>');
93115
}
94116

Model/Processor.php

+109-72
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,12 @@ public function getLogger()
5656

5757
/**
5858
* @param string $componentName
59+
* @return Processor
5960
*/
6061
public function addComponent($componentName)
6162
{
62-
try {
63-
if (!$this->isValidComponent($componentName)) {
64-
throw new ComponentException(
65-
sprintf('%s component does not appear to be a valid component.', $componentName)
66-
);
67-
}
68-
69-
$componentClass = $this->configInterface->getComponentByName($componentName);
70-
$this->components[$componentName] = new $componentClass($this->log);
71-
} catch (ComponentException $e) {
72-
throw $e;
73-
}
63+
$this->components[$componentName] = $componentName;
64+
return $this;
7465
}
7566

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

110-
try {
101+
$this->runAllComponents();
102+
return;
103+
}
111104

112-
// Read master yaml
113-
$masterPath = BP . '/app/etc/master.yaml';
114-
if (!file_exists($masterPath)) {
115-
throw new ComponentException("Master YAML does not exist. Please create one in $masterPath");
116-
}
117-
$this->log->logComment(sprintf("Found Master YAML"));
118-
$yamlContents = file_get_contents($masterPath);
119-
$yaml = new Parser();
120-
$master = $yaml->parse($yamlContents);
105+
$this->runIndividualComponents();
106+
}
121107

122-
//print_r($master);
108+
private function runIndividualComponents()
109+
{
110+
try {
123111

124-
// Validate master yaml
125-
$this->validateMasterYaml($master);
112+
// Get the master yaml
113+
$master = $this->getMasterYaml();
126114

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

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

132-
$componentClass = $this->configInterface->getComponentByName($componentAlias);
121+
// Run that component
122+
$this->runComponent($componentAlias, $masterConfig);
123+
}
124+
} catch (ComponentException $e) {
125+
$this->log->logError($e->getMessage());
126+
}
127+
}
133128

134-
/* @var ComponentAbstract $component */
135-
$component = $this->objectManager->create($componentClass);
136-
foreach ($componentConfig['sources'] as $source) {
137-
$component->setSource($source)->process();
138-
}
129+
private function runAllComponents()
130+
{
131+
try {
139132

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

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

150-
// Check if there is a node for this particular environment
151-
if (!isset($componentConfig['env'][$this->getEnvironment()])) {
152-
153-
// If not, continue to next component
154-
$this->log->logComment(
155-
sprintf(
156-
"No '%s' environment specific node for '%s' component",
157-
$this->getEnvironment(),
158-
$component->getComponentName()
159-
)
160-
);
161-
continue;
162-
}
139+
// Run the component in question
140+
$this->runComponent($componentAlias, $componentConfig);
141+
}
142+
} catch (ComponentException $e) {
143+
$this->log->logError($e->getMessage());
144+
}
145+
}
163146

164-
// Check if there are sources for the environment
165-
if (!isset($componentConfig['env'][$this->getEnvironment()]['sources'])) {
166-
167-
// If not continue
168-
$this->log->logComment(
169-
sprintf(
170-
"No '%s' environment specific sources for '%s' component",
171-
$this->getEnvironment(),
172-
$component->getComponentName()
173-
)
174-
);
175-
continue;
176-
}
147+
private function runComponent($componentAlias, $componentConfig)
148+
{
149+
$this->log->logComment(sprintf("Loading component %s", $componentAlias));
177150

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

179-
}
153+
/* @var ComponentAbstract $component */
154+
$component = $this->objectManager->create($componentClass);
155+
foreach ($componentConfig['sources'] as $source) {
156+
$component->setSource($source)->process();
157+
}
180158

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

182-
} catch (ComponentException $e) {
183-
$this->log->logError($e->getMessage());
184-
}
162+
// If not, continue to next component
163+
$this->log->logComment(
164+
sprintf("No environment node for '%s' component", $component->getComponentName())
165+
);
166+
return;
167+
}
185168

169+
// Check if there is a node for this particular environment
170+
if (!isset($componentConfig['env'][$this->getEnvironment()])) {
171+
172+
// If not, continue to next component
173+
$this->log->logComment(
174+
sprintf(
175+
"No '%s' environment specific node for '%s' component",
176+
$this->getEnvironment(),
177+
$component->getComponentName()
178+
)
179+
);
180+
return;
186181
}
182+
183+
// Check if there are sources for the environment
184+
if (!isset($componentConfig['env'][$this->getEnvironment()]['sources'])) {
185+
186+
// If not continue
187+
$this->log->logComment(
188+
sprintf(
189+
"No '%s' environment specific sources for '%s' component",
190+
$this->getEnvironment(),
191+
$component->getComponentName()
192+
)
193+
);
194+
return;
195+
}
196+
197+
}
198+
199+
/**
200+
* @return array
201+
*/
202+
private function getMasterYaml()
203+
{
204+
// Read master yaml
205+
$masterPath = BP . '/app/etc/master.yaml';
206+
if (!file_exists($masterPath)) {
207+
throw new ComponentException("Master YAML does not exist. Please create one in $masterPath");
208+
}
209+
$this->log->logComment(sprintf("Found Master YAML"));
210+
$yamlContents = file_get_contents($masterPath);
211+
$yaml = new Parser();
212+
$master = $yaml->parse($yamlContents);
213+
214+
// Validate master yaml
215+
$this->validateMasterYaml($master);
216+
217+
return $master;
187218
}
188219

189220
/**
@@ -198,6 +229,12 @@ private function isValidComponent($componentName)
198229
$this->log->logQuestion(sprintf("Does the %s component exist?", $componentName));
199230
}
200231
$componentClass = $this->configInterface->getComponentByName($componentName);
232+
233+
if (!$componentClass) {
234+
$this->log->logError(sprintf("The %s component has no class name.", $componentName));
235+
return false;
236+
}
237+
201238
$this->log->logComment(sprintf("The %s component has %s class name.", $componentName, $componentClass));
202239
$component = $this->objectManager->create($componentClass);
203240
if ($component instanceof ComponentAbstract) {

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ php vendor/bin/phpcpd vendor/ctidigital/magento2-configurator/Model/ vendor/ctid
2424
php vendor/bin/phpunit --coverage-clover build/logs/clover.xml vendor/ctidigital/magento2-configurator/Test/Unit/
2525
```
2626

27+
## Getting Started
28+
1. Create a `master.yaml` file in `<mage_root>/app/etc/`. (see `Samples/master.yaml`)
29+
2. Enable Modules `CtiDigital_Configurator`,`FireGento_FastSimpleImport`.
30+
3. Run `bin/magento configurator:run --env="<environment>"`
31+
32+
### Usage
33+
34+
* Listing available components `bin/magento configurator:list`
35+
* Running individual components `bin/magento configurator:run --env="<environment>" --components="config"`
36+
* Extra logs `bin/magento configurator:run --env="<environment>" -v`
37+
2738
## Roadmap for components to do
2839

2940
| Component | Code Written | Tests Written | Sample Files |

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"satooshi/php-coveralls": "^1.0",
2222
"magento/mtf": "dev-develop"
2323
},
24-
"version": "0.2.0-dev",
24+
"version": "0.3.0-dev",
2525
"autoload": {
2626
"files": [ "registration.php" ],
2727
"psr-4": {

0 commit comments

Comments
 (0)