3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 namespace Civi\API\Provider
;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
34 * An adhoc provider is useful for creating mock API implementations.
36 class AdhocProvider
implements EventSubscriberInterface
, ProviderInterface
{
41 public static function getSubscribedEvents() {
42 // Using a high priority allows adhoc implementations
43 // to override standard implementations -- which is
44 // handy for testing/mocking.
46 Events
::RESOLVE
=> array(
47 array('onApiResolve', Events
::W_EARLY
),
49 Events
::AUTHORIZE
=> array(
50 array('onApiAuthorize', Events
::W_EARLY
),
56 * @var array (string $name => array('perm' => string, 'callback' => callable))
58 protected $actions = array();
73 * @param string $entity
76 public function __construct($version, $entity) {
77 $this->entity
= $entity;
78 $this->version
= $version;
87 * Permissions required for invoking the action.
88 * @param mixed $callback
89 * The function which executes the API.
90 * @return ReflectionProvider
92 public function addAction($name, $perm, $callback) {
93 $this->actions
[strtolower($name)] = array(
95 'callback' => $callback,
101 * @param \Civi\API\Event\ResolveEvent $event
102 * API resolution event.
104 public function onApiResolve(\Civi\API\Event\ResolveEvent
$event) {
105 $apiRequest = $event->getApiRequest();
106 if ($this->matchesRequest($apiRequest)) {
107 $event->setApiRequest($apiRequest);
108 $event->setApiProvider($this);
109 $event->stopPropagation();
114 * @param \Civi\API\Event\AuthorizeEvent $event
115 * API authorization event.
117 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent
$event) {
118 $apiRequest = $event->getApiRequest();
119 if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission
::check($this->actions
[strtolower($apiRequest['action'])]['perm'])) {
121 $event->stopPropagation();
127 * @param array $apiRequest
128 * @return array|mixed
130 public function invoke($apiRequest) {
131 return call_user_func($this->actions
[strtolower($apiRequest['action'])]['callback'], $apiRequest);
136 * @param int $version
139 public function getEntityNames($version) {
140 return array($this->entity
);
145 * @param int $version
146 * @param string $entity
149 public function getActionNames($version, $entity) {
150 if ($version == $this->version
&& $entity == $this->entity
) {
151 return array_keys($this->actions
);
159 * @param array $apiRequest
160 * The full description of the API request.
164 public function matchesRequest($apiRequest) {
165 return $apiRequest['entity'] == $this->entity
&& $apiRequest['version'] == $this->version
&& isset($this->actions
[strtolower($apiRequest['action'])]);