Skip to content

Commit

Permalink
[ADD] Methods Map::appendMap() and Map::prependMap(), to append or pr…
Browse files Browse the repository at this point in the history
…epend the definitions from another map.
  • Loading branch information
Paul M. Jones committed Oct 16, 2013
1 parent dad01e1 commit 98afc57
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Aura/Router/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,52 @@ public function attach($path_prefix, $spec)
);
}

/**
*
* Appends the definitions of another map to this map.
*
* @param Map $map The map to append.
*
* @return void
*
*/
public function appendMap(Map $map)
{
// append the map
$this->definitions = array_merge(
$this->definitions,
$map->definitions
);

// reset existing routes and tracking properties
$this->attach_common = null;
$this->attach_routes = null;
$this->routes = [];
}

/**
*
* Prepends the definitions of another map to this map.
*
* @param Map $map The map to prepend.
*
* @return void
*
*/
public function prependMap(Map $map)
{
// prepend the map
$this->definitions = array_merge(
$map->definitions,
$this->definitions
);

// reset existing routes and tracking properties
$this->attach_common = null;
$this->attach_routes = null;
$this->routes = [];
}

/**
*
* Gets a route that matches a given path and other server conditions.
Expand Down
26 changes: 26 additions & 0 deletions tests/Aura/Router/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,4 +856,30 @@ public function testGetLog()
// this is weak. we should actually see if the log contains anything.
$this->assertSame([], $this->map->getLog());
}

public function testAppendMap()
{
$append_map = $this->newMap();
$append_map->add('bar', '/path/to/bar');

$this->map->add('foo', '/path/to/foo');
$this->map->appendMap($append_map);

$actual = array_keys($this->map->getRoutes());
$expect = ['foo', 'bar'];
$this->assertSame($expect, $actual);
}

public function testPrependMap()
{
$prepend_map = $this->newMap();
$prepend_map->add('bar', '/path/to/bar');

$this->map->add('foo', '/path/to/foo');
$this->map->prependMap($prepend_map);

$actual = array_keys($this->map->getRoutes());
$expect = ['bar', 'foo'];
$this->assertSame($expect, $actual);
}
}

3 comments on commit 98afc57

@harikt
Copy link
Member

@harikt harikt commented on 98afc57 Oct 16, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like we are using different Maps for different packages ?

@pmjones
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No; the problem is that defining routes via the Map $di->params is more detailed than I'd like. The plan is to create a $router object (like we have $di, $system, and $config objects) and make it available to the config files. Then people can do $router->add('name', '/path/to/whatever') directly. The followup problem is that the $di->params for Map won't be honored, so the framework bootstrap creates a second router object to get the config definitions, and prepends them to the $router we already have. Yes, it's a hack, but it also makes things easier at the user end. /me wipes brow

@harikt
Copy link
Member

@harikt harikt commented on 98afc57 Oct 16, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.