3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
;
30 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
33 * An adhoc provider is useful for creating mock API implementations.
35 class AdhocProvider
implements EventSubscriberInterface
, ProviderInterface
{
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.
45 Events
::RESOLVE
=> array(
46 array('onApiResolve', Events
::W_EARLY
),
48 Events
::AUTHORIZE
=> array(
49 array('onApiAuthorize', Events
::W_EARLY
),
55 * @var array (string $name => array('perm' => string, 'callback' => callable))
57 protected $actions = array();
72 * @param string $entity
75 public function __construct($version, $entity) {
76 $this->entity
= $entity;
77 $this->version
= $version;
86 * Permissions required for invoking the action.
87 * @param mixed $callback
88 * The function which executes the API.
89 * @return ReflectionProvider
91 public function addAction($name, $perm, $callback) {
92 $this->actions
[strtolower($name)] = array(
94 'callback' => $callback,
100 * @param \Civi\API\Event\ResolveEvent $event
101 * API resolution event.
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();
113 * @param \Civi\API\Event\AuthorizeEvent $event
114 * API authorization event.
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'])) {
120 $event->stopPropagation();
127 public function invoke($apiRequest) {
128 return call_user_func($this->actions
[strtolower($apiRequest['action'])]['callback'], $apiRequest);
134 public function getEntityNames($version) {
135 return array($this->entity
);
141 public function getActionNames($version, $entity) {
142 if ($version == $this->version
&& $entity == $this->entity
) {
143 return array_keys($this->actions
);
151 * @param array $apiRequest
152 * The full description of the API request.
156 public function matchesRequest($apiRequest) {
157 return $apiRequest['entity'] == $this->entity
&& $apiRequest['version'] == $this->version
&& isset($this->actions
[strtolower($apiRequest['action'])]);