From edf1817bac8fb70ad20fe4dc68af655d313f6994 Mon Sep 17 00:00:00 2001 From: Flo Date: Sat, 24 Aug 2024 20:21:55 +0000 Subject: [PATCH] intermediate commit --- .github/workflows/create branch on issue.yml | 40 ----------------- .github/workflows/php.yml | 39 ----------------- bin/createApi.php | 18 +++----- bin/createPipeline.php | 8 ++-- config/pipeline.php | 2 + .../Request/config/service_manager.php | 6 +-- .../src/Middleware/AnalyzeBodyMiddleware.php | 43 +++++++++++++++++++ 7 files changed, 60 insertions(+), 96 deletions(-) delete mode 100644 .github/workflows/create branch on issue.yml delete mode 100644 .github/workflows/php.yml create mode 100644 src/Infrastructure/Request/src/Middleware/AnalyzeBodyMiddleware.php diff --git a/.github/workflows/create branch on issue.yml b/.github/workflows/create branch on issue.yml deleted file mode 100644 index 789cd5a..0000000 --- a/.github/workflows/create branch on issue.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: Create Branch on Issue Transition to In Progress - -on: - issues: - types: - - edited - -jobs: - create-branch: - if: github.event.changes.issue.labels - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Get issue details - run: | - issue_title=$(jq -r ".issue.number" "$GITHUB_EVENT_PATH") - previous_labels=$(jq -r ".changes.labels.from" "$GITHUB_EVENT_PATH") - current_labels=$(jq -r ".issue.labels | map(.name) | join(\", \")" "$GITHUB_EVENT_PATH") - branch_name="" - - # Check if the issue transitioned from 'Todo' to 'In Progress' - if [[ "$previous_labels" == *"Todo"* && "$current_labels" == *"In Progress"* ]]; then - project_name="hp-be" # Verwende "hp-be" als Projektnamen - branch_name="${project_name}-${issue_title}" - echo "Branch name: $branch_name" - fi - - echo "::set-output name=branch_name::$branch_name" - shell: bash - - - name: Create and switch to new branch - run: | - branch_name="${{ steps.create-branch.outputs.branch_name }}" - if [ -n "$branch_name" ]; then - git checkout -b "$branch_name" - git push origin "$branch_name" - fi - working-directory: ${{ github.workspace }} diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml deleted file mode 100644 index 1ef7894..0000000 --- a/.github/workflows/php.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: PHP Composer - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -permissions: - contents: read - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Validate composer.json and composer.lock - run: composer validate --strict - - - name: Cache Composer packages - id: composer-cache - uses: actions/cache@v3 - with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-php- - - - name: Install dependencies - run: composer install --prefer-dist --no-progress - - # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" - # Docs: https://getcomposer.org/doc/articles/scripts.md - - # - name: Run test suite - # run: composer run-script test diff --git a/bin/createApi.php b/bin/createApi.php index 7a70996..8c52026 100644 --- a/bin/createApi.php +++ b/bin/createApi.php @@ -109,12 +109,11 @@ return [ 'factories' => [ // Handler {$apiHandlerName}::class => AutoWiringFactory::class, - + // Response Formatter {$apiResponseFormatterName}::class => AutoWiringFactory::class, ], ]; - "; writeToFile($serviceManagerFilePath, $serviceManagerFileContent); @@ -129,7 +128,7 @@ use {$apiHandlerUsingNamespace}; return [ [ 'name' => '{$routeName}', - 'path' => '/api/{$routePath}[/]', + 'path' => '/api/{$routePath}/', 'allowed_methods' => ['POST'], 'middleware' => [ {$apiHandlerName}::class, @@ -179,7 +178,6 @@ return [ {$cqrsHandlerName}::class => AutoWiringFactory::class, ], ]; - "; writeToFile($serviceManagerFilePath, $serviceManagerFileContent); } @@ -192,12 +190,13 @@ declare(strict_types=1); namespace {$apiHandlerNamespace}; use {$apiResponseFormatterUsingNamespace}; -use {$cqrsHandlerUsingNamespace}; 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; -use {$projectNamespace}\\Infrastructure\\Response\\SuccessResponse; class {$apiHandlerName} implements RequestHandlerInterface { @@ -207,13 +206,10 @@ class {$apiHandlerName} implements RequestHandlerInterface private readonly {$apiResponseFormatterName} \$responseFormatter, ) { } - + public function handle(ServerRequestInterface \$request): ResponseInterface { - \$data = json_decode( - \$request->getBody()->getContents(), - true - ); + \$data = \$request->getAttribute(AnalyzeBodyMiddleware::JSON_DATA); \${$cqrsVariableName} = \$this->{$cqrsBuilderVariableName}->build( \$data diff --git a/bin/createPipeline.php b/bin/createPipeline.php index 7c30360..702d04c 100644 --- a/bin/createPipeline.php +++ b/bin/createPipeline.php @@ -88,7 +88,7 @@ class {$stepClassName} implements TaskInterface #TODO ) { } - + public function __invoke( \$payload, PipelineInterface \$pipeline @@ -96,7 +96,9 @@ class {$stepClassName} implements TaskInterface { /** @var {$payloadClassName} \${$payloadVariableName} */ \${$payloadVariableName} = \$payload; + + \$pipeline->next()(\$payload, \$pipeline); } } @@ -122,7 +124,7 @@ use teewurst\\Pipeline\\Pipeline; class {$pipelineClassName} extends Pipeline { public function __construct( - {$stepsDeclaration} + {$stepsDeclaration} ) { parent::__construct([ {$stepsReference} @@ -142,7 +144,7 @@ class {$payloadClassName} { public function __construct( #TODO - ) { + ) { } } "; diff --git a/config/pipeline.php b/config/pipeline.php index f9ee865..aeaaeb4 100644 --- a/config/pipeline.php +++ b/config/pipeline.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Template\Infrastructure\Exception\Middleware\TemplateExceptionHandlerMiddleware; +use Template\Infrastructure\Request\Middleware\AnalyzeBodyMiddleware; use Template\Infrastructure\Request\Middleware\AnalyzeHeaderMiddleware; use Template\Infrastructure\Session\Middleware\SessionMiddleware; use Laminas\Stratigility\Middleware\ErrorHandler; @@ -69,6 +70,7 @@ return function (Application $app, MiddlewareFactory $factory, ContainerInterfac //// Pre Template Space $app->pipe(TemplateExceptionHandlerMiddleware::class); $app->pipe(AnalyzeHeaderMiddleware::class); + $app->pipe(AnalyzeBodyMiddleware::class); //// Template Space $app->pipe(SessionMiddleware::class); diff --git a/src/Infrastructure/Request/config/service_manager.php b/src/Infrastructure/Request/config/service_manager.php index ffff53a..92b91fa 100644 --- a/src/Infrastructure/Request/config/service_manager.php +++ b/src/Infrastructure/Request/config/service_manager.php @@ -2,13 +2,12 @@ declare(strict_types=1); -use Template\Infrastructure\Rbac\Middleware\EnsureAuthorizationMiddleware; +use Reinfi\DependencyInjection\Factory\AutoWiringFactory; use Template\Infrastructure\Request\Factory\RequestServiceFactory; +use Template\Infrastructure\Request\Middleware\AnalyzeBodyMiddleware; use Template\Infrastructure\Request\Middleware\AnalyzeHeaderMiddleware; use Template\Infrastructure\Request\Middleware\InternalRequestMiddleware; use Template\Infrastructure\Request\Service\RequestService; -use Reinfi\DependencyInjection\Factory\AutoWiringFactory; -use Reinfi\DependencyInjection\Factory\InjectionFactory; return [ 'factories' => [ @@ -17,6 +16,7 @@ return [ /// Middleware AnalyzeHeaderMiddleware::class => AutoWiringFactory::class, + AnalyzeBodyMiddleware::class => AutoWiringFactory::class, InternalRequestMiddleware::class => AutoWiringFactory::class, ], ]; diff --git a/src/Infrastructure/Request/src/Middleware/AnalyzeBodyMiddleware.php b/src/Infrastructure/Request/src/Middleware/AnalyzeBodyMiddleware.php new file mode 100644 index 0000000..a732d08 --- /dev/null +++ b/src/Infrastructure/Request/src/Middleware/AnalyzeBodyMiddleware.php @@ -0,0 +1,43 @@ +getHeaderLine(self::CONTENT_TYPE_HEADER); + + if (str_contains($contentType, self::CONTENT_TYPE_APPLICATION_JSON)) { + $jsonData = json_decode( + $request->getBody()->getContents(), + true + ); + } + + return $handler->handle($request->withAttribute( + self::JSON_DATA, + $jsonData + )); + } +} \ No newline at end of file