-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update master to output generated at 07d1f41
- Loading branch information
Showing
17 changed files
with
295 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
composer.lock | ||
coverage | ||
vendor | ||
xdebug-errors.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace abenevaut\Infrastructure\App\Events; | ||
|
||
interface EventInterface | ||
{ | ||
// do stuff | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
src/Http/Middleware/ShareLogsContextMiddlewareAbstract.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |