Skip to content

Commit

Permalink
Improving manifest middlewares, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-buchyn committed Dec 13, 2020
1 parent 5df720d commit 3d70839
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/App/AbstractApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,18 @@ public function config(): array
{
return $this->config;
}

public function readFile(string $filePath): string
{
$appClass = new \ReflectionObject($this);
$appDir = dirname($appClass->getFileName());
$path = $appDir.'/Resources/'.ltrim($filePath, '/');
if (!file_exists($path)) {
throw new \InvalidArgumentException(
sprintf('File "%s" does not exist', $path)
);
}

return file_get_contents($path);
}
}
6 changes: 6 additions & 0 deletions src/Core/AbstractManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

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

abstract class AbstractManifest implements ManifestInterface
{
use ConfigAwareTrait;

protected AppInterface $app;

public function fileNameWithoutExtension(): string
{
return static::shortName().'.'.Str::asDNSSubdomain(static::kind());
}

public function metadata(MetadataConfigurator $meta): void
{
}
Expand Down
6 changes: 6 additions & 0 deletions src/Core/ConfigMap/AbstractConfigMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dealroadshow\K8S\Framework\Core\ConfigMap;

use Dealroadshow\K8S\API\ConfigMap;
use Dealroadshow\K8S\Data\Collection\StringMap;
use Dealroadshow\K8S\Framework\Core\AbstractManifest;

Expand All @@ -14,4 +15,9 @@ public function data(StringMap $data): void
public function binaryData(StringMap $binaryData): void
{
}

public static function kind(): string
{
return ConfigMap::KIND;
}
}
5 changes: 5 additions & 0 deletions src/Core/CronJob/AbstractCronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public function suspend(): ?bool
public function configureCronJob(CronJob $cronJob): void
{
}

public static function kind(): string
{
return CronJob::KIND;
}
}
5 changes: 5 additions & 0 deletions src/Core/Deployment/AbstractDeployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function imagePullSecrets(ImagePullSecretsConfigurator $secrets): void
{
}

public static function kind(): string
{
return Deployment::KIND;
}

public function nodeSelector(StringMap $nodeSelector): void
{
}
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Ingress/AbstractIngress.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function backend(IngressBackendFactory $factory): ?IngressBackend
public function configureIngress(Ingress $ingress): void
{
}

public static function kind(): string
{
return Ingress::KIND;
}
}
5 changes: 5 additions & 0 deletions src/Core/Job/AbstractJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public function configurePodSpec(PodSpec $spec): void
public function configureJob(Job $job): void
{
}

public static function kind(): string
{
return Job::KIND;
}
}
1 change: 1 addition & 0 deletions src/Core/ManifestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

interface ManifestInterface extends AppAwareInterface, ConfigAwareInterface, MetadataAwareInterface
{
public static function kind(): string;
public static function shortName(): string;
public function fileNameWithoutExtension(): string;
}
6 changes: 6 additions & 0 deletions src/Core/Secret/AbstractSecret.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dealroadshow\K8S\Framework\Core\Secret;

use Dealroadshow\K8S\API\Secret;
use Dealroadshow\K8S\Data\Collection\StringMap;
use Dealroadshow\K8S\Framework\Core\AbstractManifest;

Expand All @@ -14,4 +15,9 @@ public function data(StringMap $data): void
public function stringData(StringMap $stringData): void
{
}

public static function kind(): string
{
return Secret::KIND;
}
}
5 changes: 5 additions & 0 deletions src/Core/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function type(ServiceTypeConfigurator $type): void
public function configureService(Service $service): void
{
}

public static function kind(): string
{
return Service::KIND;
}
}
16 changes: 15 additions & 1 deletion src/Middleware/ManifestMethodMiddlewareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ interface ManifestMethodMiddlewareInterface
{
const NO_RETURN_VALUE = 'MANIFEST_MIDDLEWARE_NO_RETURN_VALUE';

public function beforeMethodCall(ManifestInterface $manifest, string $methodName, array $params, &$returnValue);
public function beforeMethodCall(
ManifestInterface $manifest,
string $methodName,
array $params,
mixed &$returnValue
);

public function afterMethodCall(
ManifestInterface $manifest,
string $methodName,
array $params,
mixed $returnedValue,
mixed &$returnValue
);

public function supports(ManifestInterface $manifest, string $methodName, array $params): bool;
public static function priority(): int;
}
15 changes: 15 additions & 0 deletions src/Middleware/ManifestMiddlewareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ public function beforeMethodCall(ManifestInterface $manifest, string $methodName

return ManifestMethodMiddlewareInterface::NO_RETURN_VALUE;
}

public function afterMethodCall(ManifestInterface $manifest, string $methodName, array $params, mixed $returnedValue): mixed
{
foreach ($this->middlewares as $middleware) {
if ($middleware->supports($manifest, $methodName, $params)) {
$returnValue = ManifestMethodMiddlewareInterface::NO_RETURN_VALUE;
$middleware->afterMethodCall($manifest, $methodName, $params, $returnedValue, $returnValue);
if (ManifestMethodMiddlewareInterface::NO_RETURN_VALUE !== $returnValue) {
return $returnValue;
}
}
}

return ManifestMethodMiddlewareInterface::NO_RETURN_VALUE;
}
}
26 changes: 22 additions & 4 deletions src/Proxy/ManifestProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function makeProxy(ManifestInterface $manifest): ManifestInterface
{
$factory = new ProxyFactory();

$closure = function(
$prefixClosure = function(
ManifestInterface $proxy,
ManifestInterface $wrapped,
string $method,
Expand All @@ -32,15 +32,33 @@ public function makeProxy(ManifestInterface $manifest): ManifestInterface
return $result;
};

$closures = [];
$suffixClosure = function(
ManifestInterface $proxy,
ManifestInterface $wrapped,
string $method,
array $params,
mixed $returnedValue,
bool &$returnEarly
) {
$result = $this->middlewareService->afterMethodCall($wrapped, $method, $params, $returnedValue);
if (ManifestMethodMiddlewareInterface::NO_RETURN_VALUE !== $result) {
$returnEarly = true;
}

return $result;
};

$prefixClosures = [];
$suffixClosures = [];
$class = new \ReflectionClass($manifest);
foreach ($class->getMethods() as $method) {
if ($method->isFinal() || $method->isPrivate() || $method->isConstructor() || $method->isDestructor()) {
continue;
}
$closures[$method->getName()] = $closure;
$prefixClosures[$method->getName()] = $prefixClosure;
$suffixClosures[$method->getName()] = $suffixClosure;
}

return $factory->createProxy($manifest, $closures);
return $factory->createProxy($manifest, $prefixClosures, $suffixClosures);
}
}
81 changes: 81 additions & 0 deletions src/Util/Str.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Dealroadshow\K8S\Framework\Util;

class Str
{
public static function asClassName(string $str): string
{
$str = ucwords($str, " \t\r\n\f\v-_");

return preg_replace('/[\s\-_]+/', '', $str);
}

public static function camelCased(string $str): string
{
return lcfirst(static::asClassName($str));
}

public static function withoutSuffix(string $str, string $suffix): string
{
return str_ends_with($str, $suffix)
? substr($str, 0, -strlen($suffix))
: $str;
}

public static function withSuffix(string $className, string $suffix): string
{
return static::withoutSuffix($className, $suffix).$suffix;
}

public static function asDirName(string $str, string $suffix = null): string
{
$className = self::asClassName($str);

return $suffix ? self::withoutSuffix($className, $suffix) : $className;
}

public static function asNamespace(object $object): string
{
$reflection = new \ReflectionObject($object);
$namespace = $reflection->getNamespaceName();

return trim($namespace, '\\');
}

public static function asDir(object $object): string
{
$reflection = new \ReflectionObject($object);

return dirname($reflection->getFileName());
}

public static function asDNSSubdomain(string $str): string
{
$camel2dash = strtolower(
preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $str)
);
$valid = preg_replace('/([^\w\-]|[_])+/', '', $camel2dash);

if (0 === strlen($valid)) {
throw new \InvalidArgumentException(
sprintf('String "%s" cannot be converted DNS subdomain representation', $str)
);
}
if (253 < strlen($valid)) {
throw new \InvalidArgumentException(
sprintf(
'String "%s" DNS subdomain representation is too long (must be less than 253 characters)',
$str
)
);
}

return $valid;
}

public static function isValidDNSSubdomain(string $str): bool
{
return 253 > strlen($str) && 0 !== preg_match('/^[a-z0-9]+[a-z0-9\-.]+[a-z0-9]$/', $str);
}
}

0 comments on commit 3d70839

Please sign in to comment.