Commit | Line | Data |
---|---|---|
6d3bdc98 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
41498ac5 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6d3bdc98 | 5 | | | |
41498ac5 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6d3bdc98 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6d3bdc98 TO |
11 | |
12 | namespace Civi\API\Subscriber; | |
8882ff5c | 13 | |
6d3bdc98 TO |
14 | use Civi\API\Events; |
15 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
16 | ||
17 | /** | |
18 | * This is a wrapper for the legacy "API Wrapper" interface which allows | |
19 | * wrappers to run through the new kernel. It translates from dispatcher events | |
f247addc | 20 | * ('civi.api.prepare', 'civi.api.respond') to wrapper calls ('fromApiInput', 'toApiOutput'). |
6d3bdc98 TO |
21 | */ |
22 | class WrapperAdapter implements EventSubscriberInterface { | |
23 | ||
6550386a EM |
24 | /** |
25 | * @return array | |
26 | */ | |
6d3bdc98 | 27 | public static function getSubscribedEvents() { |
c64f69d9 | 28 | return [ |
39b870b8 TO |
29 | 'civi.api.prepare' => ['onApiPrepare', Events::W_MIDDLE], |
30 | 'civi.api.respond' => ['onApiRespond', Events::W_EARLY * 2], | |
c64f69d9 | 31 | ]; |
6d3bdc98 TO |
32 | } |
33 | ||
34 | /** | |
f33d2b8c | 35 | * @var \API_Wrapper[] |
6d3bdc98 TO |
36 | */ |
37 | protected $defaults; | |
38 | ||
6550386a EM |
39 | /** |
40 | * @param array $defaults | |
8882ff5c | 41 | * array(\API_Wrapper). |
6550386a | 42 | */ |
c64f69d9 | 43 | public function __construct($defaults = []) { |
6d3bdc98 TO |
44 | $this->defaults = $defaults; |
45 | } | |
46 | ||
6550386a EM |
47 | /** |
48 | * @param \Civi\API\Event\PrepareEvent $event | |
8882ff5c | 49 | * API preparation event. |
6550386a | 50 | */ |
6d3bdc98 TO |
51 | public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { |
52 | $apiRequest = $event->getApiRequest(); | |
53 | ||
54 | // For input filtering, process $apiWrappers in forward order | |
55 | foreach ($this->getWrappers($apiRequest) as $apiWrapper) { | |
56 | $apiRequest = $apiWrapper->fromApiInput($apiRequest); | |
57 | } | |
58 | ||
59 | $event->setApiRequest($apiRequest); | |
60 | } | |
61 | ||
6550386a EM |
62 | /** |
63 | * @param \Civi\API\Event\RespondEvent $event | |
8882ff5c | 64 | * API response event. |
6550386a | 65 | */ |
6d3bdc98 TO |
66 | public function onApiRespond(\Civi\API\Event\RespondEvent $event) { |
67 | $apiRequest = $event->getApiRequest(); | |
68 | $result = $event->getResponse(); | |
69 | ||
70 | // For output filtering, process $apiWrappers in reverse order | |
71 | foreach (array_reverse($this->getWrappers($apiRequest)) as $apiWrapper) { | |
72 | $result = $apiWrapper->toApiOutput($apiRequest, $result); | |
73 | } | |
74 | ||
75 | $event->setResponse($result); | |
76 | } | |
77 | ||
78 | /** | |
79 | * @param array $apiRequest | |
8882ff5c | 80 | * The full API request. |
6d3bdc98 TO |
81 | * @return array<\API_Wrapper> |
82 | */ | |
83 | public function getWrappers($apiRequest) { | |
6233ba43 CW |
84 | if (!isset($apiRequest['wrappers']) || is_null($apiRequest['wrappers'])) { |
85 | $apiRequest['wrappers'] = $apiRequest['version'] < 4 ? $this->defaults : []; | |
6d3bdc98 TO |
86 | \CRM_Utils_Hook::apiWrappers($apiRequest['wrappers'], $apiRequest); |
87 | } | |
88 | return $apiRequest['wrappers']; | |
89 | } | |
96025800 | 90 | |
6550386a | 91 | } |