Skip to content

Commit 4afd180

Browse files
committed
#12 : big commit adding DTO, some refacto, get and list command
1 parent 3bb55d9 commit 4afd180

17 files changed

+588
-16
lines changed

Diff for: bin/kloud

+11-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,17 @@ $app->addCommands([
6060
__DIR__ . '/../compose/',
6161
))->setAliases(['upgrade']),
6262

63-
(new Command\Environment\InitCommand()),
63+
(new Command\Environment\InitCommand(
64+
Command\Environment\InitCommand::$defaultName,
65+
)),
66+
67+
(new Command\Environment\Variable\ListCommand(
68+
Command\Environment\Variable\ListCommand::$defaultName,
69+
)),
70+
71+
(new Command\Environment\Variable\GetCommand(
72+
Command\Environment\Variable\GetCommand::$defaultName,
73+
)),
6474
]);
6575

6676
$app->run(new ArgvInput($argv), new ConsoleOutput());

Diff for: src/Domain/Environment/DTO/Context.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
6+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7+
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
8+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9+
10+
class Context implements NormalizableInterface, DenormalizableInterface
11+
{
12+
public ?Deployment $deployment;
13+
public iterable $environmentVariables;
14+
15+
public function __construct(?Deployment $deployment = null)
16+
{
17+
$this->deployment = $deployment;
18+
$this->environmentVariables = [];
19+
}
20+
21+
public function addVariables(EnvironmentVariableInterface ...$variables): void
22+
{
23+
array_push($this->environmentVariables, ...$variables);
24+
}
25+
26+
public function getVariableValue(string $variableName)
27+
{
28+
foreach ($this->environmentVariables as $variable) {
29+
if ($variable instanceof DirectValueEnvironmentVariable) {
30+
if ($variableName === $variable->getVariable()->__toString()) {
31+
$value = $variable->getValue()->__toString();
32+
if (!$value) {
33+
$value = ' ';
34+
}
35+
return $value;
36+
}
37+
} else if ($variable instanceof SecretValueEnvironmentVariable) {
38+
if ($variableName === $variable->getVariable()->__toString()) {
39+
return '**secret**';
40+
}
41+
} else {
42+
if ($variableName === $variable->getVariable()->__toString()) {
43+
return ' ';
44+
}
45+
}
46+
}
47+
}
48+
49+
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
50+
{
51+
$this->deployment = $denormalizer->denormalize($data['deployment'], Deployment::class, $format, $context);
52+
$this->environmentVariables = [];
53+
54+
$parser = new ExpressionParser();
55+
foreach ($data['environmentVariables'] as $variable) {
56+
if (isset($variable['value'])) {
57+
$this->environmentVariables[] = new DirectValueEnvironmentVariable(
58+
new Variable($variable['name']),
59+
$parser->parse($variable['value'])
60+
);
61+
} else if (isset($variable['secret'])) {
62+
$this->environmentVariables[] = new SecretValueEnvironmentVariable(
63+
new Variable($variable['name']),
64+
$variable['secret']
65+
);
66+
} else {
67+
$this->environmentVariables[] = new EnvironmentVariable(
68+
new Variable($variable['name'])
69+
);
70+
}
71+
}
72+
}
73+
74+
public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = [])
75+
{
76+
return [
77+
'deployment' => $normalizer->normalize($this->deployment, $format, $context),
78+
'environment' => (function($variables) {
79+
/** @var EnvironmentVariableInterface $variable */
80+
foreach ($variables as $variable) {
81+
if ($variable instanceof DirectValueEnvironmentVariable) {
82+
yield [
83+
'name' => (string) $variable->getVariable(),
84+
'value' => $variable->getValue(),
85+
];
86+
} else if ($variable instanceof SecretValueEnvironmentVariable){
87+
yield [
88+
'name' => (string) $variable->getVariable(),
89+
'secret' => $variable->getSecret(),
90+
];
91+
} else {
92+
yield [
93+
'name' => (string) $variable->getVariable(),
94+
];
95+
}
96+
}
97+
})($this->environmentVariables),
98+
];
99+
}
100+
}

Diff for: src/Domain/Environment/DTO/Deployment.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
class Deployment
6+
{
7+
public Server $server;
8+
public string $path;
9+
10+
public function __construct(?Server $server = null, ?string $path = null)
11+
{
12+
$this->server = $server;
13+
$this->path = $path;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
class DirectValueEnvironmentVariable implements ValuedEnvironmentVariableInterface
6+
{
7+
private Variable $variable;
8+
/** @var int|string|Variable|Expression */
9+
private $value;
10+
11+
public function __construct(Variable $variable, $value)
12+
{
13+
$this->variable = $variable;
14+
$this->value = $value;
15+
}
16+
17+
public function getVariable(): Variable
18+
{
19+
return $this->variable;
20+
}
21+
22+
public function getValue()
23+
{
24+
return $this->value;
25+
}
26+
}

Diff for: src/Domain/Environment/DTO/EnvironmentVariable.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
class EnvironmentVariable implements EnvironmentVariableInterface
6+
{
7+
private Variable $variable;
8+
9+
public function __construct(Variable $variable)
10+
{
11+
$this->variable = $variable;
12+
}
13+
14+
public function getVariable(): Variable
15+
{
16+
return $this->variable;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
interface EnvironmentVariableInterface
6+
{
7+
public function getVariable(): Variable;
8+
}

Diff for: src/Domain/Environment/DTO/Expression.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
6+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7+
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
8+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9+
10+
final class Expression implements \Stringable, NormalizableInterface, DenormalizableInterface
11+
{
12+
private iterable $elements;
13+
14+
public function __construct(...$elements)
15+
{
16+
$this->elements = $elements;
17+
}
18+
19+
public function __toString()
20+
{
21+
return implode('', array_map(function ($item) {
22+
return (string) $item;
23+
}, $this->elements));
24+
}
25+
26+
public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = [])
27+
{
28+
return (string) $this;
29+
}
30+
31+
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
32+
{
33+
// TODO: Implement denormalize() method.
34+
}
35+
}

Diff for: src/Domain/Environment/DTO/ExpressionParser.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
final class ExpressionParser
6+
{
7+
public function parse(string $value): Expression
8+
{
9+
$offset = 0;
10+
$length = strlen($value);
11+
$elements = [];
12+
while ($offset < $length) {
13+
if (false === preg_match('/([^$]*)(?:\\$\\{([^}]+)\\}|\\$([a-zA-Z0-9_-]+))?/', $value, $matches, 0, $offset)) {
14+
break;
15+
}
16+
17+
if (strlen($matches[1]) > 0) {
18+
$elements[] = $matches[1];
19+
}
20+
21+
if (isset($matches[3])) {
22+
$elements[] = new Variable($matches[3]);
23+
} else if (isset($matches[2])) {
24+
$elements[] = new Variable($matches[2]);
25+
}
26+
27+
$offset += strlen($matches[0]);
28+
}
29+
30+
return new Expression(...$elements);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
class SecretValueEnvironmentVariable implements EnvironmentVariableInterface
6+
{
7+
private Variable $variable;
8+
private string $secret;
9+
10+
public function __construct(Variable $variable, string $secret)
11+
{
12+
$this->variable = $variable;
13+
$this->secret = $secret;
14+
}
15+
16+
public function getVariable(): Variable
17+
{
18+
return $this->variable;
19+
}
20+
21+
public function getSecret(): string
22+
{
23+
return $this->secret;
24+
}
25+
}

Diff for: src/Domain/Environment/DTO/Server.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
class Server
6+
{
7+
public string $hostname;
8+
public int $port;
9+
public string $username;
10+
11+
public function __construct(string $hostname, int $port, string $username)
12+
{
13+
$this->hostname = $hostname;
14+
$this->port = $port;
15+
$this->username = $username;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
interface ValuedEnvironmentVariableInterface extends EnvironmentVariableInterface
6+
{
7+
/** @return int|string|Expression|Variable */
8+
public function getValue();
9+
}

Diff for: src/Domain/Environment/DTO/Variable.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Cloud\Domain\Environment\DTO;
4+
5+
class Variable
6+
{
7+
private string $name;
8+
9+
public function __construct(string $name)
10+
{
11+
$this->name = $name;
12+
}
13+
14+
public function __toString()
15+
{
16+
return $this->name;
17+
}
18+
}

0 commit comments

Comments
 (0)