Merge pull request #16429 from ixiam/dev/core#1113
[civicrm-core.git] / Civi / API / Event / PrepareEvent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\API\Event;
13
14 use Civi\API\Provider\WrappingProvider;
15
16 /**
17 * Class PrepareEvent
18 * @package Civi\API\Event
19 *
20 * Apply any pre-execution filtering to the API request.
21 *
22 * Event name: 'civi.api.prepare'
23 */
24 class PrepareEvent extends Event {
25
26 /**
27 * @param array $apiRequest
28 * The full description of the API request.
29 * @return PrepareEvent
30 */
31 public function setApiRequest($apiRequest) {
32 $this->apiRequest = $apiRequest;
33 return $this;
34 }
35
36 /**
37 * Replace the normal implementation of an API call with some wrapper.
38 *
39 * The wrapper has discretion to call -- or not call -- or iterate with --
40 * the original API implementation, with original or substituted arguments.
41 *
42 * Ex:
43 *
44 * $event->wrapApi(function($apiRequest, $continue){
45 * echo "Hello\n";
46 * $continue($apiRequest);
47 * echo "Goodbye\n";
48 * });
49 *
50 * @param callable $callback
51 * The custom API implementation.
52 * Function(array $apiRequest, callable $continue).
53 * @return PrepareEvent
54 */
55 public function wrapApi($callback) {
56 $this->apiProvider = new WrappingProvider($callback, $this->apiProvider);
57 return $this;
58 }
59
60 }