Skip to content

Commit

Permalink
#12 : big commit adding DTO, some refacto, get and list command
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas43000 committed Sep 30, 2020
1 parent 3bb55d9 commit 4afd180
Show file tree
Hide file tree
Showing 17 changed files with 588 additions and 16 deletions.
12 changes: 11 additions & 1 deletion bin/kloud
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ $app->addCommands([
__DIR__ . '/../compose/',
))->setAliases(['upgrade']),

(new Command\Environment\InitCommand()),
(new Command\Environment\InitCommand(
Command\Environment\InitCommand::$defaultName,
)),

(new Command\Environment\Variable\ListCommand(
Command\Environment\Variable\ListCommand::$defaultName,
)),

(new Command\Environment\Variable\GetCommand(
Command\Environment\Variable\GetCommand::$defaultName,
)),
]);

$app->run(new ArgvInput($argv), new ConsoleOutput());
100 changes: 100 additions & 0 deletions src/Domain/Environment/DTO/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class Context implements NormalizableInterface, DenormalizableInterface
{
public ?Deployment $deployment;
public iterable $environmentVariables;

public function __construct(?Deployment $deployment = null)
{
$this->deployment = $deployment;
$this->environmentVariables = [];
}

public function addVariables(EnvironmentVariableInterface ...$variables): void
{
array_push($this->environmentVariables, ...$variables);
}

public function getVariableValue(string $variableName)
{
foreach ($this->environmentVariables as $variable) {
if ($variable instanceof DirectValueEnvironmentVariable) {
if ($variableName === $variable->getVariable()->__toString()) {
$value = $variable->getValue()->__toString();
if (!$value) {
$value = ' ';
}
return $value;
}
} else if ($variable instanceof SecretValueEnvironmentVariable) {
if ($variableName === $variable->getVariable()->__toString()) {
return '**secret**';
}
} else {
if ($variableName === $variable->getVariable()->__toString()) {
return ' ';
}
}
}
}

public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
{
$this->deployment = $denormalizer->denormalize($data['deployment'], Deployment::class, $format, $context);
$this->environmentVariables = [];

$parser = new ExpressionParser();
foreach ($data['environmentVariables'] as $variable) {
if (isset($variable['value'])) {
$this->environmentVariables[] = new DirectValueEnvironmentVariable(
new Variable($variable['name']),
$parser->parse($variable['value'])
);
} else if (isset($variable['secret'])) {
$this->environmentVariables[] = new SecretValueEnvironmentVariable(
new Variable($variable['name']),
$variable['secret']
);
} else {
$this->environmentVariables[] = new EnvironmentVariable(
new Variable($variable['name'])
);
}
}
}

public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = [])
{
return [
'deployment' => $normalizer->normalize($this->deployment, $format, $context),
'environment' => (function($variables) {
/** @var EnvironmentVariableInterface $variable */
foreach ($variables as $variable) {
if ($variable instanceof DirectValueEnvironmentVariable) {
yield [
'name' => (string) $variable->getVariable(),
'value' => $variable->getValue(),
];
} else if ($variable instanceof SecretValueEnvironmentVariable){
yield [
'name' => (string) $variable->getVariable(),
'secret' => $variable->getSecret(),
];
} else {
yield [
'name' => (string) $variable->getVariable(),
];
}
}
})($this->environmentVariables),
];
}
}
15 changes: 15 additions & 0 deletions src/Domain/Environment/DTO/Deployment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

class Deployment
{
public Server $server;
public string $path;

public function __construct(?Server $server = null, ?string $path = null)
{
$this->server = $server;
$this->path = $path;
}
}
26 changes: 26 additions & 0 deletions src/Domain/Environment/DTO/DirectValueEnvironmentVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

class DirectValueEnvironmentVariable implements ValuedEnvironmentVariableInterface
{
private Variable $variable;
/** @var int|string|Variable|Expression */
private $value;

public function __construct(Variable $variable, $value)
{
$this->variable = $variable;
$this->value = $value;
}

public function getVariable(): Variable
{
return $this->variable;
}

public function getValue()
{
return $this->value;
}
}
18 changes: 18 additions & 0 deletions src/Domain/Environment/DTO/EnvironmentVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

class EnvironmentVariable implements EnvironmentVariableInterface
{
private Variable $variable;

public function __construct(Variable $variable)
{
$this->variable = $variable;
}

public function getVariable(): Variable
{
return $this->variable;
}
}
8 changes: 8 additions & 0 deletions src/Domain/Environment/DTO/EnvironmentVariableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

interface EnvironmentVariableInterface
{
public function getVariable(): Variable;
}
35 changes: 35 additions & 0 deletions src/Domain/Environment/DTO/Expression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class Expression implements \Stringable, NormalizableInterface, DenormalizableInterface
{
private iterable $elements;

public function __construct(...$elements)
{
$this->elements = $elements;
}

public function __toString()
{
return implode('', array_map(function ($item) {
return (string) $item;
}, $this->elements));
}

public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = [])
{
return (string) $this;
}

public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
{
// TODO: Implement denormalize() method.
}
}
32 changes: 32 additions & 0 deletions src/Domain/Environment/DTO/ExpressionParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

final class ExpressionParser
{
public function parse(string $value): Expression
{
$offset = 0;
$length = strlen($value);
$elements = [];
while ($offset < $length) {
if (false === preg_match('/([^$]*)(?:\\$\\{([^}]+)\\}|\\$([a-zA-Z0-9_-]+))?/', $value, $matches, 0, $offset)) {
break;
}

if (strlen($matches[1]) > 0) {
$elements[] = $matches[1];
}

if (isset($matches[3])) {
$elements[] = new Variable($matches[3]);
} else if (isset($matches[2])) {
$elements[] = new Variable($matches[2]);
}

$offset += strlen($matches[0]);
}

return new Expression(...$elements);
}
}
25 changes: 25 additions & 0 deletions src/Domain/Environment/DTO/SecretValueEnvironmentVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

class SecretValueEnvironmentVariable implements EnvironmentVariableInterface
{
private Variable $variable;
private string $secret;

public function __construct(Variable $variable, string $secret)
{
$this->variable = $variable;
$this->secret = $secret;
}

public function getVariable(): Variable
{
return $this->variable;
}

public function getSecret(): string
{
return $this->secret;
}
}
17 changes: 17 additions & 0 deletions src/Domain/Environment/DTO/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

class Server
{
public string $hostname;
public int $port;
public string $username;

public function __construct(string $hostname, int $port, string $username)
{
$this->hostname = $hostname;
$this->port = $port;
$this->username = $username;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

interface ValuedEnvironmentVariableInterface extends EnvironmentVariableInterface
{
/** @return int|string|Expression|Variable */
public function getValue();
}
18 changes: 18 additions & 0 deletions src/Domain/Environment/DTO/Variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

class Variable
{
private string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function __toString()
{
return $this->name;
}
}
Loading

0 comments on commit 4afd180

Please sign in to comment.