Merge pull request #4696 from colemanw/CRM-15669
[civicrm-core.git] / Civi / API / Provider / AdhocProvider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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\Provider;
29 use Civi\API\Events;
30 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
31
32 /**
33 * An adhoc provider is useful for creating mock API implementations.
34 */
35 class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
36
37 /**
38 * @return array
39 */
40 public static function getSubscribedEvents() {
41 // Using a high priority allows adhoc implementations
42 // to override standard implementations -- which is
43 // handy for testing/mocking.
44 return array(
45 Events::RESOLVE => array(
46 array('onApiResolve', Events::W_EARLY),
47 ),
48 Events::AUTHORIZE => array(
49 array('onApiAuthorize', Events::W_EARLY),
50 ),
51 );
52 }
53
54 /**
55 * @var array (string $name => array('perm' => string, 'callback' => callable))
56 */
57 protected $actions = array();
58
59 /**
60 * @var string
61 */
62 protected $entity;
63
64 /**
65 * @var int
66 */
67 protected $version;
68
69 /**
70 * @param int $version
71 * API version.
72 * @param string $entity
73 * API entity.
74 */
75 public function __construct($version, $entity) {
76 $this->entity = $entity;
77 $this->version = $version;
78 }
79
80 /**
81 * Register a new API.
82 *
83 * @param string $name
84 * Action name.
85 * @param string $perm
86 * Permissions required for invoking the action.
87 * @param mixed $callback
88 * The function which executes the API.
89 * @return ReflectionProvider
90 */
91 public function addAction($name, $perm, $callback) {
92 $this->actions[strtolower($name)] = array(
93 'perm' => $perm,
94 'callback' => $callback,
95 );
96 return $this;
97 }
98
99 /**
100 * @param \Civi\API\Event\ResolveEvent $event
101 * API resolution event.
102 */
103 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
104 $apiRequest = $event->getApiRequest();
105 if ($this->matchesRequest($apiRequest)) {
106 $event->setApiRequest($apiRequest);
107 $event->setApiProvider($this);
108 $event->stopPropagation();
109 }
110 }
111
112 /**
113 * @param \Civi\API\Event\AuthorizeEvent $event
114 * API authorization event.
115 */
116 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
117 $apiRequest = $event->getApiRequest();
118 if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission::check($this->actions[strtolower($apiRequest['action'])]['perm'])) {
119 $event->authorize();
120 $event->stopPropagation();
121 }
122 }
123
124 /**
125 * {inheritdoc}
126 */
127 public function invoke($apiRequest) {
128 return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest);
129 }
130
131 /**
132 * {inheritdoc}
133 */
134 public function getEntityNames($version) {
135 return array($this->entity);
136 }
137
138 /**
139 * {inheritdoc}
140 */
141 public function getActionNames($version, $entity) {
142 if ($version == $this->version && $entity == $this->entity) {
143 return array_keys($this->actions);
144 }
145 else {
146 return array();
147 }
148 }
149
150 /**
151 * @param array $apiRequest
152 * The full description of the API request.
153 *
154 * @return bool
155 */
156 public function matchesRequest($apiRequest) {
157 return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]);
158 }
159 }