*/
class Event extends \Symfony\Component\EventDispatcher\Event {
+ use RequestTrait;
+
/**
* @var \Civi\API\Kernel
*/
*/
protected $apiProvider;
- /**
- * @var array
- * The full description of the API request.
- *
- * @see \Civi\API\Request::create
- */
- protected $apiRequest;
-
/**
* @param \Civi\API\Provider\ProviderInterface $apiProvider
* The API responsible for executing the request.
public function __construct($apiProvider, $apiRequest, $apiKernel) {
$this->apiKernel = $apiKernel;
$this->apiProvider = $apiProvider;
- $this->apiRequest = $apiRequest;
+ $this->setApiRequest($apiRequest);
}
/**
return $this->apiProvider;
}
- /**
- * @return array
- */
- public function getApiRequest() {
- return $this->apiRequest;
- }
-
- /**
- * Create a brief string identifying the entity/action. Useful for
- * pithy matching/switching.
- *
- * Ex: if ($e->getApiRequestSig() === '3.contact.get') { ... }
- *
- * @return string
- * Ex: '3.contact.get'
- */
- public function getApiRequestSig() {
- return mb_strtolower($this->apiRequest['version'] . '.' . $this->apiRequest['entity'] . '.' . $this->apiRequest['action']);
- }
-
}
/**
* @param array $apiRequest
* The full description of the API request.
- * @return PrepareEvent
+ * @return static
*/
public function setApiRequest($apiRequest) {
- $this->apiRequest = $apiRequest;
- return $this;
+ // Elevate from 'protected' to 'public'.
+ return parent::setApiRequest($apiRequest);
}
/**
--- /dev/null
+<?php
+
+namespace Civi\API\Event;
+
+/**
+ * Trait RequestTrait
+ * @package Civi\API\Event
+ *
+ * Most events emitted by the API subsystem should include information about the active API request.
+ */
+trait RequestTrait {
+
+ /**
+ * @var \Civi\Api4\Generic\AbstractAction|array
+ * The full description of the API request.
+ *
+ * @see \Civi\API\Request::create
+ */
+ protected $apiRequest;
+
+ /**
+ * @return \Civi\Api4\Generic\AbstractAction|array
+ */
+ public function getApiRequest() {
+ return $this->apiRequest;
+ }
+
+ /**
+ * @param \Civi\Api4\Generic\AbstractAction|array $apiRequest
+ * The full description of the API request.
+ * @return static
+ */
+ protected function setApiRequest($apiRequest) {
+ $this->apiRequest = $apiRequest;
+ return $this;
+ }
+
+ /**
+ * Create a brief string identifying the entity/action. Useful for
+ * pithy matching/switching.
+ *
+ * Ex: if ($e->getApiRequestSig() === '3.contact.get') { ... }
+ *
+ * @return string
+ * Ex: '3.contact.get'
+ */
+ public function getApiRequestSig(): string {
+ return mb_strtolower($this->apiRequest['version'] . '.' . $this->apiRequest['entity'] . '.' . $this->apiRequest['action']);
+ }
+
+ /**
+ * @return string
+ * Ex: 'Contact', 'Activity'
+ */
+ public function getEntityName(): string {
+ return $this->apiRequest['entity'];
+ }
+
+ /**
+ * @return string
+ * Ex: 'create', 'update'
+ */
+ public function getActionName(): string {
+ return $this->apiRequest['action'];
+ }
+
+}
* The full description of the API request.
*/
public function setApiRequest($apiRequest) {
- $this->apiRequest = $apiRequest;
+ // Elevate from 'protected' to 'public'.
+ return parent::setApiRequest($apiRequest);
}
}