Commit | Line | Data |
---|---|---|
6d3bdc98 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6d3bdc98 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6d3bdc98 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6d3bdc98 TO |
27 | |
28 | namespace Civi\API\Subscriber; | |
8882ff5c | 29 | |
6d3bdc98 TO |
30 | use Civi\API\Events; |
31 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
32 | ||
33 | /** | |
34 | * This is a wrapper for the legacy "API Wrapper" interface which allows | |
35 | * wrappers to run through the new kernel. It translates from dispatcher events | |
36 | * ('api.prepare', 'api.respond') to wrapper calls ('fromApiInput', 'toApiOutput'). | |
37 | */ | |
38 | class WrapperAdapter implements EventSubscriberInterface { | |
39 | ||
6550386a EM |
40 | /** |
41 | * @return array | |
42 | */ | |
6d3bdc98 TO |
43 | public static function getSubscribedEvents() { |
44 | return array( | |
45 | Events::PREPARE => array('onApiPrepare', Events::W_MIDDLE), | |
0a946de2 | 46 | Events::RESPOND => array('onApiRespond', Events::W_EARLY * 2), |
6d3bdc98 TO |
47 | ); |
48 | } | |
49 | ||
50 | /** | |
51 | * @var array(\API_Wrapper) | |
52 | */ | |
53 | protected $defaults; | |
54 | ||
6550386a EM |
55 | /** |
56 | * @param array $defaults | |
8882ff5c | 57 | * array(\API_Wrapper). |
6550386a | 58 | */ |
8882ff5c | 59 | public function __construct($defaults = array()) { |
6d3bdc98 TO |
60 | $this->defaults = $defaults; |
61 | } | |
62 | ||
6550386a EM |
63 | /** |
64 | * @param \Civi\API\Event\PrepareEvent $event | |
8882ff5c | 65 | * API preparation event. |
6550386a | 66 | */ |
6d3bdc98 TO |
67 | public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { |
68 | $apiRequest = $event->getApiRequest(); | |
69 | ||
70 | // For input filtering, process $apiWrappers in forward order | |
71 | foreach ($this->getWrappers($apiRequest) as $apiWrapper) { | |
72 | $apiRequest = $apiWrapper->fromApiInput($apiRequest); | |
73 | } | |
74 | ||
75 | $event->setApiRequest($apiRequest); | |
76 | } | |
77 | ||
6550386a EM |
78 | /** |
79 | * @param \Civi\API\Event\RespondEvent $event | |
8882ff5c | 80 | * API response event. |
6550386a | 81 | */ |
6d3bdc98 TO |
82 | public function onApiRespond(\Civi\API\Event\RespondEvent $event) { |
83 | $apiRequest = $event->getApiRequest(); | |
84 | $result = $event->getResponse(); | |
85 | ||
86 | // For output filtering, process $apiWrappers in reverse order | |
87 | foreach (array_reverse($this->getWrappers($apiRequest)) as $apiWrapper) { | |
88 | $result = $apiWrapper->toApiOutput($apiRequest, $result); | |
89 | } | |
90 | ||
91 | $event->setResponse($result); | |
92 | } | |
93 | ||
94 | /** | |
95 | * @param array $apiRequest | |
8882ff5c | 96 | * The full API request. |
6d3bdc98 TO |
97 | * @return array<\API_Wrapper> |
98 | */ | |
99 | public function getWrappers($apiRequest) { | |
100 | if (!isset($apiRequest['wrappers'])) { | |
101 | $apiRequest['wrappers'] = $this->defaults; | |
102 | \CRM_Utils_Hook::apiWrappers($apiRequest['wrappers'], $apiRequest); | |
103 | } | |
104 | return $apiRequest['wrappers']; | |
105 | } | |
96025800 | 106 | |
6550386a | 107 | } |