Add ablity to dump a route collection to the database.
parent
2ed208b1e0
commit
f14521489a
|
@ -32,8 +32,17 @@ class UrlMatcherDumper implements MatcherDumperInterface {
|
|||
*/
|
||||
protected $routes;
|
||||
|
||||
public function __construct(Connection $connection) {
|
||||
/**
|
||||
* The name of the SQL table to which to dump the routes.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tableName;
|
||||
|
||||
public function __construct(Connection $connection, $table = 'router') {
|
||||
$this->connection = $connection;
|
||||
|
||||
$this->tableName = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -67,15 +76,31 @@ class UrlMatcherDumper implements MatcherDumperInterface {
|
|||
'route_set' => '',
|
||||
);
|
||||
|
||||
$compiled = $this->compileRoutes($this->routes, $route_set);
|
||||
//$compiled = $this->compileRoutes($this->routes, $route_set);
|
||||
|
||||
// Convert all of the routes into database records.
|
||||
$insert = $this->connection->insert('router');
|
||||
$insert = $this->connection->insert($this->tableName)->fields(array(
|
||||
'name',
|
||||
'route_set',
|
||||
'fit',
|
||||
'pattern',
|
||||
'pattern_outline',
|
||||
'route',
|
||||
));
|
||||
|
||||
|
||||
|
||||
foreach ($this->routes as $name => $route) {
|
||||
$insert->values($record);
|
||||
$compiled = $route->compile();
|
||||
$values = array(
|
||||
'name' => $name,
|
||||
'route_set' => $options['route_set'],
|
||||
'fit' => $compiled->getFit(),
|
||||
'pattern' => $compiled->getPattern(),
|
||||
'pattern_outline' => $compiled->getPatternOutline(),
|
||||
'route' => serialize($route),
|
||||
);
|
||||
$insert->values($values);
|
||||
}
|
||||
|
||||
// Delete any old records in this route set first, then insert the new ones.
|
||||
|
@ -83,7 +108,7 @@ class UrlMatcherDumper implements MatcherDumperInterface {
|
|||
// unstable router states due to random failures.
|
||||
$txn = $this->connection->startTransaction();
|
||||
|
||||
$this->connection->delete('router')
|
||||
$this->connection->delete($this->tableName)
|
||||
->condition('route_set', $options['route_set'])
|
||||
->execute();
|
||||
|
||||
|
|
|
@ -100,10 +100,39 @@ class UrlMatcherDumperTestCase extends WebTestBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Confirms that we can add routes to the dumper when it already has some.
|
||||
* Confirm that we can dump a route collection to the database.
|
||||
*/
|
||||
protected function prepareTables() {
|
||||
public function testDump() {
|
||||
$connection = Database::getConnection();
|
||||
$dumper= new UrlMatcherDumper($connection, 'test_routes');
|
||||
|
||||
$route = new Route('/test/{my}/path');
|
||||
$route->setOption('compiler_class', 'Drupal\Core\Routing\RouteCompiler');
|
||||
$collection = new RouteCollection();
|
||||
$collection->add('test_route', $route);
|
||||
|
||||
$dumper->addRoutes($collection);
|
||||
|
||||
$this->prepareTables($connection);
|
||||
|
||||
$dumper->dump(array('route_set' => 'test'));
|
||||
|
||||
$record = $connection->query("SELECT * FROM {test_routes} WHERE name= :name", array(':name' => 'test_route'))->fetchObject();
|
||||
|
||||
$loaded_route = unserialize($record->route);
|
||||
|
||||
$this->assertEqual($record->name, 'test_route', t('Dumped route has correct name.'));
|
||||
$this->assertEqual($record->pattern, '/test/{my}/path', t('Dumped route has correct pattern.'));
|
||||
$this->assertEqual($record->pattern_outline, '/test/%/path', t('Dumped route has correct pattern outline.'));
|
||||
$this->assertEqual($record->fit, 5 /* 101 in binary */, t('Dumped route has correct fit.'));
|
||||
$this->assertTrue($loaded_route instanceof Route, t('Route object retrieved successfully.'));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a test database table just for unit testing purposes.
|
||||
*/
|
||||
protected function prepareTables($connection) {
|
||||
|
||||
$tables['test_routes'] = array(
|
||||
'description' => 'Maps paths to various callbacks (access, page and title)',
|
||||
|
@ -129,6 +158,13 @@ class UrlMatcherDumperTestCase extends WebTestBase {
|
|||
'not null' => TRUE,
|
||||
'default' => '',
|
||||
),
|
||||
'route_set' => array(
|
||||
'description' => 'The route set grouping to which a route belongs.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '',
|
||||
),
|
||||
'access_callback' => array(
|
||||
'description' => 'The callback which determines the access to this router path. Defaults to user_access.',
|
||||
'type' => 'varchar',
|
||||
|
@ -162,6 +198,7 @@ class UrlMatcherDumperTestCase extends WebTestBase {
|
|||
'indexes' => array(
|
||||
'fit' => array('fit'),
|
||||
'pattern_outline' => array('pattern_outline'),
|
||||
'route_set' => array('route_set'),
|
||||
),
|
||||
'primary key' => array('name'),
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue