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();
71 * @param string $entity
73 public function __construct($version, $entity) {
74 $this->entity
= $entity;
75 $this->version
= $version;
81 * @param callable $callback
82 * @return ReflectionProvider
84 public function addAction($name, $perm, $callback) {
85 $this->actions
[strtolower($name)] = array(
87 'callback' => $callback,
93 * @param \Civi\API\Event\ResolveEvent $event
95 public function onApiResolve(\Civi\API\Event\ResolveEvent
$event) {
96 $apiRequest = $event->getApiRequest();
97 if ($this->matchesRequest($apiRequest)) {
98 $event->setApiRequest($apiRequest);
99 $event->setApiProvider($this);
100 $event->stopPropagation();
105 * @param \Civi\API\Event\AuthorizeEvent $event
107 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent
$event) {
108 $apiRequest = $event->getApiRequest();
109 if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission
::check($this->actions
[strtolower($apiRequest['action'])]['perm'])) {
111 $event->stopPropagation();
118 public function invoke($apiRequest) {
119 return call_user_func($this->actions
[strtolower($apiRequest['action'])]['callback'], $apiRequest);
125 function getEntityNames($version) {
126 return array($this->entity
);
132 function getActionNames($version, $entity) {
133 if ($version == $this->version
&& $entity == $this->entity
) {
134 return array_keys($this->actions
);
146 public function matchesRequest($apiRequest) {
147 return $apiRequest['entity'] == $this->entity
&& $apiRequest['version'] == $this->version
&& isset($this->actions
[strtolower($apiRequest['action'])]);