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