Skip to content

Commit

Permalink
Merge pull request #10 from dealroadshow/configurable-objects
Browse files Browse the repository at this point in the history
Configurable objects
  • Loading branch information
bpasfinsight authored Nov 23, 2020
2 parents 1da697b + 26310a0 commit f044f45
Show file tree
Hide file tree
Showing 50 changed files with 261 additions and 387 deletions.
43 changes: 16 additions & 27 deletions src/App/AbstractApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
namespace Dealroadshow\K8S\Framework\App;

use Dealroadshow\K8S\APIResourceInterface;
use Dealroadshow\K8S\Framework\Config\ConfigAwareTrait;
use Dealroadshow\K8S\Framework\Core\ManifestFile;
use Dealroadshow\K8S\Framework\Helper\Metadata\MetadataHelperInterface;
use Dealroadshow\K8S\Framework\Helper\Names\NamesHelperInterface;
use Dealroadshow\K8S\Framework\Project\ProjectInterface;

abstract class AbstractApp implements AppInterface
{
protected string $appEnv;
protected MetadataHelperInterface $metadataHelper;
protected NamesHelperInterface $namesHelper;
protected array $files = [];
protected ?ProjectInterface $project;
use ConfigAwareTrait;

public function __construct(MetadataHelperInterface $metadataHelper, NamesHelperInterface $namesHelper)
{
$metadataHelper->setApp($this);
$namesHelper->setApp($this);
protected array $files = [];

$this->metadataHelper = $metadataHelper;
$this->namesHelper = $namesHelper;
public function __construct(
protected MetadataHelperInterface $metadataHelper,
protected NamesHelperInterface $namesHelper
) {
}

public function addManifestFile(string $fileNameWithoutExtension, APIResourceInterface $resource): void
Expand All @@ -32,17 +27,17 @@ public function addManifestFile(string $fileNameWithoutExtension, APIResourceInt
sprintf(
'Filename "%s" for app "%s" is already reserved by "%s" instance',
$fileNameWithoutExtension,
$this->name(),
static::name(),
get_class($this->files[$fileNameWithoutExtension])
)
);
}
$this->files[$fileNameWithoutExtension] = new ManifestFile($fileNameWithoutExtension, $resource);
}

public function env(): string
public function manifestNamePrefix(): string
{
return $this->appEnv;
return static::name();
}

public function manifestFiles(): iterable
Expand All @@ -52,26 +47,20 @@ public function manifestFiles(): iterable

public function metadataHelper(): MetadataHelperInterface
{
$this->metadataHelper->setApp($this);

return $this->metadataHelper;
}

public function namesHelper(): NamesHelperInterface
{
return $this->namesHelper;
}
$this->namesHelper->setApp($this);

public function setEnv(string $env): void
{
$this->appEnv = $env;
}

public function setProject(ProjectInterface $project): void
{
$this->project = $project;
return $this->namesHelper;
}

public function project(): ProjectInterface
public function config(): array
{
return $this->project;
return $this->config;
}
}
14 changes: 6 additions & 8 deletions src/App/AppInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
namespace Dealroadshow\K8S\Framework\App;

use Dealroadshow\K8S\APIResourceInterface;
use Dealroadshow\K8S\Framework\Config\ConfigurableInterface;
use Dealroadshow\K8S\Framework\Core\ManifestFile;
use Dealroadshow\K8S\Framework\Helper\Metadata\MetadataHelperInterface;
use Dealroadshow\K8S\Framework\Helper\Names\NamesHelperInterface;
use Dealroadshow\K8S\Framework\Project\ProjectInterface;

interface AppInterface
interface AppInterface extends ConfigurableInterface
{
public static function name(): string;
public function addManifestFile(string $fileNameWithoutExtension, APIResourceInterface $resource): void;
public function env(): string;
public function setEnv(string $env): void;
public function metadataHelper(): MetadataHelperInterface;
public function name(): string;
public function namesHelper(): NamesHelperInterface;
public function setProject(ProjectInterface $project): void;
public function project(): ProjectInterface;
public function manifestNamePrefix(): string;
public function manifestConfig(string $shortName): array;

/**
* @return ManifestFile[]|iterable
* @return ManifestFile[]
*/
public function manifestFiles(): iterable;
}
31 changes: 14 additions & 17 deletions src/App/AppProcessor.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
<?php


namespace Dealroadshow\K8S\Framework\App;

use Dealroadshow\K8S\Framework\Core\ManifestProcessor;
use Dealroadshow\K8S\Framework\Dumper\Context\ContextInterface;
use Dealroadshow\K8S\Framework\Project\ProjectInterface;
use Dealroadshow\K8S\Framework\Registry\ManifestRegistry;

class AppProcessor
{
private ManifestRegistry $manifestRegistry;
private ManifestProcessor $manifestProcessor;
/**
* @var ContextInterface
*/
private ContextInterface $context;
public function __construct(
private ManifestRegistry $manifestRegistry,
private ManifestProcessor $manifestProcessor,
private ContextInterface $context
) {
}

public function __construct(ManifestRegistry $manifestRegistry, ManifestProcessor $manifestProcessor, ContextInterface $context)
public function processAll(AppInterface ...$apps): void
{
$this->manifestRegistry = $manifestRegistry;
$this->manifestProcessor = $manifestProcessor;
$this->context = $context;
foreach ($apps as $app) {
$this->process($app);
}
}

public function process(AppInterface $app, ProjectInterface $project): void
public function process(AppInterface $app): void
{
$app->setProject($project);
$query = $this->manifestRegistry->query();
$query->app($app);
if($tags = $this->context->includeTags()) {
if ($tags = $this->context->includeTags()) {
$query->includeTags($tags);
}
if($tags = $this->context->excludeTags()) {
if ($tags = $this->context->excludeTags()) {
$query->excludeTags($tags);
}
foreach ($query->execute() as $manifest) {
$this->manifestProcessor->process($manifest, $app);
}
}
}
}
8 changes: 8 additions & 0 deletions src/Config/ConfigAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Dealroadshow\K8S\Framework\Config;

interface ConfigAwareInterface
{
public function setConfig(array $config): void;
}
13 changes: 13 additions & 0 deletions src/Config/ConfigAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Dealroadshow\K8S\Framework\Config;

trait ConfigAwareTrait
{
protected array $config = [];

public function setConfig(array $config): void
{
$this->config = $config;
}
}
8 changes: 8 additions & 0 deletions src/Config/ConfigProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Dealroadshow\K8S\Framework\Config;

interface ConfigProviderInterface
{
public function config(): array;
}
7 changes: 7 additions & 0 deletions src/Config/ConfigurableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Dealroadshow\K8S\Framework\Config;

interface ConfigurableInterface extends ConfigAwareInterface, ConfigProviderInterface
{
}
22 changes: 22 additions & 0 deletions src/Core/AbstractManifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Dealroadshow\K8S\Framework\Core;

use Dealroadshow\K8S\Framework\App\AppInterface;
use Dealroadshow\K8S\Framework\Config\ConfigAwareTrait;

abstract class AbstractManifest implements ManifestInterface
{
use ConfigAwareTrait;

protected AppInterface $app;

public function metadata(MetadataConfigurator $meta): void
{
}

public function setApp(AppInterface $app): void
{
$this->app = $app;
}
}
1 change: 0 additions & 1 deletion src/Core/AppAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
interface AppAwareInterface
{
public function setApp(AppInterface $app): void;
public function app(): AppInterface;
}
20 changes: 0 additions & 20 deletions src/Core/AppAwareTrait.php

This file was deleted.

8 changes: 2 additions & 6 deletions src/Core/ConfigMap/AbstractConfigMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
namespace Dealroadshow\K8S\Framework\Core\ConfigMap;

use Dealroadshow\K8S\Data\Collection\StringMap;
use Dealroadshow\K8S\Framework\Core\AppAwareTrait;
use Dealroadshow\K8S\Framework\Core\ConfigureMetaTrait;
use Dealroadshow\K8S\Framework\Core\AbstractManifest;

abstract class AbstractConfigMap implements ConfigMapInterface
abstract class AbstractConfigMap extends AbstractManifest implements ConfigMapInterface
{
use ConfigureMetaTrait;
use AppAwareTrait;

public function data(StringMap $data): void
{
}
Expand Down
3 changes: 1 addition & 2 deletions src/Core/ConfigMap/ConfigMapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace Dealroadshow\K8S\Framework\Core\ConfigMap;

use Dealroadshow\K8S\Data\Collection\StringMap;
use Dealroadshow\K8S\Framework\Core\AppAwareInterface;
use Dealroadshow\K8S\Framework\Core\ManifestInterface;

interface ConfigMapInterface extends ManifestInterface, AppAwareInterface
interface ConfigMapInterface extends ManifestInterface
{
public function data(StringMap $data): void;
public function binaryData(StringMap $binaryData): void;
Expand Down
10 changes: 0 additions & 10 deletions src/Core/ConfigureMetaTrait.php

This file was deleted.

23 changes: 22 additions & 1 deletion src/Core/Container/Resources/CPU.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,28 @@ public static function cores(int $coresNumber): self
return new self(strval($coresNumber));
}

public function jsonSerialize()
public static function fromString(string $string): self
{
if (is_numeric($string)) {
return static::cores($string);
}

$exception = new \InvalidArgumentException(
sprintf('Invalid CPU string "%s"', $string)
);
if (str_ends_with($string, self::MILLICORES_SUFFIX)) {
$millicoresNumber = rtrim($string, self::MILLICORES_SUFFIX);
if (!is_numeric($millicoresNumber)) {
throw $exception;
}

return self::millicores($millicoresNumber);
}

throw $exception;
}

public function jsonSerialize(): string
{
return $this->toString();
}
Expand Down
Loading

0 comments on commit f044f45

Please sign in to comment.