Skip to content

Added YAML operation. Added normalizers support #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions example/Form/definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ inputs:
default: DefaultForm
validators:
empty:
message: Form cannot be empty
message: Form name cannot be empty
ends_with:
message: Name should end with `Form`
options: ['Form']

id:
input: ask
message: What is the ID of the Form ?
default: ${name:(snake_case, strtolower)}
validators:
empty:
message: Form ID cannot be empty

template:
input: confirm
message: Do you want to have a twig template also ?
Expand All @@ -38,7 +46,14 @@ actions:
- action: copy
if: template
source: Form/template/template.html
target: ${path}/template.html.twig
target: ${path}/${id}.template.html.twig

- action: yaml
target: Form/forms.yml
spaces: 2
append:
forms:
${id}: ${path}/${name}

- action: notify
message: Form generated successfully
Expand Down
23 changes: 23 additions & 0 deletions src/App/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use Alexecus\Spawner\Managers\OperationsManager;
use Alexecus\Spawner\Managers\ValidatorsManager;
use Alexecus\Spawner\Managers\InputsManager;
use Alexecus\Spawner\Managers\NormalizerManager;

use Alexecus\Spawner\Operations;
use Alexecus\Spawner\Validators;
use Alexecus\Spawner\Input;
use Alexecus\Spawner\Normalizers;

/**
* Class that bootstraps the generator application
Expand All @@ -36,6 +38,11 @@ class Application
*/
private $inputs;

/**
* @var NormalizerManager
*/
private $normalizers;

/**
* Stores the command
*
Expand All @@ -53,6 +60,7 @@ public function __construct($name = 'Spawner', $version = '1.0')
$this->operations = Container::resolve(OperationsManager::class);
$this->validators = Container::resolve(ValidatorsManager::class);
$this->inputs = Container::resolve(InputsManager::class);
$this->normalizers = Container::resolve(NormalizerManager::class);

$this->init();
}
Expand All @@ -69,13 +77,16 @@ public function init()
$this->addOperation('copy', Operations\Copy::class);
$this->addOperation('notify', Operations\Notify::class);
$this->addOperation('template', Operations\Template::class);
$this->addOperation('yaml', Operations\Yaml::class);

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

$this->addInput('ask', Input\AskInput::class);
$this->addInput('confirm', Input\ConfirmInput::class);

$this->addNormalizer('snake_case', Normalizers\SnakeCase::class);
}

/**
Expand Down Expand Up @@ -135,6 +146,7 @@ public function run()
$command->setOperations($this->operations);
$command->setValidators($this->validators);
$command->setInputs($this->inputs);
$command->setNormalizers($this->normalizers);

$this->console->add($command);
}
Expand Down Expand Up @@ -179,4 +191,15 @@ public function addInput($id, $class)
{
$this->inputs->setInput($id, Container::resolve($class));
}

/**
* Adds a new normalizer class
*
* @param string $id The instances ID
* @param string $class The fully qualified class name
*/
public function addNormalizer($id, $class)
{
$this->normalizers->setNormalizer($id, Container::resolve($class));
}
}
1 change: 1 addition & 0 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class Command extends Base
use CommandInputs;
use CommandOperations;
use CommandValidators;
use CommandNormalizers;

/**
* @var SymfonyStyle
Expand Down
1 change: 1 addition & 0 deletions src/Command/CommandInputs.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function input($id)
$input = $this->inputs->getInput($id);

$input->setOutput($this->style);
$input->setValidators($this->validators);

return $input;
}
Expand Down
40 changes: 40 additions & 0 deletions src/Command/CommandNormalizers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Alexecus\Spawner\Command;

use Alexecus\Spawner\Managers\NormalizerManager;

trait CommandNormalizers
{
/**
* @var NormalizerManager
*/
protected $normalizers;

/**
*
*/
public function setNormalizers(NormalizerManager $normalizers)
{
$this->normalizers = $normalizers;
}

/**
*
*/
public function normalize($id, $value)
{
$normalizer = $this->normalizers->getNormalizer($id);

// check if we have a registered normalizer with the specified ID
// if not just check if the a php method of that name exists and use
// that instead
if ($normalizer) {
$value = $normalizer->normalize($value);
} elseif (function_exists($id)) {
$value = $id($value);
}

return $value;
}
}
60 changes: 60 additions & 0 deletions src/Definition/DefinitionArguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Alexecus\Spawner\Definition;

use ReflectionMethod;
use RuntimeException;

trait DefinitionArguments
{
private function resolveArguments($options, $vars)
{
$result = [];

foreach ($options as $key => $value) {
$key = $this->doReplaceArguments($key, $vars);

if (is_array($value)) {
$result[$key] = $this->resolveArguments($value, $vars);
} elseif (is_string($value)) {
$result[$key] = $this->doReplaceArguments($value, $vars);
}
}

return $result;
}

private function doReplaceArguments($value, $vars)
{
return preg_replace_callback('/\$\{(.*?)\}/', function ($matches) use ($vars) {
$rules = [];
list($string, $match) = $matches;

$definition = explode(':', $match);

if (count($definition) === 2) {
list($match, $rules) = $definition;
}

$value = isset($vars[$match]) ? $vars[$match] : $string;

if (!empty($rules)) {
$ruleset = $this->doExtractRules($rules);

foreach ($ruleset as $rule) {
$value = $this->normalize($rule, $value);
}
}

return $value;
}, $value);
}

private function doExtractRules($rules)
{
$rules = trim($rules, '()');
$ruleset = explode(',', $rules);

return array_map('trim', $ruleset);
}
}
33 changes: 25 additions & 8 deletions src/Definition/DefinitionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DefinitionCommand extends Command
{
use DefinitionInputs;
use DefinitionOperations;
use DefinitionArguments;

private $root;
private $yaml;
Expand Down Expand Up @@ -40,21 +41,37 @@ public function execute(InputInterface $input, OutputInterface $output)
$vars = [];
$inputs = $this->yaml['inputs'] ?? [];

foreach ($inputs as $key => $input) {
if (isset($input['input'])) {
$action = $input['input'];
foreach ($inputs as $key => $options) {
if (isset($options['input'])) {
$action = $options['input'];
$options = $this->resolveArguments($options, $vars);

$vars[$key] = $this->handleInput($action, $input);
$vars[$key] = $this->handleInput($action, $options);
}
}

d($vars);
exit;

$operations = $this->yaml['actions'] ?? [];

foreach ($operations as $key => $operation) {
if (isset($operation['action'])) {
$action = $operation['action'];
foreach ($operations as $key => $options) {
if (isset($options['action'])) {
$action = $options['action'];

$options = $this->resolveArguments($options, $vars);

// Code for the IF directive
// don't execute an operation if an `if` condition is present
if (isset($options['if'])) {
$condition = $options['if'];

if (isset($vars[$condition]) && !$vars[$condition]) {
continue;
}
}

$this->handleOperation($action, $operation, $vars);
// $this->handleOperation($action, $options, $vars);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Definition/DefinitionInputs.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public function handleInput($name, $options)
$return = $instance->perform(...$arguments);

// terminate directive
// if a terminate directive is present then stop script execution
if (!empty($options['terminate']) && !$return) {
exit;
}
}

return $return;
}
Expand Down
37 changes: 4 additions & 33 deletions src/Definition/DefinitionOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Alexecus\Spawner\Definition;

use ReflectionMethod;
use RuntimeException;

use Alexecus\Spawner\Resolver\PathResource;
use \ReflectionMethod;
use SebastianBergmann\CodeCoverage\RuntimeException;

trait DefinitionOperations
{
Expand All @@ -15,18 +16,7 @@ public function handleOperation($name, $options, $vars)
if ($instance) {
$arguments = [];

// Code for the IF directive
if (isset($options['if'])) {
$condition = $options['if'];

if (isset($vars[$condition]) && !$vars[$condition]) {
return;
}
}

$instance = $this->operation($name);
$options = $this->handleReplacements($options, $vars);

$params = (new ReflectionMethod($instance, 'perform'))->getParameters();

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

new \RuntimeException("Missing argument `$key` for definition `$name`");
new RuntimeException("Missing argument `$key` for definition `$name`");
}
}

$instance->perform(...$arguments);
}
}

private function handleReplacements($options, $vars)
{
$result = [];

foreach ($options as $key => $value) {
if (is_array($value)) {
$result[$key] = $this->handleReplacements($value, $vars);
} elseif (is_string($value)) {
$result[$key] = preg_replace_callback('/\$\{(.*?)\}/', function ($matches) use ($vars) {
list($string, $match) = $matches;

return isset($vars[$match]) ? $vars[$match] : $string;
}, $value);
}
}

return $result;
}
}
4 changes: 2 additions & 2 deletions src/Input/AskInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

class AskInput extends AbstractInput
{
public function perform($message, $default, $validations = [])
public function perform($message, $default = null, $validators = [])
{
$question = new Question($message, $default);

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

if ($validator) {
Expand Down
Loading