array( array('onApiResolve', Events::W_EARLY), ), Events::AUTHORIZE => array( array('onApiAuthorize', Events::W_EARLY), ), ); } /** * @var array (string $name => array('perm' => string, 'callback' => callable)) */ protected $actions = array(); /** * @var string */ protected $entity; /** * @var int */ protected $version; /** * @param int $version * API version. * @param string $entity * API entity. */ public function __construct($version, $entity) { $this->entity = $entity; $this->version = $version; } /** * Register a new API. * * @param string $name * Action name. * @param string $perm * Permissions required for invoking the action. * @param mixed $callback * The function which executes the API. * @return ReflectionProvider */ public function addAction($name, $perm, $callback) { $this->actions[strtolower($name)] = array( 'perm' => $perm, 'callback' => $callback, ); return $this; } /** * @param \Civi\API\Event\ResolveEvent $event * API resolution event. */ public function onApiResolve(\Civi\API\Event\ResolveEvent $event) { $apiRequest = $event->getApiRequest(); if ($this->matchesRequest($apiRequest)) { $event->setApiRequest($apiRequest); $event->setApiProvider($this); $event->stopPropagation(); } } /** * @param \Civi\API\Event\AuthorizeEvent $event * API authorization event. */ public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) { $apiRequest = $event->getApiRequest(); if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission::check($this->actions[strtolower($apiRequest['action'])]['perm'])) { $event->authorize(); $event->stopPropagation(); } } /** * {inheritdoc} * @param array $apiRequest * @return array|mixed */ public function invoke($apiRequest) { return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest); } /** * {inheritdoc} * @param int $version * @return array */ public function getEntityNames($version) { return array($this->entity); } /** * {inheritdoc} * @param int $version * @param string $entity * @return array */ public function getActionNames($version, $entity) { if ($version == $this->version && $entity == $this->entity) { return array_keys($this->actions); } else { return array(); } } /** * @param array $apiRequest * The full description of the API request. * * @return bool */ public function matchesRequest($apiRequest) { return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]); } }