Skip to content

Commit

Permalink
Merge branch '0.9' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielsolomon committed Mar 26, 2019
2 parents 4153179 + 7a15985 commit 7450cab
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 48 deletions.
35 changes: 35 additions & 0 deletions src/Generator/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nip\Router\Generator\Traits\HasPreviousUrlTrait;
use Nip\Router\Generator\Traits\UrlFunctionTrait;
use Nip\Router\RequestContext;
use Nip\Router\Route\Route;

/**
* Class UrlGenerator
Expand All @@ -32,4 +33,38 @@ public function setRequest(Request $request)
$this->cachedSchema = null;
return $this->setContext($context);
}

public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
{
$name = $this->initDefaultRoute($name, $parameters);
return parent::generate($name, $parameters, $referenceType);
}


/**
* @param $name
* @param array $params
* @return null|Route
*/
protected function initDefaultRoute($name, &$params = [])
{
$route = $this->routes->get($name);
if ($route) {
return $name;
}
$parts = explode(".", $name);
$count = count($parts);
if ($count <= 3) {
if (in_array(reset($parts), app('mvc.modules')->getNames())) {
$module = array_shift($parts);
$params['controller'] = isset($parts[0]) ? $parts[0] : null;
$params['action'] = isset($parts[1]) ? $parts[1] : null;
$route = $this->routes->get($module . '.default');
if ($route) {
return $route->getName();
}
}
}
return $name;
}
}
2 changes: 1 addition & 1 deletion src/RequestContext/Traits/HasUrlFunctionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ public function getHttpHost()

return $this->getHost() . ':' . $port;
}
}
}
2 changes: 1 addition & 1 deletion src/RouteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function generateStandardRoute(
$name,
$class,
$mapPrefix = '',
$map = '/:controller/:action',
$map = '/{controller}/{action?index}',
$params = []
) {
return self::generateGenericRoute($collection, $name, $class, $mapPrefix . $map, $params);
Expand Down
49 changes: 5 additions & 44 deletions src/Router/Traits/HasGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nip\Router\Router\Traits;

use Nip\Router\Route\Route;
use Nip\Router\Router;

/**
Expand All @@ -13,61 +14,21 @@ trait HasGeneratorTrait

/**
* @param $name
* @param boolean $params
* @param array $params
* @return string|null
*/
public function assemble($name, $params = [])
{
$route = $this->getDefaultRoute($name, $params);

if ($route) {
$route->setRequest($this->getRequest());
return $route->assemble($params);
}

trigger_error("Route \"$name\" not connected", E_USER_ERROR);
return null;
return $this->generate($name, $params);
}

/**
* @param $name
* @param boolean $params
* @param array $params
* @return string
*/
public function assembleFull($name, $params = [])
{
$route = $this->getDefaultRoute($name, $params);
if ($route) {
$route->setRequest($this->getRequest());
return $route->assembleFull($params);
}

trigger_error("Route \"$name\" not connected", E_USER_ERROR);

return null;
}

/**
* @param $name
* @param array $params
* @return null|Route\Route
*/
public function getDefaultRoute($name, &$params = [])
{
$route = $this->getRoute($name);
if (!$route) {
$parts = explode(".", $name);
$count = count($parts);
if ($count <= 3) {
if (in_array(reset($parts), app('mvc.modules')->getNames())) {
$module = array_shift($parts);
$params['controller'] = isset($parts[0]) ? $parts[0] : null;
$params['action'] = isset($parts[1]) ? $parts[1] : null;
$route = $this->getRoute($module . '.default');
}
}
}

return $route;
return $this->generate($name, $params, self::ABSOLUTE_URL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public function testPrependRoute()
$firstRoute = reset($allRoutes);
self::assertSame('route3', $firstRoute->getName());
}
}
}
10 changes: 9 additions & 1 deletion tests/src/Router/Traits/HasGeneratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nip\Router\Tests\Router\Traits;

use Nip\Router\Generator\UrlGenerator;
use Nip\Router\Route\Route;
use Nip\Router\RouteFactory;
use Nip\Router\Router;
Expand All @@ -14,6 +15,13 @@
*/
class HasGeneratorTraitTest extends AbstractTest
{

public function testGetGenerator()
{
$router = new Router();
self::assertInstanceOf(UrlGenerator::class, $router->getGenerator());
}

public function testAssemble()
{
$router = new Router();
Expand All @@ -28,7 +36,7 @@ public function testAssemble()
);

self::assertSame(
'/api/posts/',
'/api/posts',
$router->assemble('api.index', ['controller' => 'posts'])
);

Expand Down

0 comments on commit 7450cab

Please sign in to comment.