You are here

class RoutingTransactionNameSubscriber in New Relic 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/RoutingTransactionNameSubscriber.php \Drupal\new_relic_rpm\EventSubscriber\RoutingTransactionNameSubscriber
  2. 2.0.x src/EventSubscriber/RoutingTransactionNameSubscriber.php \Drupal\new_relic_rpm\EventSubscriber\RoutingTransactionNameSubscriber

Enhances routes with a transaction name or transaction name callback.

The transaction name defaults to the route name, but can be overridden later by further routing alterations. A transaction name callback may be set, and will be invoked by a Route Enhancer.

Hierarchy

Expanded class hierarchy of RoutingTransactionNameSubscriber

See also

\Drupal\new_relic_rpm\RouteEnhancer\TransactionNameEnhancer

1 file declares its use of RoutingTransactionNameSubscriber
RoutingTransactionNameSubscriberTest.php in tests/src/Unit/EventListener/RoutingTransactionNameSubscriberTest.php
1 string reference to 'RoutingTransactionNameSubscriber'
new_relic_rpm.services.yml in ./new_relic_rpm.services.yml
new_relic_rpm.services.yml
1 service uses RoutingTransactionNameSubscriber
new_relic_rpm.router_subscriber in ./new_relic_rpm.services.yml
Drupal\new_relic_rpm\EventSubscriber\RoutingTransactionNameSubscriber

File

src/EventSubscriber/RoutingTransactionNameSubscriber.php, line 20

Namespace

Drupal\new_relic_rpm\EventSubscriber
View source
class RoutingTransactionNameSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      RoutingEvents::ALTER => [
        'addTransactionNamesToRoutes',
      ],
    ];
  }

  /**
   * Add transaction names/callbacks to individual routes.
   */
  public function addTransactionNamesToRoutes(RouteBuildEvent $event) {
    $collection = $event
      ->getRouteCollection();

    // Set a transaction name for the route.
    foreach ($collection as $route_name => $route) {
      $route
        ->setDefault('_transaction_name', $route_name);
    }
    $entity_bundle_routes = [
      'entity.node.canonical',
      'entity.node.edit_form',
      'entity.taxonomy_term.canonical',
      'entity.taxonomy_term.edit_form',
      'entity.taxonomy_term.add_form',
    ];
    foreach ($entity_bundle_routes as $entity_bundle_route) {
      if ($route = $collection
        ->get($entity_bundle_route)) {
        $route
          ->setDefault('_transaction_name_callback', [
          self::class,
          'entityBundleRouteTransactionName',
        ]);
      }
    }
    if ($route = $collection
      ->get('node.add')) {
      $route
        ->setDefault('_transaction_name_callback', [
        self::class,
        'nodeAddTransactionName',
      ]);
    }
  }

  /**
   * Get the transaction name for an "entity.$TYPE.$OP" route.
   */
  public static function entityBundleRouteTransactionName(Request $request) {
    $name = $request->attributes
      ->get('_transaction_name');
    if (preg_match('/^entity\\.([a-z_]+)\\./', $name, $matches)) {
      $entity_type = $matches[1];
      if (($entity = $request->attributes
        ->get($entity_type)) && $entity instanceof EntityInterface) {
        return sprintf('%s:%s', $name, $entity
          ->bundle());
      }
    }
    return $name;
  }

  /**
   * Get the transaction name for an "node.add" route.
   */
  public static function nodeAddTransactionName(Request $request) {
    $name = $request->attributes
      ->get('_transaction_name');
    if (($node_type = $request->attributes
      ->get('node_type')) && $node_type instanceof EntityInterface) {
      return sprintf('%s:%s', $name, $node_type
        ->id());
    }
    return $name;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RoutingTransactionNameSubscriber::addTransactionNamesToRoutes public function Add transaction names/callbacks to individual routes.
RoutingTransactionNameSubscriber::entityBundleRouteTransactionName public static function Get the transaction name for an "entity.$TYPE.$OP" route.
RoutingTransactionNameSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RoutingTransactionNameSubscriber::nodeAddTransactionName public static function Get the transaction name for an "node.add" route.