Add files via upload
This commit is contained in:
parent
886424fc11
commit
aba73b00de
323
bin/createApi.php
Normal file
323
bin/createApi.php
Normal file
@ -0,0 +1,323 @@
|
||||
<?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 = 'Weedkeeper';
|
||||
|
||||
$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 {$cqrsHandlerUsingNamespace};
|
||||
use {$cqrsBuilderUsingNamespace};
|
||||
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 {$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);
|
||||
149
bin/createPipeline.php
Normal file
149
bin/createPipeline.php
Normal file
@ -0,0 +1,149 @@
|
||||
<?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 = 'Bee';
|
||||
|
||||
$pipelineNamespace = $argv[1];
|
||||
$pipelineName = $argv[2];
|
||||
$stepNames = array_slice($argv, 3);
|
||||
|
||||
# Pipeline
|
||||
$pipelineClassName = $pipelineName . 'Pipeline';
|
||||
$pipelineVariableName = lcfirst($pipelineClassName);
|
||||
$pipelineFilePath = $projectSourceDirectory . 'HandlingDomain/' . $pipelineNamespace . '/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);
|
||||
}
|
||||
|
||||
$stepsUsingNamespaces = [];
|
||||
$stepsDeclarations = [];
|
||||
$stepsReferences = [];
|
||||
|
||||
foreach ($steps as $step) {
|
||||
$stepClassName = $step['stepClassName'];
|
||||
$stepVariableName = $step['stepVariableName'];
|
||||
$stepFilePath = $step['stepFilePath'];
|
||||
$stepUsingNamespace = $step['stepUsingNamespace'];
|
||||
|
||||
$stepsUsingNamespaces[] = $stepUsingNamespace . ';';
|
||||
$stepsDeclarations[] = 'private readonly ' . $stepClassName . ' $' . $stepVariableName . ',';
|
||||
$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);
|
||||
$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}
|
||||
{
|
||||
public function __construct(
|
||||
#TODO
|
||||
) {
|
||||
}
|
||||
}
|
||||
";
|
||||
writeToFile($payloadFilePath, $payloadFileContent);
|
||||
Loading…
Reference in New Issue
Block a user