eca-1.0.x-dev/modules/endpoint/src/Plugin/ECA/Event/EndpointEvent.php
modules/endpoint/src/Plugin/ECA/Event/EndpointEvent.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | <?php namespace Drupal\eca_endpoint\Plugin\ECA\Event; use Drupal\Core\Form\FormStateInterface; use Drupal\eca\Attributes\Token; use Drupal\eca\Entity\Objects\EcaEvent; use Drupal\eca\Event\Tag; use Drupal\eca\Plugin\ECA\Event\EventBase; use Drupal\eca_endpoint\EndpointEvents; use Drupal\eca_endpoint\Event\EndpointAccessEvent; use Drupal\eca_endpoint\Event\EndpointEventBase; use Drupal\eca_endpoint\Event\EndpointResponseEvent; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Contracts\EventDispatcher\Event; /** * Plugin implementation of ECA endpoint events. * * @EcaEvent( * id = "eca_endpoint", * deriver = "Drupal\eca_endpoint\Plugin\ECA\Event\EndpointEventDeriver", * eca_version_introduced = "1.1.0" * ) */ class EndpointEvent extends EventBase { /** * The endpoint base path. * * @var string */ protected string $endpointBasePath ; /** * {@inheritdoc} */ public static function create(ContainerInterface $container , array $configuration , $plugin_id , $plugin_definition ): static { $instance = parent::create( $container , $configuration , $plugin_id , $plugin_definition ); $instance ->endpointBasePath = $container ->getParameter( 'eca_endpoint.base_path' ); return $instance ; } /** * {@inheritdoc} */ public static function definitions(): array { $definitions = []; $definitions [ 'response' ] = [ 'label' => 'ECA Endpoint response' , 'event_name' => EndpointEvents::RESPONSE, 'event_class' => EndpointResponseEvent:: class , 'tags' => Tag::RUNTIME, ]; $definitions [ 'access' ] = [ 'label' => 'ECA Endpoint access' , 'event_name' => EndpointEvents::ACCESS, 'event_class' => EndpointAccessEvent:: class , 'tags' => Tag::RUNTIME | Tag::BEFORE, ]; return $definitions ; } /** * {@inheritdoc} */ public function defaultConfiguration(): array { return [ 'first_path_argument' => '' , 'second_path_argument' => '' , ] + parent::defaultConfiguration(); } /** * {@inheritdoc} */ public function buildConfigurationForm( array $form , FormStateInterface $form_state ): array { $form [ 'first_path_argument' ] = [ '#type' => 'textfield' , '#title' => $this ->t( 'First path argument' ), '#default_value' => $this ->configuration[ 'first_path_argument' ], '#description' => $this ->t( 'The <strong>first</strong> path argument to match up. This argument will be resolved from the URL path <em>/eca/<strong>{first}</strong>/{second}</em>.' ), '#required' => TRUE, '#weight' => 10, ]; $form [ 'second_path_argument' ] = [ '#type' => 'textfield' , '#title' => $this ->t( 'Second path argument' ), '#default_value' => $this ->configuration[ 'second_path_argument' ], '#description' => $this ->t( 'Optionally specify a second path argument to match up. This argument will be resolved from the URL path <em>/eca/{first}/<strong>{second}</strong></em>.' ), '#required' => FALSE, '#weight' => 20, ]; return parent::buildConfigurationForm( $form , $form_state ); } /** * {@inheritdoc} */ public function submitConfigurationForm( array & $form , FormStateInterface $form_state ): void { parent::submitConfigurationForm( $form , $form_state ); $this ->configuration[ 'first_path_argument' ] = $form_state ->getValue( 'first_path_argument' ); $this ->configuration[ 'second_path_argument' ] = $form_state ->getValue( 'second_path_argument' ); } /** * {@inheritdoc} */ public function generateWildcard(string $eca_config_id , EcaEvent $ecaEvent ): string { switch ( $this ->getDerivativeId()) { case 'response' : case 'access' : $configuration = $ecaEvent ->getConfiguration(); $first_path_argument = trim((string) ( $configuration [ 'first_path_argument' ] ?? '' )); $second_path_argument = trim((string) ( $configuration [ 'second_path_argument' ] ?? '' )); $wildcard = '' ; $wildcard .= $first_path_argument === '' ? '*' : $first_path_argument ; $wildcard .= '::' ; $wildcard .= $second_path_argument === '' ? '*' : $second_path_argument ; return $wildcard ; default : return parent::generateWildcard( $eca_config_id , $ecaEvent ); } } /** * {@inheritdoc} */ public static function appliesForWildcard(Event $event , string $event_name , string $wildcard ): bool { if ( $event instanceof EndpointEventBase) { [ $first_path_argument , $second_path_argument ] = explode ( '::' , $wildcard , 2); if (( $first_path_argument !== '*' ) && (reset( $event ->pathArguments) !== $first_path_argument )) { return FALSE; } if (( $second_path_argument !== '*' ) && (next( $event ->pathArguments) !== $second_path_argument )) { return FALSE; } return TRUE; } return FALSE; } /** * {@inheritdoc} */ #[Token( name: 'event' , description: 'The event.' , classes: [ EndpointAccessEvent:: class , ], properties: [ new Token(name: 'arguments' , description: 'The arguments of the request path.' ), new Token(name: 'uid' , description: 'The ID of the user account of the request.' ), ], )] #[Token( name: 'event' , description: 'The event.' , classes: [ EndpointResponseEvent:: class , ], properties: [ new Token(name: 'path_arguments' , description: 'The arguments of the request path.' ), new Token(name: 'request' , description: 'The request.' , properties: [ new Token(name: 'method' , description: 'The request method, e.g. "GET" or "POST".' ), new Token(name: 'path' , description: 'The requested path.' ), new Token(name: 'query' , description: 'The query arguments of the request.' ), new Token(name: 'headers' , description: 'The request headers.' ), new Token(name: 'content_type' , description: 'The content type of the request.' ), new Token(name: 'content' , description: 'The content of the POST request.' ), new Token(name: 'ip' , description: 'The client IP.' ), ]), new Token(name: 'uid' , description: 'The ID of the user account of the request.' ), ], )] protected function buildEventData(): array { $event = $this ->event; $data = []; if ( $event instanceof EndpointAccessEvent) { $data += [ 'arguments' => $event ->pathArguments, 'uid' => $event ->account->id(), ]; } elseif ( $event instanceof EndpointResponseEvent) { $data += [ 'path_arguments' => $event ->pathArguments, 'request' => [ 'method' => $event ->request->getMethod(), 'path' => $event ->request->getPathInfo(), 'query' => $event ->request->query->all(), 'headers' => $event ->request->headers->all(), 'content_type' => $event ->request->getContentTypeFormat(), 'content' => (string) $event ->request->getContent(), 'ip' => $event ->request->getClientIp(), ], 'uid' => $event ->account->id(), ]; } $data += parent::buildEventData(); return $data ; } } |