-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#12 : big commit adding DTO, some refacto, get and list command
- Loading branch information
1 parent
3bb55d9
commit 4afd180
Showing
17 changed files
with
588 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/Domain/Environment/DTO/DirectValueEnvironmentVariable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/Domain/Environment/DTO/SecretValueEnvironmentVariable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Domain/Environment/DTO/ValuedEnvironmentVariableInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.