Commit a3f5ca8a authored by catch's avatar catch
Browse files

Issue #2338747 by dawehner, chx: Move {router} out of system.install and create the table lazy

parent 252fee22
Loading
Loading
Loading
Loading
+92 −1
Changes for core/lib/Drupal/Core/Routing/MatcherDumper.php: 92 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@

namespace Drupal\Core\Routing;

use Drupal\Core\Database\Query\Query;
use Drupal\Core\Database\SchemaObjectExistsException;
use Drupal\Core\State\StateInterface;
use Symfony\Component\Routing\RouteCollection;

@@ -14,6 +16,8 @@

/**
 * Dumps Route information to a database table.
 *
 * @see \Drupal\Core\Routing\RouteProvider
 */
class MatcherDumper implements MatcherDumperInterface {

@@ -97,7 +101,13 @@ public function dump(array $options = array()) {
    try {
      // We don't use truncate, because it is not guaranteed to be transaction
      // safe.
      $this->connection->delete($this->tableName)->execute();
      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);
@@ -162,4 +172,85 @@ public function getRoutes() {
    return $this->routes;
  }

  /**
   * Checks if the tree table exists and create it if not.
   *
   * @return bool
   *   TRUE if the table was created, FALSE otherwise.
   */
  protected function ensureTableExists() {
    try {
      if (!$this->connection->schema()->tableExists($this->tableName)) {
        $this->connection->schema()->createTable($this->tableName, $this->schemaDefinition());
        return TRUE;
      }
    }
    catch (SchemaObjectExistsException $e) {
      // If another process has already created the config table, attempting to
      // recreate it will throw an exception. In this case just catch the
      // exception and do nothing.
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Defines the schema for the router table.
   *
   * @return array
   *   The schema API definition for the SQL storage table.
   */
  protected function schemaDefinition() {
    $schema = [
      'description' => 'Maps paths to various callbacks (access, page and title)',
      'fields' => [
        'name' => [
          'description' => 'Primary Key: Machine name of this route',
          'type' => 'varchar_ascii',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ],
        'path' => [
          'description' => 'The path for this URI',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ],
        'pattern_outline' => [
          'description' => 'The pattern',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ],
        'fit' => [
          'description' => 'A numeric representation of how specific the path is.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
        ],
        'route' => [
          'description' => 'A serialized Route object',
          'type' => 'blob',
          'size' => 'big',
        ],
        'number_parts' => [
          'description' => 'Number of parts in this router path.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'size' => 'small',
        ],
      ],
      'indexes' => [
        'pattern_outline_parts' => ['pattern_outline', 'number_parts'],
      ],
      'primary key' => ['name'],
    ];

    return $schema;
  }

}
+10 −0
Changes for core/lib/Drupal/Core/Routing/RouteProvider.php: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -210,7 +210,12 @@ public function preLoadRoutes($names) {
        $routes = $cache->data;
      }
      else {
        try {
          $result = $this->connection->query('SELECT name, route FROM {' . $this->connection->escapeTable($this->tableName) . '} WHERE name IN ( :names[] )', array(':names[]' => $routes_to_load));
        }
        catch (\Exception $e) {
          $result = [];
        }
        $routes = $result->fetchAllKeyed();

        $this->cache->set($cid, $routes, Cache::PERMANENT, ['routes']);
@@ -336,10 +341,15 @@ protected function getRoutesByPath($path) {
    // The >= check on number_parts allows us to match routes with optional
    // trailing wildcard parts as long as the pattern matches, since we
    // dump the route pattern without those optional parts.
    try {
      $routes = $this->connection->query("SELECT name, route, fit FROM {" . $this->connection->escapeTable($this->tableName) . "} WHERE pattern_outline IN ( :patterns[] ) AND number_parts >= :count_parts", array(
        ':patterns[]' => $ancestors, ':count_parts' => count($parts),
      ))
        ->fetchAll(\PDO::FETCH_ASSOC);
    }
    catch (\Exception $e) {
      $routes = [];
    }

    // We sort by fit and name in PHP to avoid a SQL filesort.
    usort($routes, array($this, 'routeProviderRouteCompare'));
+0 −1
Changes for core/modules/breakpoint/src/Tests/BreakpointDiscoveryTest.php: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ class BreakpointDiscoveryTest extends KernelTestBase {

  protected function setUp() {
    parent::setUp();
    $this->installSchema('system', array('router'));
    \Drupal::service('theme_handler')->install(array('breakpoint_theme_test'));
  }

+0 −1
Changes for core/modules/comment/src/Tests/CommentDefaultFormatterCacheTagsTest.php: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -59,7 +59,6 @@ protected function setUp() {
    $this->installConfig(array('system', 'filter', 'comment'));

    // Comment rendering generates links, so build the router.
    $this->installSchema('system', array('router'));
    $this->container->get('router.builder')->rebuild();

    // Set up a field, so that the entity that'll be referenced bubbles up a
+0 −1
Changes for core/modules/comment/src/Tests/Migrate/d6/MigrateCommentTest.php: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ protected function setUp() {
    $this->installEntitySchema('node');
    $this->installEntitySchema('comment');
    $this->installSchema('comment', ['comment_entity_statistics']);
    $this->installSchema('system', ['router']);
    $this->installConfig(['comment']);

    // The entity.node.canonical route must exist when the RDF hook is called.
Loading