Skip to content

Commit b1f781b

Browse files
authored
Added YAML operation. Added normalizers support (#5)
* Added yaml operation * Fixed fatal caused by file read issues * Added support for normalizers * Added support for normalizers
1 parent 0cf9256 commit b1f781b

16 files changed

+320
-53
lines changed

example/Form/definition.yml

+17-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ inputs:
1414
default: DefaultForm
1515
validators:
1616
empty:
17-
message: Form cannot be empty
17+
message: Form name cannot be empty
1818
ends_with:
1919
message: Name should end with `Form`
2020
options: ['Form']
2121

22+
id:
23+
input: ask
24+
message: What is the ID of the Form ?
25+
default: ${name:(snake_case, strtolower)}
26+
validators:
27+
empty:
28+
message: Form ID cannot be empty
29+
2230
template:
2331
input: confirm
2432
message: Do you want to have a twig template also ?
@@ -38,7 +46,14 @@ actions:
3846
- action: copy
3947
if: template
4048
source: Form/template/template.html
41-
target: ${path}/template.html.twig
49+
target: ${path}/${id}.template.html.twig
50+
51+
- action: yaml
52+
target: Form/forms.yml
53+
spaces: 2
54+
append:
55+
forms:
56+
${id}: ${path}/${name}
4257

4358
- action: notify
4459
message: Form generated successfully

src/App/Application.php

+23
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
use Alexecus\Spawner\Managers\OperationsManager;
1212
use Alexecus\Spawner\Managers\ValidatorsManager;
1313
use Alexecus\Spawner\Managers\InputsManager;
14+
use Alexecus\Spawner\Managers\NormalizerManager;
1415

1516
use Alexecus\Spawner\Operations;
1617
use Alexecus\Spawner\Validators;
1718
use Alexecus\Spawner\Input;
19+
use Alexecus\Spawner\Normalizers;
1820

1921
/**
2022
* Class that bootstraps the generator application
@@ -36,6 +38,11 @@ class Application
3638
*/
3739
private $inputs;
3840

41+
/**
42+
* @var NormalizerManager
43+
*/
44+
private $normalizers;
45+
3946
/**
4047
* Stores the command
4148
*
@@ -53,6 +60,7 @@ public function __construct($name = 'Spawner', $version = '1.0')
5360
$this->operations = Container::resolve(OperationsManager::class);
5461
$this->validators = Container::resolve(ValidatorsManager::class);
5562
$this->inputs = Container::resolve(InputsManager::class);
63+
$this->normalizers = Container::resolve(NormalizerManager::class);
5664

5765
$this->init();
5866
}
@@ -69,13 +77,16 @@ public function init()
6977
$this->addOperation('copy', Operations\Copy::class);
7078
$this->addOperation('notify', Operations\Notify::class);
7179
$this->addOperation('template', Operations\Template::class);
80+
$this->addOperation('yaml', Operations\Yaml::class);
7281

7382
$this->addValidator('empty', Validators\EmptyValidator::class);
7483
$this->addValidator('starts_with', Validators\StartsWithValidator::class);
7584
$this->addValidator('ends_with', Validators\EndsWithValidator::class);
7685

7786
$this->addInput('ask', Input\AskInput::class);
7887
$this->addInput('confirm', Input\ConfirmInput::class);
88+
89+
$this->addNormalizer('snake_case', Normalizers\SnakeCase::class);
7990
}
8091

8192
/**
@@ -135,6 +146,7 @@ public function run()
135146
$command->setOperations($this->operations);
136147
$command->setValidators($this->validators);
137148
$command->setInputs($this->inputs);
149+
$command->setNormalizers($this->normalizers);
138150

139151
$this->console->add($command);
140152
}
@@ -179,4 +191,15 @@ public function addInput($id, $class)
179191
{
180192
$this->inputs->setInput($id, Container::resolve($class));
181193
}
194+
195+
/**
196+
* Adds a new normalizer class
197+
*
198+
* @param string $id The instances ID
199+
* @param string $class The fully qualified class name
200+
*/
201+
public function addNormalizer($id, $class)
202+
{
203+
$this->normalizers->setNormalizer($id, Container::resolve($class));
204+
}
182205
}

src/Command/Command.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ abstract class Command extends Base
1313
use CommandInputs;
1414
use CommandOperations;
1515
use CommandValidators;
16+
use CommandNormalizers;
1617

1718
/**
1819
* @var SymfonyStyle

src/Command/CommandInputs.php

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function input($id)
3030
$input = $this->inputs->getInput($id);
3131

3232
$input->setOutput($this->style);
33+
$input->setValidators($this->validators);
3334

3435
return $input;
3536
}

src/Command/CommandNormalizers.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Alexecus\Spawner\Command;
4+
5+
use Alexecus\Spawner\Managers\NormalizerManager;
6+
7+
trait CommandNormalizers
8+
{
9+
/**
10+
* @var NormalizerManager
11+
*/
12+
protected $normalizers;
13+
14+
/**
15+
*
16+
*/
17+
public function setNormalizers(NormalizerManager $normalizers)
18+
{
19+
$this->normalizers = $normalizers;
20+
}
21+
22+
/**
23+
*
24+
*/
25+
public function normalize($id, $value)
26+
{
27+
$normalizer = $this->normalizers->getNormalizer($id);
28+
29+
// check if we have a registered normalizer with the specified ID
30+
// if not just check if the a php method of that name exists and use
31+
// that instead
32+
if ($normalizer) {
33+
$value = $normalizer->normalize($value);
34+
} elseif (function_exists($id)) {
35+
$value = $id($value);
36+
}
37+
38+
return $value;
39+
}
40+
}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Alexecus\Spawner\Definition;
4+
5+
use ReflectionMethod;
6+
use RuntimeException;
7+
8+
trait DefinitionArguments
9+
{
10+
private function resolveArguments($options, $vars)
11+
{
12+
$result = [];
13+
14+
foreach ($options as $key => $value) {
15+
$key = $this->doReplaceArguments($key, $vars);
16+
17+
if (is_array($value)) {
18+
$result[$key] = $this->resolveArguments($value, $vars);
19+
} elseif (is_string($value)) {
20+
$result[$key] = $this->doReplaceArguments($value, $vars);
21+
}
22+
}
23+
24+
return $result;
25+
}
26+
27+
private function doReplaceArguments($value, $vars)
28+
{
29+
return preg_replace_callback('/\$\{(.*?)\}/', function ($matches) use ($vars) {
30+
$rules = [];
31+
list($string, $match) = $matches;
32+
33+
$definition = explode(':', $match);
34+
35+
if (count($definition) === 2) {
36+
list($match, $rules) = $definition;
37+
}
38+
39+
$value = isset($vars[$match]) ? $vars[$match] : $string;
40+
41+
if (!empty($rules)) {
42+
$ruleset = $this->doExtractRules($rules);
43+
44+
foreach ($ruleset as $rule) {
45+
$value = $this->normalize($rule, $value);
46+
}
47+
}
48+
49+
return $value;
50+
}, $value);
51+
}
52+
53+
private function doExtractRules($rules)
54+
{
55+
$rules = trim($rules, '()');
56+
$ruleset = explode(',', $rules);
57+
58+
return array_map('trim', $ruleset);
59+
}
60+
}

src/Definition/DefinitionCommand.php

+25-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class DefinitionCommand extends Command
1313
{
1414
use DefinitionInputs;
1515
use DefinitionOperations;
16+
use DefinitionArguments;
1617

1718
private $root;
1819
private $yaml;
@@ -40,21 +41,37 @@ public function execute(InputInterface $input, OutputInterface $output)
4041
$vars = [];
4142
$inputs = $this->yaml['inputs'] ?? [];
4243

43-
foreach ($inputs as $key => $input) {
44-
if (isset($input['input'])) {
45-
$action = $input['input'];
44+
foreach ($inputs as $key => $options) {
45+
if (isset($options['input'])) {
46+
$action = $options['input'];
47+
$options = $this->resolveArguments($options, $vars);
4648

47-
$vars[$key] = $this->handleInput($action, $input);
49+
$vars[$key] = $this->handleInput($action, $options);
4850
}
4951
}
5052

53+
d($vars);
54+
exit;
55+
5156
$operations = $this->yaml['actions'] ?? [];
5257

53-
foreach ($operations as $key => $operation) {
54-
if (isset($operation['action'])) {
55-
$action = $operation['action'];
58+
foreach ($operations as $key => $options) {
59+
if (isset($options['action'])) {
60+
$action = $options['action'];
61+
62+
$options = $this->resolveArguments($options, $vars);
63+
64+
// Code for the IF directive
65+
// don't execute an operation if an `if` condition is present
66+
if (isset($options['if'])) {
67+
$condition = $options['if'];
68+
69+
if (isset($vars[$condition]) && !$vars[$condition]) {
70+
continue;
71+
}
72+
}
5673

57-
$this->handleOperation($action, $operation, $vars);
74+
// $this->handleOperation($action, $options, $vars);
5875
}
5976
}
6077
}

src/Definition/DefinitionInputs.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public function handleInput($name, $options)
3434
$return = $instance->perform(...$arguments);
3535

3636
// terminate directive
37+
// if a terminate directive is present then stop script execution
3738
if (!empty($options['terminate']) && !$return) {
3839
exit;
39-
}
40+
}
4041

4142
return $return;
4243
}

src/Definition/DefinitionOperations.php

+4-33
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Alexecus\Spawner\Definition;
44

5+
use ReflectionMethod;
6+
use RuntimeException;
7+
58
use Alexecus\Spawner\Resolver\PathResource;
6-
use \ReflectionMethod;
7-
use SebastianBergmann\CodeCoverage\RuntimeException;
89

910
trait DefinitionOperations
1011
{
@@ -15,18 +16,7 @@ public function handleOperation($name, $options, $vars)
1516
if ($instance) {
1617
$arguments = [];
1718

18-
// Code for the IF directive
19-
if (isset($options['if'])) {
20-
$condition = $options['if'];
21-
22-
if (isset($vars[$condition]) && !$vars[$condition]) {
23-
return;
24-
}
25-
}
26-
2719
$instance = $this->operation($name);
28-
$options = $this->handleReplacements($options, $vars);
29-
3020
$params = (new ReflectionMethod($instance, 'perform'))->getParameters();
3121

3222
foreach ($params as $param) {
@@ -45,30 +35,11 @@ public function handleOperation($name, $options, $vars)
4535
continue;
4636
}
4737

48-
new \RuntimeException("Missing argument `$key` for definition `$name`");
38+
new RuntimeException("Missing argument `$key` for definition `$name`");
4939
}
5040
}
5141

5242
$instance->perform(...$arguments);
5343
}
5444
}
55-
56-
private function handleReplacements($options, $vars)
57-
{
58-
$result = [];
59-
60-
foreach ($options as $key => $value) {
61-
if (is_array($value)) {
62-
$result[$key] = $this->handleReplacements($value, $vars);
63-
} elseif (is_string($value)) {
64-
$result[$key] = preg_replace_callback('/\$\{(.*?)\}/', function ($matches) use ($vars) {
65-
list($string, $match) = $matches;
66-
67-
return isset($vars[$match]) ? $vars[$match] : $string;
68-
}, $value);
69-
}
70-
}
71-
72-
return $result;
73-
}
7445
}

src/Input/AskInput.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
class AskInput extends AbstractInput
1313
{
14-
public function perform($message, $default, $validations = [])
14+
public function perform($message, $default = null, $validators = [])
1515
{
1616
$question = new Question($message, $default);
1717

18-
foreach ($validations as $key => $validation) {
18+
foreach ($validators as $key => $validation) {
1919
$validator = $this->validators->getValidator($key);
2020

2121
if ($validator) {

0 commit comments

Comments
 (0)