mytube-backend/bin/createPipeline.php
2024-08-04 14:23:33 +00:00

195 lines
5.4 KiB
PHP

<?php
if (count($argv) < 4) {
echo 'Use of this Command:' . PHP_EOL
. 'createPipeline Handling_Namespace Pipeline_Name Step_1_Name Step_2_Name' . PHP_EOL;
die;
}
$projectSourceDirectory = 'src/';
$projectNamespace = 'MyTube';
$pipelineNamespace = $argv[1];
$pipelineName = $argv[2];
$stepNames = array_slice($argv, 3);
# Pipeline
$pipelineClassName = $pipelineName . 'Pipeline';
$pipelineVariableName = lcfirst($pipelineClassName);
$pipelineDirectoryPath = $projectSourceDirectory . 'HandlingDomain/' . $pipelineNamespace . '/';
$pipelineFilePath = $pipelineDirectoryPath . 'src/Pipeline/' . $pipelineName . '/' . $pipelineClassName . '.php';
$pipelineFullNamespace = $projectNamespace . '\\Handling\\' . $pipelineNamespace . '\\Pipeline\\' . $pipelineName;
$pipelineUsingNamespace = $pipelineFullNamespace . '\\' . $pipelineClassName;
# Payload
$payloadClassName = $pipelineName . 'Payload';
$payloadVariableName = lcfirst($payloadClassName);
$payloadFilePath = $projectSourceDirectory . 'HandlingDomain/' . $pipelineNamespace . '/src/Pipeline/' . $pipelineName . '/' . $payloadClassName . '.php';
$payloadFullNamespace = $projectNamespace . '\\Handling\\' . $pipelineNamespace . '\\Pipeline\\' . $pipelineName;
$payloadUsingNamespace = $payloadFullNamespace . '\\' . $payloadClassName;
# Step
$stepsFilePath = $projectSourceDirectory . 'HandlingDomain/' . $pipelineNamespace . '/src/Pipeline/' . $pipelineName . '/Step/';
$stepsFullNamespace = $projectNamespace . '\\Handling\\' . $pipelineNamespace . '\\Pipeline\\' . $pipelineName . '\\Step';
$steps = [];
foreach ($stepNames as $stepName) {
$stepClassName = $stepName . 'Step';
$steps[] = [
'stepClassName' => $stepClassName,
'stepVariableName' => lcfirst($stepClassName),
'stepFilePath' => $stepsFilePath . $stepClassName . '.php',
'stepUsingNamespace' => 'use ' . $payloadFullNamespace . '\\Step\\' . $stepClassName,
];
}
########## WRITE FILES ###############
function writeToFile($path, $content) {
echo 'Writing contents to file ' . $path . PHP_EOL;
$directory = pathinfo($path, PATHINFO_DIRNAME);
if (!is_dir($directory)) {
mkdir($directory, 0755, true);
}
file_put_contents($path, $content);
}
$createNamespaceFiles = !file_exists($pipelineDirectoryPath);
$stepsUsingNamespaces = [];
$stepsDeclarations = [];
$stepsAutoWirings = [];
$stepsReferences = [];
foreach ($steps as $step) {
$stepClassName = $step['stepClassName'];
$stepVariableName = $step['stepVariableName'];
$stepFilePath = $step['stepFilePath'];
$stepUsingNamespace = $step['stepUsingNamespace'];
$stepsUsingNamespaces[] = $stepUsingNamespace . ';';
$stepsDeclarations[] = 'private readonly ' . $stepClassName . ' $' . $stepVariableName . ',';
$stepsAutoWirings[] = $stepClassName . '::class => AutoWiringFactory::class,';
$stepsReferences[] = '$this->' . $stepVariableName . ',';
$stepFileContent = "<?php
declare(strict_types=1);
namespace {$stepsFullNamespace};
use {$payloadUsingNamespace};
use teewurst\\Pipeline\\PipelineInterface;
use teewurst\\Pipeline\\TaskInterface;
class {$stepClassName} implements TaskInterface
{
public function __construct(
#TODO
) {
}
public function __invoke(\$payload, PipelineInterface \$pipeline): void
{
/** @var {$payloadClassName} \${$payloadVariableName} */
\${$payloadVariableName} = \$payload;
\$pipeline->next()(\$payload, \$pipeline);
}
}
";
writeToFile($stepFilePath, $stepFileContent);
}
$stepsUsingNamespace = implode(PHP_EOL, $stepsUsingNamespaces);
$stepsDeclaration = implode(PHP_EOL . ' ', $stepsDeclarations);
$stepsAutoWiring = implode(PHP_EOL . ' ', $stepsAutoWirings);
$stepsReference = implode(PHP_EOL . ' ', $stepsReferences);
$pipelineFileContent = "<?php
declare(strict_types=1);
namespace {$pipelineFullNamespace};
{$stepsUsingNamespace}
use teewurst\\Pipeline\\Pipeline;
class {$pipelineClassName} extends Pipeline
{
public function __construct(
{$stepsDeclaration}
) {
parent::__construct([
{$stepsReference}
]);
}
}
";
writeToFile($pipelineFilePath, $pipelineFileContent);
$payloadFileContent = "<?php
declare(strict_types=1);
namespace {$payloadFullNamespace};
class {$payloadClassName}
{
#TODO
}
";
writeToFile($payloadFilePath, $payloadFileContent);
if ($createNamespaceFiles) {
$serviceManagerFileContent = "<?php
declare(strict_types=1);
use {$pipelineUsingNamespace};
{$stepsUsingNamespace}
use Reinfi\\DependencyInjection\\Factory\\AutoWiringFactory;
use Reinfi\\DependencyInjection\\Factory\\InjectionFactory;
return [
'factories' => [
/// Pipeline
// {$pipelineName}
{$pipelineClassName}::class => AutoWiringFactory::class,
{$stepsAutoWiring}
],
];
";
$serviceManagerFilePath = $pipelineDirectoryPath . 'config/service_manager.php';
writeToFile($serviceManagerFilePath, $serviceManagerFileContent);
$configProviderFileContent = "<?php
declare(strict_types=1);
namespace MyTube\\Handling\\{$pipelineNamespace};
class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => require __DIR__ . './../config/service_manager.php',
];
}
}
";
$configProviderFilePath = $pipelineDirectoryPath . 'src/ConfigProvider.php';
writeToFile($configProviderFilePath, $configProviderFileContent);
}