public function MatcherDumper::dump

public MatcherDumper::dump(array $options = array())

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.

Overrides MatcherDumperInterface::dump

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 = array()) {
  // 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, array()));
  // 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(array(
        'name',
        'fit',
        'path',
        'pattern_outline',
        'number_parts',
        'route',
      ));
      $names = array();
      foreach ($routes as $name => $route) {
        /** @var \Symfony\Component\Routing\Route $route */
        $route->setOption('compiler_class', '\Drupal\Core\Routing\RouteCompiler');
        /** @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 = array(
          '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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Routing!MatcherDumper.php/function/MatcherDumper::dump/8.1.x