You are here

public function MatcherDumper::dump in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Routing/MatcherDumper.php \Drupal\Core\Routing\MatcherDumper::dump()
  2. 9 core/lib/Drupal/Core/ProxyClass/Routing/MatcherDumper.php \Drupal\Core\ProxyClass\Routing\MatcherDumper::dump()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Routing/MatcherDumper.php \Drupal\Core\Routing\MatcherDumper::dump()

Dumps a set of routes to the router table in the database.

Available options:

  • provider: The route grouping that is being dumped. All existing routes with this provider will be deleted on dump.
  • base_class: The base class name.

Parameters

array $options: An array of options.

File

core/lib/Drupal/Core/Routing/MatcherDumper.php, line 87

Class

MatcherDumper
Dumps Route information to a database table.

Namespace

Drupal\Core\Routing

Code

public function dump(array $options = []) {

  // Convert all of the routes into database records.
  // Accumulate the menu masks on top of any we found before.
  $masks = array_flip($this->state
    ->get('routing.menu_masks.' . $this->tableName, []));

  // Delete any old records first, then insert the new ones. That avoids
  // stale data. The transaction makes it atomic to avoid unstable router
  // states due to random failures.
  $transaction = $this->connection
    ->startTransaction();
  try {

    // We don't use truncate, because it is not guaranteed to be transaction
    // safe.
    try {
      $this->connection
        ->delete($this->tableName)
        ->execute();
    } catch (\Exception $e) {
      $this
        ->ensureTableExists();
    }

    // Split the routes into chunks to avoid big INSERT queries.
    $route_chunks = array_chunk($this->routes
      ->all(), 50, TRUE);
    foreach ($route_chunks as $routes) {
      $insert = $this->connection
        ->insert($this->tableName)
        ->fields([
        'name',
        'fit',
        'path',
        'pattern_outline',
        'number_parts',
        'route',
      ]);
      $names = [];
      foreach ($routes as $name => $route) {

        /** @var \Symfony\Component\Routing\Route $route */
        $route
          ->setOption('compiler_class', RouteCompiler::class);

        /** @var \Drupal\Core\Routing\CompiledRoute $compiled */
        $compiled = $route
          ->compile();

        // The fit value is a binary number which has 1 at every fixed path
        // position and 0 where there is a wildcard. We keep track of all such
        // patterns that exist so that we can minimize the number of path
        // patterns we need to check in the RouteProvider.
        $masks[$compiled
          ->getFit()] = 1;
        $names[] = $name;
        $values = [
          'name' => $name,
          'fit' => $compiled
            ->getFit(),
          'path' => $route
            ->getPath(),
          'pattern_outline' => $compiled
            ->getPatternOutline(),
          'number_parts' => $compiled
            ->getNumParts(),
          'route' => serialize($route),
        ];
        $insert
          ->values($values);
      }

      // Insert all new routes.
      $insert
        ->execute();
    }
  } catch (\Exception $e) {
    $transaction
      ->rollBack();
    watchdog_exception('Routing', $e);
    throw $e;
  }

  // Sort the masks so they are in order of descending fit.
  $masks = array_keys($masks);
  rsort($masks);
  $this->state
    ->set('routing.menu_masks.' . $this->tableName, $masks);
  $this->routes = NULL;
}