Skip to content

Commit

Permalink
Update master to output generated at 07d1f41
Browse files Browse the repository at this point in the history
  • Loading branch information
abenevaut committed Dec 21, 2024
1 parent b842761 commit f19cd41
Show file tree
Hide file tree
Showing 17 changed files with 295 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Be free to fork this project.
All inspired code or re-used code have to be licensed to GNU GPLv3.
No contribution is accepted on this READ ONLY repository. Please contribute on https://github.com/abenevaut/opensource.

Roadmap: https://github.com/users/abenevaut/projects/15
Roadmap: https://github.com/users/abenevaut/projects/14
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
No contribution is accepted on this READ ONLY repository. Please contribute on https://github.com/abenevaut/opensource.

Roadmap: https://github.com/users/abenevaut/projects/15
Roadmap: https://github.com/users/abenevaut/projects/14
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
coverage
vendor
xdebug-errors.log
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
"phpunit/phpunit": "^10.5",
"mockery/mockery": "^1.6",
"squizlabs/php_codesniffer": "^3.9",
"icanhazstring/composer-unused": "^0.8.5",
"laravel/framework": "^9.45 || ^10 || ^11"
"laravel/framework": "^9.45 || ^10 || ^11",
"phpstan/phpstan": "^1.10 || ^2.0",
"nikic/php-parser": "^5.0",
"fakerphp/faker": "^1.24"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:

app:
image: ghcr.io/abenevaut/vapor-ci:php83
restart: unless-stopped
environment:
- php_xdebug_log=/var/task/xdebug-errors.log
volumes:
- .:/var/task
9 changes: 9 additions & 0 deletions docs/Contribute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# How to contribute

## Build

Maintain composer packages with php 8.1

```bash
/opt/homebrew/opt/[email protected]/bin/php /opt/homebrew/bin//composer update
```
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 1
editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'
paths:
- src
rules:
- Tests\Rules\CodeDoesNotContainDumpRule
- Tests\Rules\ControllersExtendBaseControllerRule
8 changes: 8 additions & 0 deletions src/App/Events/EventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace abenevaut\Infrastructure\App\Events;

interface EventInterface
{
// do stuff
}
4 changes: 3 additions & 1 deletion src/App/Listeners/ListenerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace abenevaut\Infrastructure\App\Listeners;

use abenevaut\Infrastructure\App\Events\EventInterface;

abstract class ListenerAbstract
{
abstract public function handle($event): void;
abstract public function handle(EventInterface $event): void;
}
8 changes: 7 additions & 1 deletion src/Console/ProcessPoolCommandAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

abstract class ProcessPoolCommandAbstract extends Command
{
/**
* @var array<Process>
*/
private array $queue = [];

private int $queueLength = 0;

/**
* @var array<Process>
*/
private array $computingQueue = [];

private int $computingQueueLength = 0;
Expand All @@ -24,7 +30,7 @@ public function getQueueLength(): int
return $this->queueLength;
}

public function handle(): bool
public function handle(): int
{
return $this
->boot()
Expand Down
24 changes: 24 additions & 0 deletions src/Http/Middleware/IdentifyClientRequestMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace abenevaut\Infrastructure\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class IdentifyClientRequestMiddleware extends ShareLogsContextMiddlewareAbstract
{
/**
* @return array<string, int|string>
*/
protected function additionalContext(Request $request): array
{
$clientId = '';
if (Auth::guard('api')->check()) {
$clientId = Auth::guard('api')->client()->id;
}

return [
'client-id' => $clientId,
];
}
}
24 changes: 24 additions & 0 deletions src/Http/Middleware/IdentifyUserRequestMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace abenevaut\Infrastructure\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class IdentifyUserRequestMiddleware
{
/**
* @return array<string, mixed>
*/
protected function additionalContext(Request $request): array
{
$userId = '';
if (Auth::check()) {
$userId = Auth::user()->getAuthIdentifier();
}

return [
'user-id' => $userId,
];
}
}
43 changes: 43 additions & 0 deletions src/Http/Middleware/ShareLogsContextMiddlewareAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace abenevaut\Infrastructure\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

abstract class ShareLogsContextMiddlewareAbstract
{
/**
* @return array<string, string>
*/
abstract protected function additionalContext(Request $request): array;

public function handle(Request $request, Closure $next): Response
{
$sharedContext = array_merge(
$this->requestHitId(),
$this->additionalContext($request)
);

Log::shareContext($sharedContext);

/** @var Response $response */
$response = $next($request);
$response->header('REQUEST-HIT-ID', $sharedContext['request-hit-id']);

return $response;
}

/**
* @return array<string, string>
*/
protected function requestHitId(): array
{
return [
'request-hit-id' => (string) Str::ulid()
];
}
}
18 changes: 18 additions & 0 deletions src/Http/Requests/ApiFormRequestAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace abenevaut\Infrastructure\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;

abstract class ApiFormRequestAbstract extends FormRequest
{
protected function failedValidation(Validator $validator)
{
$response = new JsonResponse(['errors' => $validator->errors()], 400);

throw new HttpResponseException($response);
}
}
40 changes: 40 additions & 0 deletions tests/Rules/CodeDoesNotContainDumpRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Rules;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name as NodeName;
use PHPStan\Rules\Rule;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\RuleErrorBuilder;

/**
* https://laravel-france.com/posts/phpstan-il-est-ou-dd
*/
class CodeDoesNotContainDumpRule implements Rule
{
public function getNodeType(): string
{
return FuncCall::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (! $node->name instanceof NodeName) {
return [];
}

$functionName = $node->name->toString();

if (in_array($functionName, ['dd', 'var_dump', 'dump'], true)) {
return [
RuleErrorBuilder::message(
sprintf('Method %s is prohibited', $functionName)
)->build(),
];
}

return [];
}
}
40 changes: 40 additions & 0 deletions tests/Rules/ControllersExtendBaseControllerRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Rules;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;

class ControllersExtendBaseControllerRule implements Rule
{
public function getNodeType(): string
{
return InClassNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (
empty($scope->getNamespace())
|| !str_starts_with($scope->getNamespace(), 'abenevaut\Infrastructure\Http\Controllers')
) {
return [];
}

$reflectionClass = $node->getClassReflection();

if ($reflectionClass->getName() === 'abenevaut\Infrastructure\Http\Controllers\ControllerAbstract') {
return [];
}

if (!$reflectionClass->isSubclassOf('Illuminate\Routing\Controller')) {
return [
"Controllers should extend 'Illuminate\Routing\Controller' (see rule #49)"
];
}

return [];
}
}
55 changes: 55 additions & 0 deletions tests/Unit/ShareLogsContextMiddlewareAbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Tests\Unit;

use abenevaut\Infrastructure\Http\Middleware\ShareLogsContextMiddlewareAbstract;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use PHPUnit\Framework\TestCase;

class ShareLogsContextMiddlewareAbstractTest extends TestCase
{
use WithFaker;

public function testToRetrieveSharedContext()
{
Log::spy();

$requestHitId = $this->makeFaker()->uuid;

$middlewareStub = $this
->createPartialMock(
ShareLogsContextMiddlewareAbstract::class,
[
'requestHitId',
'additionalContext'
]
);

$middlewareStub
->expects($this->once())
->method('requestHitId')
->willReturn(['request-hit-id' => $requestHitId]);

$middlewareStub
->expects($this->once())
->method('additionalContext')
->willReturn([]);

/** @var Response $request */
$response = $middlewareStub
->handle(
$this->createMock(Request::class),
function ($request) {
return new Response();
}
);

Log::shouldHaveReceived('shareContext')->once()->withAnyArgs();

$this->assertTrue($response->headers->has('REQUEST-HIT-ID'));
$this->assertSame($response->headers->get('REQUEST-HIT-ID'), $requestHitId);
}
}

0 comments on commit f19cd41

Please sign in to comment.