generated from flo/template-backend
320 lines
8.5 KiB
PHP
320 lines
8.5 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 = 'Template';
|
|
|
|
$apiType = strtolower($argv[1]) === 'true' ? 'External' : 'Internal';
|
|
$apiNamespace = $argv[2];
|
|
$apiName = $argv[3];
|
|
$cqrsType = strtolower($argv[4]) === 'true' ? 'Command' : 'Query';
|
|
$cqrsNamespace = $argv[5];
|
|
|
|
## API
|
|
$apiNamespacePath = $projectSourceDirectory . 'ApiDomain/' . $apiType . '/' . $apiNamespace;
|
|
$apiNamespace = $projectNamespace . '\\API\\' . $apiType . '\\' . $apiNamespace;
|
|
|
|
# API Handler
|
|
$apiHandlerName = $apiName . 'Handler';
|
|
$apiHandlerNamespace = $apiNamespace . '\\Handler';
|
|
$apiHandlerUsingNamespace = $apiHandlerNamespace . '\\' . $apiHandlerName;
|
|
$apiHandlerFilePath = $apiNamespacePath . '/src/Handler/' . $apiHandlerName . '.php';
|
|
|
|
# API Response Formatter
|
|
$apiResponseFormatterName = $apiName . 'ResponseFormatter';
|
|
$apiResponseFormatterNamespace = $apiNamespace . '\\ResponseFormatter';
|
|
$apiResponseFormatterUsingNamespace = $apiResponseFormatterNamespace . '\\' . $apiResponseFormatterName;
|
|
$apiResponseFormatterFilePath = $apiNamespacePath . '/src/ResponseFormatter/' . $apiResponseFormatterName . '.php';
|
|
|
|
## CQRS
|
|
$cqrsNamespacePath = $projectSourceDirectory . 'HandlingDomain/' . $cqrsNamespace;
|
|
$cqrsFilePath = $cqrsNamespacePath . '/src/Handler/' . $cqrsType . '/' . $apiName . '/';
|
|
$cqrsName = $apiName . $cqrsType;
|
|
$cqrsVariableName = lcfirst($cqrsName);
|
|
$cqrsNamespace = $projectNamespace . '\\Handling\\' . $cqrsNamespace;
|
|
$cqrsHandlerNamespace = $cqrsNamespace . '\\Handler\\' . $cqrsType . '\\' . $apiName;
|
|
|
|
# Command / Query
|
|
$cqrsPath = $cqrsFilePath . $cqrsName . '.php';
|
|
|
|
# Handler
|
|
$cqrsHandlerName = $cqrsName . 'Handler';
|
|
$cqrsHandlerVariableName = lcfirst($cqrsHandlerName);
|
|
$cqrsHandlerPath = $cqrsFilePath . $cqrsName . 'Handler' . '.php';
|
|
$cqrsHandlerUsingNamespace = $cqrsHandlerNamespace . '\\' . $cqrsHandlerName;
|
|
|
|
# Result
|
|
$cqrsResultName = $cqrsName . 'Result';
|
|
$cqrsResultVariableName = lcfirst($cqrsResultName);
|
|
$cqrsResultPath = $cqrsFilePath . $cqrsName . 'Result' . '.php';
|
|
$cqrsResultUsingNamespace = $cqrsHandlerNamespace . '\\' . $cqrsResultName;
|
|
|
|
# Builder
|
|
$cqrsBuilderName = $cqrsName . 'Builder';
|
|
$cqrsBuilderVariableName = lcfirst($cqrsBuilderName);
|
|
$cqrsBuilderPath = $cqrsFilePath . $cqrsName . 'Builder' . '.php';
|
|
$cqrsBuilderUsingNamespace = $cqrsHandlerNamespace . '\\' . $cqrsBuilderName;
|
|
|
|
|
|
########## COMMON ################
|
|
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);
|
|
}
|
|
|
|
########## CREATE CONFIGS #############
|
|
if (!is_dir($apiNamespacePath)) {
|
|
// API Namespace is new, create Configs
|
|
// Config Provider
|
|
$configProviderFilePath = $apiNamespacePath . '/src/ConfigProvider.php';
|
|
$configProviderFileContent = "<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace {$apiNamespace};
|
|
|
|
class ConfigProvider
|
|
{
|
|
public function __invoke(): array
|
|
{
|
|
return [
|
|
'dependencies' => require __DIR__ . '/./../config/service_manager.php',
|
|
'routes' => require __DIR__ . '/./../config/routes.php',
|
|
];
|
|
}
|
|
}
|
|
";
|
|
writeToFile($configProviderFilePath, $configProviderFileContent);
|
|
|
|
// Service Manager
|
|
$serviceManagerFilePath = $apiNamespacePath . '/config/service_manager.php';
|
|
$serviceManagerFileContent = "<?php
|
|
|
|
use {$apiHandlerUsingNamespace};
|
|
use {$apiResponseFormatterUsingNamespace};
|
|
use Reinfi\DependencyInjection\Factory\AutoWiringFactory;
|
|
|
|
return [
|
|
'factories' => [
|
|
// Handler
|
|
{$apiHandlerName}::class => AutoWiringFactory::class,
|
|
|
|
// Response Formatter
|
|
{$apiResponseFormatterName}::class => AutoWiringFactory::class,
|
|
],
|
|
];
|
|
";
|
|
writeToFile($serviceManagerFilePath, $serviceManagerFileContent);
|
|
|
|
// Routes
|
|
$routeName = strtolower($argv[2]) . '.' . strtolower($argv[3]);
|
|
$routePath = strtolower($argv[2]) . '/' . strtolower($argv[3]);
|
|
$routesFilePath = $apiNamespacePath . '/config/routes.php';
|
|
$routesFileContent = "<?php
|
|
|
|
use {$apiHandlerUsingNamespace};
|
|
|
|
return [
|
|
[
|
|
'name' => '{$routeName}',
|
|
'path' => '/api/{$routePath}/',
|
|
'allowed_methods' => ['POST'],
|
|
'middleware' => [
|
|
{$apiHandlerName}::class,
|
|
],
|
|
],
|
|
];
|
|
";
|
|
writeToFile($routesFilePath, $routesFileContent);
|
|
}
|
|
|
|
if (!is_dir($cqrsNamespacePath)) {
|
|
// CQRS Namespace is new, create Configs
|
|
// API Namespace is new, create Configs
|
|
// Config Provider
|
|
$configProviderFilePath = $cqrsNamespacePath . '/src/ConfigProvider.php';
|
|
$configProviderFileContent = "<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace {$cqrsNamespace};
|
|
|
|
class ConfigProvider
|
|
{
|
|
public function __invoke(): array
|
|
{
|
|
return [
|
|
'dependencies' => require __DIR__ . '/./../config/service_manager.php',
|
|
];
|
|
}
|
|
}
|
|
";
|
|
writeToFile($configProviderFilePath, $configProviderFileContent);
|
|
|
|
// Service Manager
|
|
$serviceManagerFilePath = $cqrsNamespacePath . '/config/service_manager.php';
|
|
$serviceManagerFileContent = "<?php
|
|
|
|
use {$cqrsBuilderUsingNamespace};
|
|
use {$cqrsHandlerUsingNamespace};
|
|
use Reinfi\DependencyInjection\Factory\AutoWiringFactory;
|
|
|
|
return [
|
|
'factories' => [
|
|
/// CQRS
|
|
// {$apiName}
|
|
{$cqrsBuilderName}::class => AutoWiringFactory::class,
|
|
{$cqrsHandlerName}::class => AutoWiringFactory::class,
|
|
],
|
|
];
|
|
";
|
|
writeToFile($serviceManagerFilePath, $serviceManagerFileContent);
|
|
}
|
|
|
|
########## WRITE FILES ###############
|
|
$apiHandlerFileContent = "<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace {$apiHandlerNamespace};
|
|
|
|
use {$apiResponseFormatterUsingNamespace};
|
|
use {$cqrsBuilderUsingNamespace};
|
|
use {$cqrsHandlerUsingNamespace};
|
|
use {$projectNamespace}\\Infrastructure\\Request\\Middleware\\AnalyzeBodyMiddleware;
|
|
use {$projectNamespace}\\Infrastructure\\Response\\SuccessResponse;
|
|
use Psr\\Http\\Message\\ResponseInterface;
|
|
use Psr\\Http\\Message\\ServerRequestInterface;
|
|
use Psr\\Http\\Server\\RequestHandlerInterface;
|
|
|
|
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 = \$request->getAttribute(AnalyzeBodyMiddleware::JSON_DATA);
|
|
|
|
\${$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 {$cqrsHandlerNamespace};
|
|
|
|
class {$cqrsName}
|
|
{
|
|
public function __construct(
|
|
#TODO
|
|
) {
|
|
}
|
|
|
|
#TODO
|
|
}
|
|
";
|
|
writeToFile($cqrsPath, $cqrsFileContent);
|
|
|
|
$cqrsHandlerFileContent = "<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace {$cqrsHandlerNamespace};
|
|
|
|
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 {$cqrsHandlerNamespace};
|
|
|
|
class {$cqrsBuilderName}
|
|
{
|
|
public function build(
|
|
#TODO
|
|
): {$cqrsName} {
|
|
return new {$cqrsName}(
|
|
#TODO
|
|
);
|
|
}
|
|
}
|
|
";
|
|
writeToFile($cqrsBuilderPath, $cqrsBuilderFileContent);
|
|
|
|
$cqrsResultFileContent = "<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace {$cqrsHandlerNamespace};
|
|
|
|
class {$cqrsResultName}
|
|
{
|
|
public function __construct(
|
|
#TODO
|
|
) {
|
|
}
|
|
}
|
|
";
|
|
writeToFile($cqrsResultPath, $cqrsResultFileContent);
|