Skip to content

Commit 5805d5e

Browse files
committed
fix: use pitch/annotation for Graceful annotation
1 parent ce4446a commit 5805d5e

8 files changed

+1469
-1039
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
},
2323
"require": {
2424
"php": ">=7.4",
25+
"composer/composer": "^1 || ^2",
26+
"pitch/annotation": "^1.0",
2527
"symfony/config": "^5",
26-
"symfony/http-kernel": "^5",
27-
"composer/composer": "^1 || ^2"
28+
"symfony/http-kernel": "^5"
2829
},
2930
"require-dev": {
3031
"phpunit/phpunit": "^9.5",

composer.lock

+1,459-836
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Configuration/ConfigurationAnnotation.php

-55
This file was deleted.

src/Configuration/Graceful.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
use Attribute;
55
use Doctrine\Common\Annotations\Annotation;
66
use Doctrine\Common\Annotations\Annotation\Target;
7+
use Pitch\Annotation\AbstractAnnotation;
78

89
/**
910
* @Annotation
1011
* @Target({"METHOD"})
1112
*/
1213
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
13-
class Graceful extends ConfigurationAnnotation
14+
class Graceful extends AbstractAnnotation
1415
{
1516
public ?string $value = null;
1617

src/EventSubscriber/ControllerSubscriber.php

-39
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,29 @@
11
<?php
22
namespace Pitch\AdrBundle\EventSubscriber;
33

4-
use Doctrine\Common\Annotations\Reader;
54
use Pitch\AdrBundle\Action\ActionProxy;
65
use Pitch\AdrBundle\Configuration\Graceful;
7-
use ReflectionAttribute;
8-
use ReflectionMethod;
96
use Symfony\Component\HttpKernel\KernelEvents;
107
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
118
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
12-
use Symfony\Component\HttpKernel\Event\ControllerEvent;
139

1410
class ControllerSubscriber implements EventSubscriberInterface
1511
{
16-
private ?Reader $reader;
1712
private array $globalGraceful;
1813

1914
public function __construct(
20-
?Reader $reader,
2115
?array $globalGraceful
2216
) {
23-
$this->reader = $reader;
2417
$this->globalGraceful = \array_map(fn($g) => new Graceful($g), (array) $globalGraceful);
2518
}
2619

2720
public static function getSubscribedEvents()
2821
{
2922
return [
30-
KernelEvents::CONTROLLER => ['onKernelController', -1024],
3123
KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', -1024],
3224
];
3325
}
3426

35-
public function onKernelController(ControllerEvent $event)
36-
{
37-
$controller = $event->getController();
38-
39-
if (\is_object($controller)) {
40-
$controller = [$controller, '__invoke'];
41-
}
42-
43-
$reflMethod = new ReflectionMethod($controller[0], $controller[1]);
44-
45-
$annotations = $this->reader
46-
? $this->reader->getMethodAnnotations($reflMethod)
47-
: [];
48-
49-
$attributes = PHP_MAJOR_VERSION >= 8
50-
? \array_map(
51-
function (ReflectionAttribute $a) {
52-
$class = $a->getName();
53-
$args = $a->getArguments();
54-
if (isset($args[0]) && \is_array($args[0])) {
55-
return $a->newInstance();
56-
}
57-
return new $class(['value' => $args[0], 'not' => $args['not']]);
58-
},
59-
$reflMethod->getAttributes(Graceful::class, ReflectionAttribute::IS_INSTANCEOF),
60-
)
61-
: [];
62-
63-
$event->getRequest()->attributes->set('_' . Graceful::class, [...$annotations, ...$attributes]);
64-
}
65-
6627
public function onKernelControllerArguments(ControllerArgumentsEvent $event)
6728
{
6829

test/Configuration/ConfigurationAnnotationTest.php

-45
This file was deleted.

test/EventSubscriber/ControllerSubscriberTest.php

+4-61
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,14 @@
11
<?php
22
namespace Pitch\AdrBundle\EventSubscriber;
33

4-
use Doctrine\Common\Annotations\AnnotationReader;
54
use Pitch\AdrBundle\Action\ActionProxy;
65
use Pitch\AdrBundle\Configuration\Graceful;
76
use Symfony\Component\HttpFoundation\Request;
87
use Symfony\Component\HttpKernel\HttpKernelInterface;
98
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
10-
use Symfony\Component\HttpKernel\Event\ControllerEvent;
119

1210
class ControllerSubscriberTest extends EventSubscriberTest
1311
{
14-
public function testReadAnnotations()
15-
{
16-
$event = $this->getControllerEvent(new class {
17-
/**
18-
* @Graceful("Foo")
19-
* @Graceful("Bar", not={"Baz"})
20-
*/
21-
public function __invoke()
22-
{
23-
}
24-
});
25-
26-
// without Doctrine Annotations
27-
$this->getSubscriberObject([], false)->onKernelController($event);
28-
$this->assertEquals([], $event->getRequest()->attributes->get('_' . Graceful::class));
29-
30-
// with Doctrine Annotations
31-
$this->getSubscriberObject([], true)->onKernelController($event);
32-
$this->assertEquals([
33-
new Graceful(['value' => 'Foo']),
34-
new Graceful(['value' => 'Bar', 'not' => 'Baz']),
35-
], $event->getRequest()->attributes->get('_' . Graceful::class));
36-
}
37-
38-
public function testReadAttributes()
39-
{
40-
$event = $this->getControllerEvent(new class {
41-
#[Graceful(['value' => 'Foo'])]
42-
#[Graceful('Bar', not: 'Baz')]
43-
public function __invoke()
44-
{
45-
}
46-
});
47-
48-
$this->getSubscriberObject([], false)->onKernelController($event);
49-
$this->assertEquals(
50-
PHP_MAJOR_VERSION >= 8
51-
? [
52-
new Graceful(['value' => 'Foo']),
53-
new Graceful(['value' => 'Bar', 'not' => 'Baz']),
54-
]
55-
: [],
56-
$event->getRequest()->attributes->get('_' . Graceful::class)
57-
);
58-
}
59-
6012
public function provideGraceful(): array
6113
{
6214
return [
@@ -129,17 +81,6 @@ protected function getGracefulForArray(
12981
return \array_map(fn($g) => new Graceful($g), $gracefulList);
13082
}
13183

132-
protected function getControllerEvent(
133-
callable $controller
134-
): ControllerEvent {
135-
return new ControllerEvent(
136-
$this->createMock(HttpKernelInterface::class),
137-
$controller,
138-
new Request(),
139-
HttpKernelInterface::MASTER_REQUEST,
140-
);
141-
}
142-
14384
/**
14485
* @param Graceful[] $controllerGraceful
14586
*/
@@ -149,8 +90,11 @@ protected function getControllerArgumentsEvent(
14990
$request = new Request();
15091
$request->attributes->set('_' . Graceful::class, $controllerGraceful);
15192

93+
/** @var HTTPKernelInterface */
94+
$httpKernel = $this->createMock(HttpKernelInterface::class);
95+
15296
return new ControllerArgumentsEvent(
153-
$this->createMock(HttpKernelInterface::class),
97+
$httpKernel,
15498
function () {
15599
},
156100
[],
@@ -164,7 +108,6 @@ protected function getSubscriberObject(
164108
bool $reader = false
165109
): ControllerSubscriber {
166110
return new ControllerSubscriber(
167-
$reader ? new AnnotationReader() : null,
168111
$globalGraceful,
169112
);
170113
}

test/PitchAdrBundleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function testAddCompilerPass()
1616

1717
$bundle = new PitchAdrBundle();
1818

19+
/** @var ContainerBuilder $container */
1920
$bundle->build($container);
2021
}
2122

0 commit comments

Comments
 (0)