From 8d881ade8b73f48df4632141450278dc052bd5d0 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Fri, 24 Mar 2017 18:11:04 +0300 Subject: [PATCH] uri function restored, router available in a global scope by default --- .../Core/Bootloaders/SpiralBindings.php | 6 ++++- source/functions.php | 22 +++++++++++++++++++ tests/-app-/.env | 2 +- tests/Http/UriGenerationTest.php | 8 +++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/source/Spiral/Core/Bootloaders/SpiralBindings.php b/source/Spiral/Core/Bootloaders/SpiralBindings.php index 022beb31a..523ee55ae 100644 --- a/source/Spiral/Core/Bootloaders/SpiralBindings.php +++ b/source/Spiral/Core/Bootloaders/SpiralBindings.php @@ -10,6 +10,7 @@ use Psr\Http\Message\ServerRequestInterface; use Spiral\Core\Exceptions\ScopeException; +use Spiral\Http\HttpDispatcher; use Spiral\Http\Routing\RouteInterface; /** @@ -69,7 +70,6 @@ class SpiralBindings extends Bootloader //Http scope dependent 'cookies' => 'Spiral\Http\Cookies\CookieQueue', - 'router' => 'Spiral\Http\Routing\RouterInterface', 'session' => 'Spiral\Session\SessionInterface', //Pagination manager @@ -92,6 +92,10 @@ class SpiralBindings extends Bootloader //Scope depended 'actor' => 'Spiral\Security\ActorInterface', + //Default router is http specific + 'Spiral\Http\Routing\RouterInterface' => [HttpDispatcher::class, 'getRouter'], + 'router' => 'Spiral\Http\Routing\RouterInterface', + //Thought request attributes 'Spiral\Http\Routing\RouteInterface' => [self::class, 'activeRoute'], ]; diff --git a/source/functions.php b/source/functions.php index 6e8da714d..7b0d55a51 100644 --- a/source/functions.php +++ b/source/functions.php @@ -5,10 +5,12 @@ * @license MIT * @author Anton Titov (Wolfy-J) */ +use Psr\Http\Message\UriInterface; use Spiral\Core\Core; use Spiral\Core\DirectoriesInterface; use Spiral\Core\EnvironmentInterface; use Spiral\Debug\Dumper; +use Spiral\Http\Routing\RouterInterface; use Spiral\Translator\Exceptions\TranslatorException; use Spiral\Translator\TranslatorInterface; @@ -135,6 +137,26 @@ function interpolate( } } +if (!function_exists('uri')) { + /** + * Generate valid route URL using route name and set of parameters. Should support controller + * and action name separated by ":" - in this case router should find appropriate route and + * create url using it. + * + * @param string $route Route name. + * @param array|\Traversable $parameters + * + * @return UriInterface + * @throws \Spiral\Http\Exceptions\RouterException + * @throws \Spiral\Http\Exceptions\RouteException + * @throws \Spiral\Http\Exceptions\UndefinedRouteException + */ + function uri(string $route, $parameters = []): UriInterface + { + return spiral(RouterInterface::class)->uri($route, $parameters); + } +} + if (!function_exists('l')) { /** * Translate message using default or specific bundle name. diff --git a/tests/-app-/.env b/tests/-app-/.env index d42c72a25..cb35dfca0 100644 --- a/tests/-app-/.env +++ b/tests/-app-/.env @@ -9,7 +9,7 @@ DEBUG = true CACHE_BOOTLOADERS = false #Encryption key used by Encrypter component to encrypt/decrypt data and protect your cookies -SPIRAL_KEY = ZGVmMDAwMDAyYTAzNzZiMjhlMzU2ZDdkYWIzYzNjNWQxYmMyMmZiYjg0ZWY4ODA1ZGQ2ZTFkMjVjOGU1YWM3YjYyNWFhYjk2MDlkZjdiYWIyOGFkYmNjM2I0ZDMyZTJkMGE3YWRjNmM1OWJlZWJiODk1NmNhNzE3YTgwOGM0OTUyMTAxMTEzYw== +SPIRAL_KEY = ZGVmMDAwMDBjN2E5YTFiOGFkMDQ5YTgzYzk4ZmE3ZmRmYmFhZTZhOWM5MzczZDE2ZWJlZDExYjZkNmZhMGIwOTg4MmQ4NmEzNDY3NjhiN2YwYjAyMDcwZjZiOTM1YTZiM2FjMDkwNDAwNDljZDkwMGZhNTA4NWJlZWU2MzQ4M2I5ZDk1NjNlZQ== #Production applications must always have view cache turned on, disabled cache can only be useful #in development diff --git a/tests/Http/UriGenerationTest.php b/tests/Http/UriGenerationTest.php index 494b69bb8..cb32b7b98 100644 --- a/tests/Http/UriGenerationTest.php +++ b/tests/Http/UriGenerationTest.php @@ -113,4 +113,12 @@ public function testStrictUriWithQuery() $this->assertSame('/pattern?a=1', (string)$router->uri('name', ['a' => 1])); } + + public function testStrictUriWithQueryViaUri() + { + $router = $this->http->getRouter(); + $router->addRoute(new Route('name', '/pattern', 'target')); + + $this->assertSame('/pattern?a=1', (string)uri('name', ['a' => 1])); + } } \ No newline at end of file