Skip to content

Commit

Permalink
Merge pull request #5 from kafkiansky/1.0.x-dev
Browse files Browse the repository at this point in the history
1.0.x dev
  • Loading branch information
kafkiansky authored Dec 13, 2023
2 parents 065ea4e + 6541ade commit e031eb9
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendor/
composer.lock
.idea/
.phpunit.result.cache
.phpunit.cache/
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ final class SomeController
}
```

Also, you can use nested groups:

```yaml
symiddleware:
global:
- App\Controller\SetCorsHeaders
- web
groups:
web:
middlewares:
- 'App\Middleware\ModifyRequestMiddleware'
- debug
debug:
if: false
middlewares:
- 'App\Middleware\LogSqlQuery'
```

Duplicated middlewares will be removed.

## Customization
Expand Down
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
},
"require": {
"php": "^8.2",
"symfony/dependency-injection": "^6.2",
"symfony/event-dispatcher": "^6.2",
"symfony/http-kernel": "^6.2",
"symfony/config": "^6.2",
"symfony/http-foundation": "^6.2",
"symfony/dependency-injection": "^7.0",
"symfony/event-dispatcher": "^7.0",
"symfony/http-kernel": "^7.0",
"symfony/config": "^7.0",
"symfony/http-foundation": "^7.0",
"psr/http-server-middleware": "^1.0",
"psr/http-server-handler": "^1.0",
"symfony/psr-http-message-bridge": "^2.1",
"symfony/psr-http-message-bridge": "^7.0",
"nyholm/psr7": "^1.4",
"symfony/cache": "^6.2"
"symfony/cache": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4.9"
"phpunit/phpunit": "^10.0",
"vimeo/psalm": "^5.0"
},
"license": "MIT",
"authors": [
Expand All @@ -44,6 +44,6 @@
"minimum-stability": "stable",
"scripts": {
"lint": "./vendor/bin/psalm --no-cache",
"test": "./vendor/bin/phpunit --do-not-cache-result"
"test": "./vendor/bin/phpunit --do-not-cache-result --no-coverage"
}
}
49 changes: 22 additions & 27 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix=".php">./src/Resources</directory>
<directory suffix=".php">./src/DependencyInjection</directory>
<file>./src/SymiddlewareBundle.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="clover.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<report>
<clover outputFile="clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory suffix=".php">./src/Resources</directory>
<directory suffix=".php">./src/DependencyInjection</directory>
<file>./src/SymiddlewareBundle.php</file>
</exclude>
</source>
</phpunit>
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
Expand Down
13 changes: 1 addition & 12 deletions src/Attribute/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,13 @@
use Psr\Http\Server\MiddlewareInterface;

#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
/**
* @psalm-immutable
*/
final class Middleware
{
/**
* @psalm-readonly
*
* @var class-string<MiddlewareInterface>[]|string[]
*/
public array $list;

/**
* @param class-string<MiddlewareInterface>[]|string[] $list
*/
public function __construct(array $list)
public function __construct(public readonly array $list)
{
$this->list = $list;
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/Attribute/Reader/CacheAttributesReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

final class CacheAttributesReader implements AttributeReader
{
private CacheItemPoolInterface $cache;
private AttributeReader $delegate;

public function __construct(CacheItemPoolInterface $cache, AttributeReader $delegate)
{
$this->cache = $cache;
$this->delegate = $delegate;
public function __construct(
private readonly CacheItemPoolInterface $cache,
private readonly AttributeReader $delegate,
) {
}

/**
Expand Down Expand Up @@ -85,7 +82,7 @@ private function normalizeToCache(array $middlewares): array

private static function cacheKey(object $class, ?string $method = null): string
{
$key = 'symfony.middleware.'.str_replace('\\', '', get_class($class));
$key = 'symfony.middleware.'.str_replace('\\', '', $class::class);

if ($method !== null) {
$key .= '.'.$method;
Expand Down
7 changes: 1 addition & 6 deletions src/Attribute/Reader/ClassMethodAttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
final class ClassMethodAttributeReader implements AttributeReader
{
/**
* @param object $class
* @param string|null $method
*
* @throws \ReflectionException
*
* @return Middleware[]
* {@inheritdoc}
*/
public function read(object $class, ?string $method = null): array
{
Expand Down
16 changes: 8 additions & 8 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ public function getConfigTreeBuilder(): TreeBuilder
}

/**
* @return \Closure
* @return \Closure(mixed): string
*/
private function createMiddlewareNormalizer(): callable
{
return function (mixed $middlewareName): string {
if (!\is_string($middlewareName) || !\is_a($middlewareName, MiddlewareInterface::class, true)) {
throw new \RuntimeException(
return match (true) {
\is_string($middlewareName) && \class_exists($middlewareName) && \is_a($middlewareName, MiddlewareInterface::class, true) => $middlewareName,
\is_string($middlewareName) && !\class_exists($middlewareName) => $middlewareName,
default => throw new \RuntimeException(
vsprintf('Each middleware must implements the "%s" interface, but "%s" doesn\'t.', [
MiddlewareInterface::class,
$middlewareName,
\is_string($middlewareName) ? $middlewareName : get_debug_type($middlewareName),
])
);
}

return $middlewareName;
)
};
};
}
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/SymiddlewareExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function load(array $configs, ContainerBuilder $container): void
return;
}

/** @var array{groups: array<string, array{if?: string}>} $config */
/** @var array{groups: array<string, array{if?: string}>, global?: string[]} $config */
$config = $this->processConfiguration($configuration, $configs);

$config['groups']['global'] = [
Expand Down
13 changes: 3 additions & 10 deletions src/Integration/ControllerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@

final class ControllerListener
{
private MiddlewareGatherer $middlewareGatherer;
private AttributeReader $reader;
private ControllerReplacer $controllerReplacer;

public function __construct(
MiddlewareGatherer $middlewareGatherer,
AttributeReader $reader,
ControllerReplacer $controllerReplacer
private readonly MiddlewareGatherer $middlewareGatherer,
private readonly AttributeReader $reader,
private readonly ControllerReplacer $controllerReplacer,
) {
$this->middlewareGatherer = $middlewareGatherer;
$this->reader = $reader;
$this->controllerReplacer = $controllerReplacer;
}

/**
Expand Down
13 changes: 3 additions & 10 deletions src/Integration/ControllerReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@

final class ControllerReplacer
{
private PsrRequestTransformer $psrRequestTransformer;
private PsrResponseTransformer $psrResponseTransformer;
private PsrRequestCloner $psrRequestCloner;

public function __construct(
PsrRequestTransformer $psrRequestTransformer,
PsrResponseTransformer $psrResponseTransformer,
PsrRequestCloner $psrRequestCloner,
private readonly PsrRequestTransformer $psrRequestTransformer,
private readonly PsrResponseTransformer $psrResponseTransformer,
private readonly PsrRequestCloner $psrRequestCloner,
) {
$this->psrRequestTransformer = $psrRequestTransformer;
$this->psrResponseTransformer = $psrResponseTransformer;
$this->psrRequestCloner = $psrRequestCloner;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/MiddlewareAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
final class MiddlewareAction
{
public function __construct(
private MiddlewareRunner $middlewareRunner,
private ServerRequestInterface $serverRequest,
private readonly MiddlewareRunner $middlewareRunner,
private readonly ServerRequestInterface $serverRequest,
) {
}

Expand Down
19 changes: 11 additions & 8 deletions src/Middleware/MiddlewareGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

final class MiddlewareGatherer
{
private MiddlewareRegistry $middlewareRegistry;
/**
* Array-key where global middleware may be defined.
*/
private const GLOBAL_MIDDLEWARE_GROUP = 'global';

public function __construct(MiddlewareRegistry $middlewareRegistry)
{
$this->middlewareRegistry = $middlewareRegistry;
public function __construct(
private readonly MiddlewareRegistry $middlewareRegistry,
) {
}

/**
Expand All @@ -26,11 +29,11 @@ public function __construct(MiddlewareRegistry $middlewareRegistry)
*/
public function gather(array $attributes): array
{
$middlewaresOrGroups = array_unique(array_merge(...array_map(function (Middleware $middleware): array {
return $middleware->list;
}, $attributes)));
$middlewaresOrGroups = array_unique(
array_merge([], ...array_map(fn (Middleware $middleware): array => $middleware->list, $attributes)),
);

$middlewares = $this->middlewareRegistry->byName(MiddlewareRegistry::GLOBAL_MIDDLEWARE_GROUP);
$middlewares = $this->middlewareRegistry->byName(self::GLOBAL_MIDDLEWARE_GROUP);

foreach ($middlewaresOrGroups as $middlewareOrGroup) {
$middlewares = array_merge($middlewares, $this->middlewareRegistry->byName($middlewareOrGroup));
Expand Down
18 changes: 4 additions & 14 deletions src/Middleware/MiddlewareRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,19 @@

final class MiddlewareRunner
{
/**
* @var MiddlewareInterface[]
*/
private array $middlewares;
private RequestHandlerInterface $requestHandler;
private PsrResponseTransformer $psrResponseTransformer;

/**
* @param MiddlewareInterface[] $middlewares
*/
public function __construct(
array $middlewares,
RequestHandlerInterface $requestHandler,
PsrResponseTransformer $psrResponseTransformer,
private readonly array $middlewares,
private readonly RequestHandlerInterface $requestHandler,
private readonly PsrResponseTransformer $psrResponseTransformer,
) {
$this->middlewares = $middlewares;
$this->requestHandler = $requestHandler;
$this->psrResponseTransformer = $psrResponseTransformer;
}

public function run(ServerRequestInterface $serverRequest): Response
{
/** @var \Closure(ServerRequestInterface): ResponseInterface */
/** @psalm-var \Closure(ServerRequestInterface): ResponseInterface $processor */
$processor = array_reduce(
array_reverse($this->middlewares),
/** @param \Closure(ServerRequestInterface): ResponseInterface $stack */
Expand Down
5 changes: 0 additions & 5 deletions src/Middleware/Registry/MiddlewareRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

interface MiddlewareRegistry
{
/**
* Array-key where global middleware may be defined.
*/
public const GLOBAL_MIDDLEWARE_GROUP = 'global';

/**
* @param class-string<MiddlewareInterface>|string $middlewareFqcnOrGroup
*
Expand Down
Loading

0 comments on commit e031eb9

Please sign in to comment.