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 Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
18
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* JSON API listener handle JSON API annotations |
22
|
|
|
* |
23
|
|
|
* @package Reva2\JsonApi\EventListener |
24
|
|
|
* @author Sergey Revenko <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ApiListener implements EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Reader |
30
|
|
|
*/ |
31
|
|
|
protected $reader; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor |
35
|
|
|
* |
36
|
|
|
* @param Reader $reader |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Reader $reader) |
39
|
|
|
{ |
40
|
|
|
$this->reader = $reader; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Load JSON API configuration from controller annotations |
45
|
|
|
* |
46
|
|
|
* @param FilterControllerEvent $event |
47
|
|
|
*/ |
48
|
|
|
public function onKernelController(FilterControllerEvent $event) |
49
|
|
|
{ |
50
|
|
|
$controller = $event->getController(); |
51
|
|
|
if (!is_array($controller)) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$config = null; |
56
|
|
|
|
57
|
|
|
$refClass = new \ReflectionClass($controller[0]); |
58
|
|
|
if (null !== ($annotation = $this->reader->getClassAnnotation($refClass, ApiRequest::class))) { |
59
|
|
|
/* @var $annotation ApiRequest */ |
60
|
|
|
$config = $annotation->toArray(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$refMethod = $refClass->getMethod($controller[1]); |
64
|
|
|
if (null !== ($annotation = $this->reader->getMethodAnnotation($refMethod, ApiRequest::class))) { |
65
|
|
|
if (null !== $config) { |
66
|
|
|
$config = array_replace($config, $annotation->toArray()); |
67
|
|
|
} else { |
68
|
|
|
$config = $annotation->toArray(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (null !== $config) { |
73
|
|
|
$event->getRequest()->attributes->set('_jsonapi', $config); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public static function getSubscribedEvents() |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
KernelEvents::CONTROLLER => 'onKernelController' |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
} |