mytube-backend/bin/createApi.php
2024-02-14 20:38:28 +01:00

156 lines
3.9 KiB
PHP

<?php
if (count($argv) !== 6) {
echo 'Use of this Command:' . PHP_EOL
. 'createApi API_IsExternal API_Namespace API_Name CQRS_IsCommand CQRS_Namespace' . PHP_EOL;
die;
}
$projectSourceDirectory = 'src/';
$projectNamespace = 'MyTube';
$apiType = strtolower($argv[1]) === 'true' ? 'External' : 'Internal';
$apiNamespace = $argv[2];
$apiName = $argv[3];
$cqrsType = strtolower($argv[4]) === 'true' ? 'Command' : 'Query';
$cqrsNamespace = $argv[5];
# API Handler
$apiHandlerName = $apiName . 'Handler';
$apiHandlerNamespace = $projectNamespace . '\\API\\' . $apiType . '\\' . $apiNamespace . '\\Handler';
$apiHandlerFilePath = $projectSourceDirectory . 'ApiDomain/' . $apiType . '/' . $apiNamespace . '/src/Handler/' . $apiHandlerName . '.php';
# CQRS
$cqrsFilePath = $projectSourceDirectory . 'HandlingDomain/' . $cqrsNamespace . '/src/Handler/' . $cqrsType . '/' . $apiName . '/';
$cqrsName = $apiName . $cqrsType;
$cqrsVariableName = lcfirst($cqrsName);
$cqrsNamespace = $projectNamespace . '\\Handling\\' . $cqrsNamespace . '\\Handler\\' . $cqrsType . '\\' . $apiName;
# Command / Query
$cqrsPath = $cqrsFilePath . $cqrsName . '.php';
$cqrsUsingNamespace = $cqrsNamespace . '\\' . $cqrsName;
# Handler
$cqrsHandlerName = $cqrsName . 'Handler';
$cqrsHandlerVariableName = lcfirst($cqrsHandlerName);
$cqrsHandlerPath = $cqrsFilePath . $cqrsName . 'Handler' . '.php';
$cqrsHandlerUsingNamespace = $cqrsNamespace . '\\' . $cqrsHandlerName;
# Builder
$cqrsBuilderName = $cqrsName . 'Builder';
$cqrsBuilderVariableName = lcfirst($cqrsBuilderName);
$cqrsBuilderPath = $cqrsFilePath . $cqrsName . 'Builder' . '.php';
$cqrsBuilderUsingNamespace = $cqrsNamespace . '\\' . $cqrsBuilderName;
########## 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);
}
$apiHandlerFileContent = "<?php
declare(strict_types=1);
namespace {$apiHandlerNamespace};
use {$cqrsHandlerUsingNamespace};
use {$cqrsBuilderUsingNamespace};
use Psr\\Http\\Message\\ResponseInterface;
use Psr\\Http\\Message\\ServerRequestInterface;
use Psr\\Http\\Server\\RequestHandlerInterface;
use Bee\\Infrastructure\\Response\\SuccessResponse;
class {$apiHandlerName} implements RequestHandlerInterface
{
public function __construct(
private readonly {$cqrsHandlerName} \${$cqrsHandlerVariableName},
private readonly {$cqrsBuilderName} \${$cqrsBuilderVariableName},
) {
}
public function handle(ServerRequestInterface \$request): ResponseInterface
{
\$data = json_decode(
\$request->getBody()->getContents(),
true
);
\${$cqrsVariableName} = \$this->{$cqrsBuilderVariableName}->build(
\$data
);
\$result = \$this->{$cqrsHandlerVariableName}->execute(\${$cqrsVariableName});
return new SuccessResponse('OK');
}
}
";
writeToFile($apiHandlerFilePath, $apiHandlerFileContent);
$cqrsFileContent = "<?php
declare(strict_types=1);
namespace {$cqrsNamespace};
class {$cqrsName}
{
public function __construct(
#TODO
) {
}
#TODO
}
";
writeToFile($cqrsPath, $cqrsFileContent);
$cqrsHandlerFileContent = "<?php
declare(strict_types=1);
namespace {$cqrsNamespace};
class {$cqrsHandlerName}
{
public function __construct(
#TODO
) {
}
public function execute({$cqrsName} \${$cqrsVariableName}): void
{
}
}
";
writeToFile($cqrsHandlerPath, $cqrsHandlerFileContent);
$cqrsBuilderFileContent = "<?php
declare(strict_types=1);
namespace {$cqrsNamespace};
class {$cqrsBuilderName}
{
public function build(
#TODO
): {$cqrsName} {
return new {$cqrsName}(
#TODO
);
}
}
";
writeToFile($cqrsBuilderPath, $cqrsBuilderFileContent);