mytube-backend/bin/createApi.php
2024-02-23 14:06:49 +01:00

203 lines
5.4 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';
# Response Formatter
$apiResponseFormatterName = $apiName . 'ResponseFormatter';
$apiResponseFormatterNamespace = $projectNamespace . '\\API\\' . $apiType . '\\' . $apiNamespace . '\\ResponseFormatter';
$apiResponseFormatterUsingNamespace = $apiResponseFormatterNamespace . '\\' . $apiResponseFormatterName;
$apiResponseFormatterFilePath = $projectSourceDirectory . 'ApiDomain/' . $apiType . '/' . $apiNamespace . '/src/ResponseFormatter/' . $apiResponseFormatterName . '.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;
# Result
$cqrsResultName = $cqrsName . 'Result';
$cqrsResultVariableName = lcfirst($cqrsResultName);
$cqrsResultPath = $cqrsFilePath . $cqrsResultName . '.php';
$cqrsResultUsingNamespace = $cqrsNamespace . '\\' . $cqrsResultName;
# 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 {$apiResponseFormatterUsingNamespace};
use Psr\\Http\\Message\\ResponseInterface;
use Psr\\Http\\Message\\ServerRequestInterface;
use Psr\\Http\\Server\\RequestHandlerInterface;
use {$projectNamespace}\\Infrastructure\\Response\\SuccessResponse;
class {$apiHandlerName} implements RequestHandlerInterface
{
public function __construct(
private readonly {$cqrsHandlerName} \${$cqrsHandlerVariableName},
private readonly {$cqrsBuilderName} \${$cqrsBuilderVariableName},
private readonly {$apiResponseFormatterName} \$responseFormatter,
) {
}
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(\$this->responseFormatter->format(\$result));
}
}
";
writeToFile($apiHandlerFilePath, $apiHandlerFileContent);
$apiResponseFormatterFileContent = "<?php
declare(strict_types=1);
namespace {$apiResponseFormatterNamespace};
use {$cqrsResultUsingNamespace};
class {$apiResponseFormatterName}
{
public function format({$cqrsResultName} \${$cqrsResultVariableName}): array
{
return [];
}
}
";
writeToFile($apiResponseFormatterFilePath, $apiResponseFormatterFileContent);
$cqrsFileContent = "<?php
declare(strict_types=1);
namespace {$cqrsNamespace};
class {$cqrsName}
{
public function __construct(
#TODO
) {
}
}
";
writeToFile($cqrsPath, $cqrsFileContent);
$cqrsResultFileContent = "<?php
declare(strict_types=1);
namespace {$cqrsNamespace};
class {$cqrsResultName}
{
public function __construct(
#TODO
) {
}
}
";
writeToFile($cqrsResultPath, $cqrsResultFileContent);
$cqrsHandlerFileContent = "<?php
declare(strict_types=1);
namespace {$cqrsNamespace};
class {$cqrsHandlerName}
{
public function __construct(
#TODO
) {
}
public function execute({$cqrsName} \${$cqrsVariableName}): {$cqrsResultName}
{
return new {$cqrsResultName}();
}
}
";
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);