dev/core#1744 - Civi/Token - Simplify event naming
authorTim Otten <totten@civicrm.org>
Wed, 6 May 2020 02:12:53 +0000 (19:12 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 6 May 2020 02:51:57 +0000 (19:51 -0700)
Civi/Token/AbstractTokenSubscriber.php
Civi/Token/Event/TokenRegisterEvent.php
Civi/Token/Event/TokenRenderEvent.php
Civi/Token/Event/TokenValueEvent.php
Civi/Token/Events.php
Civi/Token/TokenCompatSubscriber.php
Civi/Token/TokenProcessor.php
tests/phpunit/Civi/Token/TokenProcessorTest.php

index 9af4ca685442175cfed6d491a546832e9a9e328e..4f167828763e390d4ecc21e478362f01d613f66f 100644 (file)
@@ -41,8 +41,8 @@ abstract class AbstractTokenSubscriber implements EventSubscriberInterface {
 
   public static function getSubscribedEvents() {
     return [
-      Events::TOKEN_REGISTER => 'registerTokens',
-      Events::TOKEN_EVALUATE => 'evaluateTokens',
+      'civi.token.list' => 'registerTokens',
+      'civi.token.eval' => 'evaluateTokens',
       \Civi\ActionSchedule\Events::MAILING_QUERY => 'alterActionScheduleQuery',
     ];
   }
index c7fdfa01fe7ba7228b535a27fa8ce1f7ca890434..ee867b949e895594206032a8223b045ddb7a992b 100644 (file)
@@ -18,6 +18,8 @@ namespace Civi\Token\Event;
  *   'label' => ts('Default Profile URL (View Mode)'),
  * ));
  * @endcode
+ *
+ * Event name: 'civi.token.list'
  */
 class TokenRegisterEvent extends TokenEvent {
 
index 3fda8442eb663aaa2bc1543f7157d613ea4a724e..5c1612e49ceb1ff22617a8164d374e1ee9d39756 100644 (file)
@@ -5,13 +5,15 @@ namespace Civi\Token\Event;
  * Class TokenRenderEvent
  * @package Civi\Token\Event
  *
- * A TokenRenderEvent is fired after the TokenProcessor has rendered
- * a message.
+ * Perform post-processing on a rendered message. A TokenRenderEvent is fired
+ * after the TokenProcessor has rendered a message.
  *
- * The render event may be used for post-processing the text, but
+ * WARNING: The render event may be used for post-processing the text, but
  * it's very difficult to do substantive work in a secure, robust
  * way within this event. The event primarily exists to facilitate
  * a transition of some legacy code.
+ *
+ * Event name: 'civi.token.render'
  */
 class TokenRenderEvent extends TokenEvent {
 
index 5201ecdd6505599ac042dcabb8fbce04bb367833..c9a251ccc752ae05c24770372f7c5a1e4ce6470e 100644 (file)
@@ -32,6 +32,7 @@ namespace Civi\Token\Event;
  * }
  * @encode
  *
+ * Event name: 'civi.token.eval'
  */
 class TokenValueEvent extends TokenEvent {
 
index 7c7869ef0951b1482faba8965b423316c1e748e4..4f17c09f5441cf34c4fe664b64590e353f976499 100644 (file)
@@ -4,28 +4,20 @@ namespace Civi\Token;
 
 class Events {
   /**
-   * Create a list of supported tokens.
-   *
    * @see \Civi\Token\Event\TokenRegisterEvent
+   * @deprecated - You may simply use the event name directly. dev/core#1744
    */
   const TOKEN_REGISTER = 'civi.token.list';
 
   /**
-   * Create a list of supported tokens.
-   *
    * @see \Civi\Token\Event\TokenValueEvent
+   * @deprecated - You may simply use the event name directly. dev/core#1744
    */
   const TOKEN_EVALUATE = 'civi.token.eval';
 
   /**
-   * Perform post-processing on a rendered message.
-   *
-   * WARNING: It is difficult to develop robust,
-   * secure code using this stage. However, we need
-   * to support it during a transitional period
-   * while the token logic is reorganized.
-   *
    * @see \Civi\Token\Event\TokenRenderEvent
+   * @deprecated - You may simply use the event name directly. dev/core#1744
    */
   const TOKEN_RENDER = 'civi.token.render';
 
index ae8a55f616652c890b199343a11f72ac64c8d5bc..c4a07af5595c81771897e0c3d0a486bd9d2d53c5 100644 (file)
@@ -25,8 +25,8 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      Events::TOKEN_EVALUATE => 'onEvaluate',
-      Events::TOKEN_RENDER => 'onRender',
+      'civi.token.eval' => 'onEvaluate',
+      'civi.token.render' => 'onRender',
     ];
   }
 
index e27705300d9d9f9fa6a6a775cde796c56bed751d..0d1ff138db5038887eabfea041e2678d365e6df0 100644 (file)
@@ -306,7 +306,7 @@ class TokenProcessor {
     if ($this->tokens === NULL) {
       $this->tokens = [];
       $event = new TokenRegisterEvent($this, ['entity' => 'undefined']);
-      $this->dispatcher->dispatch(Events::TOKEN_REGISTER, $event);
+      $this->dispatcher->dispatch('civi.token.list', $event);
     }
     return $this->tokens;
   }
@@ -332,7 +332,7 @@ class TokenProcessor {
    */
   public function evaluate() {
     $event = new TokenValueEvent($this);
-    $this->dispatcher->dispatch(Events::TOKEN_EVALUATE, $event);
+    $this->dispatcher->dispatch('civi.token.eval', $event);
     return $this;
   }
 
@@ -371,7 +371,7 @@ class TokenProcessor {
     $event->context = $row->context;
     $event->row = $row;
     $event->string = strtr($message['string'], $filteredTokens);
-    $this->dispatcher->dispatch(Events::TOKEN_RENDER, $event);
+    $this->dispatcher->dispatch('civi.token.render', $event);
     return $event->string;
   }
 
index bbef30cfe203b55461b8e626e8c597b0789de9af..f4e667a166154cd9dd10c73b2c0b8845ab7e140c 100644 (file)
@@ -22,8 +22,8 @@ class TokenProcessorTest extends \CiviUnitTestCase {
     $this->useTransaction(TRUE);
     parent::setUp();
     $this->dispatcher = new EventDispatcher();
-    $this->dispatcher->addListener(Events::TOKEN_REGISTER, [$this, 'onListTokens']);
-    $this->dispatcher->addListener(Events::TOKEN_EVALUATE, [$this, 'onEvalTokens']);
+    $this->dispatcher->addListener('civi.token.list', [$this, 'onListTokens']);
+    $this->dispatcher->addListener('civi.token.eval', [$this, 'onEvalTokens']);
     $this->counts = [
       'onListTokens' => 0,
       'onEvalTokens' => 0,