mytube-backend/bin/createApi.php
2024-02-24 20:31:55 +01:00

314 lines
8.2 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];
$apiDirectoryPath = $projectSourceDirectory . 'ApiDomain/' . $apiType . '/' . $apiNamespace . '/';
$cqrsDirectoryPath = $projectSourceDirectory . 'HandlingDomain/' . $cqrsNamespace . '/';
# API Handler
$apiHandlerName = $apiName . 'Handler';
$apiHandlerNamespace = $projectNamespace . '\\API\\' . $apiType . '\\' . $apiNamespace . '\\Handler';
$apiHandlerUsingNamespace = $apiHandlerNamespace . '\\' . $apiHandlerName;
$apiHandlerFilePath = $apiDirectoryPath . 'src/Handler/' . $apiHandlerName . '.php';
# Response Formatter
$apiResponseFormatterName = $apiName . 'ResponseFormatter';
$apiResponseFormatterNamespace = $projectNamespace . '\\API\\' . $apiType . '\\' . $apiNamespace . '\\ResponseFormatter';
$apiResponseFormatterUsingNamespace = $apiResponseFormatterNamespace . '\\' . $apiResponseFormatterName;
$apiResponseFormatterFilePath = $apiDirectoryPath . 'src/ResponseFormatter/' . $apiResponseFormatterName . '.php';
# CQRS
$cqrsFilePath = $cqrsDirectoryPath . '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);
}
if (!file_exists($apiDirectoryPath)) {
$routesFileContent = "<?php
declare(strict_types=1);
use {$apiHandlerUsingNamespace};
return [
[
'name' => 'TODO.TODO',
'path' => '/api/TODO/TODO[/]',
'allowed_methods' => ['POST'],
'middleware' => [
{$apiHandlerName}::class,
],
],
];
";
$routesFilePath = $apiDirectoryPath . 'config/routes.php';
writeToFile($routesFilePath, $routesFileContent);
$serviceManagerFileContent = "<?php
declare(strict_types=1);
use {$apiHandlerUsingNamespace};
use {$apiResponseFormatterUsingNamespace};
use Reinfi\DependencyInjection\Factory\AutoWiringFactory;
return [
'factories' => [
// Handler
{$apiHandlerName}::class => AutoWiringFactory::class,
// Response Formatter
{$apiResponseFormatterName}::class => AutoWiringFactory::class,
],
];
";
$serviceManagerFilePath = $apiDirectoryPath . 'config/service_manager.php';
writeToFile($serviceManagerFilePath, $serviceManagerFileContent);
$configProviderFileContent = "<?php
declare(strict_types=1);
namespace MyTube\API\{$apiType}\{$apiNamespace};
class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => require __DIR__ . './../config/service_manager.php',
'routes' => require __DIR__ . '/./../config/routes.php',
];
}
}
";
$configProviderFilePath = $apiDirectoryPath . 'src/ConfigProvider.php';
writeToFile($configProviderFilePath, $configProviderFileContent);
}
if (!file_exists($cqrsDirectoryPath)) {
$serviceManagerFileContent = "<?php
declare(strict_types=1);
use {$cqrsHandlerUsingNamespace};
use {$cqrsBuilderUsingNamespace};
use Reinfi\DependencyInjection\Factory\AutoWiringFactory;
use Reinfi\DependencyInjection\Factory\InjectionFactory;
return [
'factories' => [
/// CQRS
// {$apiName}
{$cqrsBuilderName}::class => AutoWiringFactory::class,
{$cqrsHandlerName}::class => AutoWiringFactory::class,
],
];
";
$serviceManagerFilePath = $cqrsDirectoryPath . 'config/service_manager.php';
writeToFile($serviceManagerFilePath, $serviceManagerFileContent);
$configProviderFileContent = "<?php
declare(strict_types=1);
namespace MyTube\Handling\{$cqrsNamespace};
class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => require __DIR__ . './../config/service_manager.php',
];
}
}
";
$configProviderFilePath = $cqrsDirectoryPath . 'src/ConfigProvider.php';
writeToFile($configProviderFilePath, $configProviderFileContent);
}
$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);