ew_deployer_config/Classes/Config/AbstractConfiguration.php
2024-12-14 14:10:55 +01:00

92 lines
2.2 KiB
PHP

<?php
namespace Evoweb\DeployerConfig\Config;
use Deployer\Deployer;
use Deployer\Task\Task;
use phpDocumentor\Reflection\DocBlockFactory;
use ReflectionMethod;
use function Deployer\get;
use function Deployer\has;
use function Deployer\set;
abstract class AbstractConfiguration
{
protected array $tasks = [];
protected function initializeTasks(): void
{
foreach ($this->tasks as $name => $config) {
$this->registerTask($name, $config);
}
}
protected function registerTask(string $name, array $config): void
{
$methodName = $config['methodName'] ?? '';
if ($methodName === '' || !method_exists(static::class, $methodName)) {
return;
}
$task = new Task($name, $this->$methodName(...));
$description = $this->getFunctionSummary($methodName);
if ($description) {
$task->desc($description);
}
$stages = $config['stages'] ?? [];
if (count($stages)) {
$task->onStage(...$stages);
}
$deployer = Deployer::get();
$deployer->tasks->set($name, $task);
}
protected function getFunctionSummary(string $function): string
{
$summary = '';
try {
$reflector = new ReflectionMethod(Deployment::class, $function);
$comment = $reflector->getDocComment();
if ($comment) {
$summary = DocBlockFactory::createInstance()->create($comment)->getSummary();
}
} catch (\Exception) {
}
return $summary;
}
protected function getTasks(): array
{
return array_keys($this->tasks);
}
protected function get(string $name, $default = null)
{
return get($name, $default) ?? (
isset(Deployer::get()->config[$name])
? Deployer::get()->config->get($name)
: $default
);
}
protected function set(string $name, $value): void
{
set($name, $value);
}
protected function has(string $name): bool
{
return has($name) || Deployer::get()->config->has($name);
}
protected function hasArray(string $name): bool
{
return $this->has($name) && is_array($this->get($name));
}
}