Skip to content

Commit

Permalink
Work on script command
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Sep 11, 2018
1 parent a168e6c commit 80f4c4b
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/Command/AboutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Phabalicious\Configuration\ConfigurationService;
use Phabalicious\Configuration\HostConfig;
use Phabalicious\Method\TaskContext;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -36,8 +37,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->print($output, $this->getDockerConfig(), 2);
}


$this->getMethods()->runTask('about', $this->getHostConfig());
$context = new TaskContext($this->getConfiguration(), $output);
$this->getMethods()->runTask('about', $this->getHostConfig(), $context);
}

private function print(OutputInterface $output, array $data, int $level = 0)
Expand Down
3 changes: 2 additions & 1 deletion src/Command/ScriptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$script_data = $script_data['script'];
}

$context = new TaskContext();
$context = new TaskContext($this->getConfiguration(), $output);
$context->set('variables', $script_arguments);
$context->set('scriptData', $script_data);

$this->getMethods()->call('script', 'runScript', $this->getHostConfig(), $context);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Configuration/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,13 @@ public function getDockerConfig(string $config_name)
return $data;
}

public function getAllSettings($without = ['hosts', 'dockerHosts'])
{
$copy = $this->settings;
foreach ($without as $key) {
unset($copy[$key]);
}
return $copy;
}

}
2 changes: 1 addition & 1 deletion src/Method/MethodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function callImpl(
if (method_exists($method, $task_name)) {
$method->{$task_name}($configuration, $context);
} elseif (!$optional) {
thrsow new TaskNotFoundInMethodException(
throw new TaskNotFoundInMethodException(
'Could not find task `' . $task_name . '` in method `' . $method_name . '`'
);
}
Expand Down
48 changes: 48 additions & 0 deletions src/Method/ScriptMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Phabalicious\Method;

use Phabalicious\Configuration\ConfigurationService;
use Phabalicious\Configuration\HostConfig;
use Phabalicious\Utilities\Utilities;
use Phabalicious\Validation\ValidationErrorBagInterface;
use Phabalicious\Validation\ValidationService;

class ScriptMethod extends BaseMethod implements MethodInterface
{
Expand All @@ -17,5 +21,49 @@ public function supports(string $method_name): bool
return $method_name = 'script';
}

public function getDefaultConfig(ConfigurationService $configuration_service, array $host_config): array {
return [
'rootFolder' => $configuration_service->getFabfilePath(),
];
}

public function validateConfig(array $config, ValidationErrorBagInterface $errors) {
$service = new ValidationService($config, $errors, 'host-config');
$service->hasKey('rootFolder', 'The root-folder of your configuration.');
}


public function runScript(HostConfig $host_config, TaskContext $context)
{
$commands = $context->get('scriptData', []);
$variables = $context->get('variables', []);
$callbacks = $context->get('callbacks', []);
$environment = $context->get('environment', []);
$root_folder = isset($host_config['siteFolder']) ? $host_config['siteFolder'] ? $host_config['rootFolder'] : '.';

if (!empty($host_config['environment'])) {
$environment = Utilities::mergeData($environment, $host_config['environment']);
}
$variables['host'] = $host_config->raw();
$variables['settings'] = $context->getConfigurationService()->getAllSettings(['hosts', 'dockerHosts']);

$replacements = Utilities::expandVariables($variables);
$commands = Utilities::expandStrings($commands, $replacements);
$commands = Utilities::expandStrings($commands, $replacements);
$environment = Utilities::expandStrings($environment, $replacements);

$this->runScriptImpl($root_folder, $commands, $host_config, $callbacks, $environment, $replacements);

}

private function runScriptImpl(
string $root_folder,
array $commands,
HostConfig $host_config,
array $callbacks = [],
array $environment = [],
array $replacements = []
{
}

}
37 changes: 35 additions & 2 deletions src/Method/TaskContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,50 @@

namespace Phabalicious\Method;

use Phabalicious\Configuration\ConfigurationService;
use Symfony\Component\Console\Output\OutputInterface;

class TaskContext implements TaskContextInterface
{
private $data = [];

private $output;

private $configurationService;

public function __construct(ConfigurationService $service, OutputInterface $output)
{
$this->setOutput($output);
$this->setConfigurationService($service);
}

public function set(string $key, $value)
{
$this->data[$key] = $value;
}

public function get(string $key)
public function get(string $key, $default = null)
{
return isset($this->data[$key]) ? $this->data[$key] : $default;
}

public function setOutput(OutputInterface $output)
{
$this->output = $output;
}

public function getOutput(): OutputInterface
{
return $this->output;
}

public function setConfigurationService(ConfigurationService $service)
{
$this->configurationService = $service;
}

public function getConfigurationService(): ConfigurationService
{
return isset($this->data[$key]) ? $this->data[$key] : null;
return $this->configurationService;
}
}
17 changes: 15 additions & 2 deletions src/Method/TaskContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

namespace Phabalicious\Method;

use Phabalicious\Configuration\ConfigurationService;
use Symfony\Component\Console\Output\OutputInterface;

interface TaskContextInterface {

public function set(string $key, $data);

public function get(string $key);
}
public function get(string $key, $default = null);

public function setOutput(OutputInterface $output);

public function getOutput(): OutputInterface;

public function setConfigurationService(ConfigurationService $service);

public function getConfigurationService(): ConfigurationService;

}

40 changes: 39 additions & 1 deletion src/Utilities/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,42 @@ public static function mergeData(array $data, array $override_data): array
{
return NestedArray::mergeDeep($data, $override_data);
}
}

public static function expandVariables(array $variables): array
{
$result = [];
foreach ($variables as $key => $value) {
self::expandVariablesImpl($key, $value, $result);
}
return $result;
}

private static function expandVariablesImpl(string $prefix, array $variables, array &$result)
{
foreach ($variables as $key => $value) {
if (is_array($value)) {
self::expandVariablesImpl($prefix . '.' . $key, $value, $result);
} else {
$result["%$prefix.$key%"] = (string) ($value);
}
}
}

public static function expandStrings(array $strings, array $replacements): array
{
if (empty($strings)) {
return [];
}
$result = [];
$pattern = implode('|', array_filter(array_keys($replacements), 'preg_quote'));
foreach ($strings as $key => $line) {
$result[$key] = preg_replace_callback('/' . $pattern . '/', function ($found) use ($replacements) {
return $replacements[$found[0]];
}, $line);
}

return $result;
}


}
70 changes: 70 additions & 0 deletions tests/UtilitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Created by PhpStorm.
* User: stephan
* Date: 10.09.18
* Time: 22:03
*/

namespace Phabalicious\Tests;

use Phabalicious\Utilities\Utilities;
use PHPUnit\Framework\TestCase;

class UtilitiesTest extends TestCase
{

public function testExpandCommands()
{
$replacements = [
'%one.two%' => 'Example 1',
'%one.three%' => 'Example 2',
];

$commands = [
'First %one.two% example',
'Second %one.two% %one.two% example',
'This %one.two% %one.three% example'
];
$result = Utilities::expandCommands($commands, $replacements);

$this->assertEquals([
'First Example 1 example',
'Second Example 1 Example 1 example',
'This Example 1 Example 2 example'
], $result);

}

public function testExpandVariables()
{
$data = [
'one' => [
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
],
'two' => [
'one' => 'One',
'two' => 'Two',
'three' => [
'one' => 'One',
'two' => 'Two',
'three' => 'Three'
]
]
];
$result = Utilities::expandVariables($data);

$this->assertEquals([
'%one.one%' => 'One',
'%one.two%' => 'Two',
'%one.three%' => 'Three',
'%two.one%' => 'One',
'%two.two%' => 'Two',
'%two.three.one%' => 'One',
'%two.three.two%' => 'Two',
'%two.three.three%' => 'Three',
], $result);
}
}

0 comments on commit 80f4c4b

Please sign in to comment.