diff --git a/bin/createApi.php b/bin/createApi.php new file mode 100644 index 0000000..740d9b0 --- /dev/null +++ b/bin/createApi.php @@ -0,0 +1,323 @@ + require __DIR__ . '/./../config/service_manager.php', + 'routes' => require __DIR__ . '/./../config/routes.php', + ]; + } +} +"; + writeToFile($configProviderFilePath, $configProviderFileContent); + + // Service Manager + $serviceManagerFilePath = $apiNamespacePath . '/config/service_manager.php'; + $serviceManagerFileContent = " [ + // 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 = " '{$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 = " require __DIR__ . '/./../config/service_manager.php', + ]; + } +} +"; + writeToFile($configProviderFilePath, $configProviderFileContent); + + // Service Manager + $serviceManagerFilePath = $cqrsNamespacePath . '/config/service_manager.php'; + $serviceManagerFileContent = " [ + /// CQRS + // {$apiName} + {$cqrsBuilderName}::class => AutoWiringFactory::class, + {$cqrsHandlerName}::class => AutoWiringFactory::class, + ], +]; + +"; + writeToFile($serviceManagerFilePath, $serviceManagerFileContent); +} + +########## WRITE FILES ############### +$apiHandlerFileContent = "getBody()->getContents(), + true + ); + + \${$cqrsVariableName} = \$this->{$cqrsBuilderVariableName}->build( + \$data + ); + \$result = \$this->{$cqrsHandlerVariableName}->execute(\${$cqrsVariableName}); + + return new SuccessResponse(\$this->responseFormatter->format(\$result)); + } +} +"; +writeToFile($apiHandlerFilePath, $apiHandlerFileContent); + +$apiResponseFormatterFileContent = " $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 = "next()(\$payload, \$pipeline); + } +} +"; + writeToFile($stepFilePath, $stepFileContent); +} + +$stepsUsingNamespace = implode(PHP_EOL, $stepsUsingNamespaces); +$stepsDeclaration = implode(PHP_EOL . ' ', $stepsDeclarations); +$stepsReference = implode(PHP_EOL . ' ', $stepsReferences); + + + +$pipelineFileContent = "