function MatcherDumper::dump

Same name in this branch
  1. 10 core/lib/Drupal/Core/ProxyClass/Routing/MatcherDumper.php \Drupal\Core\ProxyClass\Routing\MatcherDumper::dump()
Same name in other branches
  1. 9 core/lib/Drupal/Core/ProxyClass/Routing/MatcherDumper.php \Drupal\Core\ProxyClass\Routing\MatcherDumper::dump()
  2. 9 core/lib/Drupal/Core/Routing/MatcherDumper.php \Drupal\Core\Routing\MatcherDumper::dump()
  3. 8.9.x core/lib/Drupal/Core/ProxyClass/Routing/MatcherDumper.php \Drupal\Core\ProxyClass\Routing\MatcherDumper::dump()
  4. 8.9.x core/lib/Drupal/Core/Routing/MatcherDumper.php \Drupal\Core\Routing\MatcherDumper::dump()
  5. 11.x core/lib/Drupal/Core/ProxyClass/Routing/MatcherDumper.php \Drupal\Core\ProxyClass\Routing\MatcherDumper::dump()
  6. 11.x 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.

Throws

\Exception Thrown if the table could not be created or the database connection failed.

File

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

Class

MatcherDumper
Dumps Route information to a database table.

Namespace

Drupal\Core\Routing

Code

public function dump(array $options = []) : string {
    // 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.
    try {
        $transaction = $this->connection
            ->startTransaction();
        // We don't use truncate, because it is not guaranteed to be transaction
        // safe.
        try {
            $this->connection
                ->delete($this->tableName)
                ->execute();
        } catch (\Exception $e) {
            if (!$this->ensureTableExists()) {
                throw $e;
            }
        }
        // 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) {
        if (isset($transaction)) {
            $transaction->rollBack();
        }
        Error::logException($this->logger, $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;
    return '';
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.