1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the reva2/jsonapi. |
4
|
|
|
* |
5
|
|
|
* (c) OrbitScripts LLC <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Reva2\JsonApi\EventListener; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\Reader; |
15
|
|
|
use Reva2\JsonApi\Annotations\ApiRequest; |
16
|
|
|
use Reva2\JsonApi\Contracts\Factories\FactoryInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
19
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* JSON API listener handle JSON API annotations |
23
|
|
|
* |
24
|
|
|
* @package Reva2\JsonApi\EventListener |
25
|
|
|
* @author Sergey Revenko <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ApiListener implements EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Reader |
31
|
|
|
*/ |
32
|
|
|
protected $reader; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FactoryInterface |
36
|
|
|
*/ |
37
|
|
|
protected $factory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Default matcher configuration |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $defMatcher; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor |
48
|
|
|
* |
49
|
|
|
* @param Reader $reader |
50
|
|
|
* @param FactoryInterface $factory |
51
|
|
|
* @param array $defMatcher |
52
|
|
|
*/ |
53
|
3 |
|
public function __construct(Reader $reader, FactoryInterface $factory, array $defMatcher) |
54
|
|
|
{ |
55
|
3 |
|
$this->reader = $reader; |
56
|
3 |
|
$this->factory = $factory; |
57
|
3 |
|
$this->defMatcher = $defMatcher; |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Load JSON API configuration from controller annotations |
62
|
|
|
* |
63
|
|
|
* @param FilterControllerEvent $event |
64
|
|
|
*/ |
65
|
2 |
|
public function onKernelController(FilterControllerEvent $event) |
66
|
|
|
{ |
67
|
2 |
|
$controller = $event->getController(); |
68
|
2 |
|
if (!is_array($controller)) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
$config = null; |
73
|
|
|
|
74
|
2 |
|
$refClass = new \ReflectionClass($controller[0]); |
75
|
2 |
|
if (null !== ($annotation = $this->reader->getClassAnnotation($refClass, ApiRequest::class))) { |
76
|
|
|
/* @var $annotation ApiRequest */ |
77
|
1 |
|
$config = $annotation->toArray(); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
2 |
|
$refMethod = $refClass->getMethod($controller[1]); |
81
|
2 |
|
if (null !== ($annotation = $this->reader->getMethodAnnotation($refMethod, ApiRequest::class))) { |
82
|
2 |
|
if (null !== $config) { |
83
|
1 |
|
$config = array_replace($config, $annotation->toArray()); |
84
|
1 |
|
} else { |
85
|
1 |
|
$config = $annotation->toArray(); |
86
|
|
|
} |
87
|
2 |
|
} |
88
|
|
|
|
89
|
2 |
|
if (null !== $config) { |
90
|
2 |
|
if (!array_key_exists('matcher', $config)) { |
91
|
1 |
|
$config['matcher'] = $this->defMatcher; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
2 |
|
$event->getRequest()->attributes->set('_jsonapi', $this->factory->createEnvironment($config)); |
95
|
2 |
|
} |
96
|
2 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
1 |
|
public static function getSubscribedEvents() |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
1 |
|
KernelEvents::CONTROLLER => 'onKernelController' |
105
|
1 |
|
]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|