Commit | Line | Data |
---|---|---|
6d3bdc98 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.4 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | namespace Civi\API\Subscriber; | |
29 | use Civi\API\Events; | |
30 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
31 | ||
32 | /** | |
33 | * This is a wrapper for the legacy "API Wrapper" interface which allows | |
34 | * wrappers to run through the new kernel. It translates from dispatcher events | |
35 | * ('api.prepare', 'api.respond') to wrapper calls ('fromApiInput', 'toApiOutput'). | |
36 | */ | |
37 | class WrapperAdapter implements EventSubscriberInterface { | |
38 | ||
39 | public static function getSubscribedEvents() { | |
40 | return array( | |
41 | Events::PREPARE => array('onApiPrepare', Events::W_MIDDLE), | |
42 | Events::RESPOND => array('onApiRespond', Events::W_EARLY), | |
43 | ); | |
44 | } | |
45 | ||
46 | /** | |
47 | * @var array(\API_Wrapper) | |
48 | */ | |
49 | protected $defaults; | |
50 | ||
51 | function __construct($defaults = array()) { | |
52 | $this->defaults = $defaults; | |
53 | } | |
54 | ||
55 | public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { | |
56 | $apiRequest = $event->getApiRequest(); | |
57 | ||
58 | // For input filtering, process $apiWrappers in forward order | |
59 | foreach ($this->getWrappers($apiRequest) as $apiWrapper) { | |
60 | $apiRequest = $apiWrapper->fromApiInput($apiRequest); | |
61 | } | |
62 | ||
63 | $event->setApiRequest($apiRequest); | |
64 | } | |
65 | ||
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 | |
80 | * @return array<\API_Wrapper> | |
81 | */ | |
82 | public function getWrappers($apiRequest) { | |
83 | if (!isset($apiRequest['wrappers'])) { | |
84 | $apiRequest['wrappers'] = $this->defaults; | |
85 | \CRM_Utils_Hook::apiWrappers($apiRequest['wrappers'], $apiRequest); | |
86 | } | |
87 | return $apiRequest['wrappers']; | |
88 | } | |
89 | } |