diff --git a/src/Contracts/LocalizationInterface.php b/src/Contracts/LocalizationInterface.php index 1f31c2c..3a629b2 100644 --- a/src/Contracts/LocalizationInterface.php +++ b/src/Contracts/LocalizationInterface.php @@ -1,4 +1,5 @@ app->register(Providers\UtilitiesServiceProvider::class); $this->registerLocalization(); - $this->app->register(Providers\RouterServiceProvider::class); + $this->app->register(Providers\RoutingServiceProvider::class); } /** diff --git a/src/Middleware/LocaleCookieRedirect.php b/src/Middleware/LocaleCookieRedirect.php index 529d101..6e53633 100644 --- a/src/Middleware/LocaleCookieRedirect.php +++ b/src/Middleware/LocaleCookieRedirect.php @@ -53,6 +53,4 @@ public function handle(Request $request, Closure $next) return $next($request); } - - } diff --git a/src/Providers/RouterServiceProvider.php b/src/Providers/RoutingServiceProvider.php similarity index 68% rename from src/Providers/RouterServiceProvider.php rename to src/Providers/RoutingServiceProvider.php index ba83df1..ccd7491 100644 --- a/src/Providers/RouterServiceProvider.php +++ b/src/Providers/RoutingServiceProvider.php @@ -4,17 +4,16 @@ use Arcanedev\Localization\Middleware\LocaleSessionRedirect; use Arcanedev\Localization\Middleware\LocalizationRedirect; use Arcanedev\Localization\Middleware\LocalizationRoutes; -use Arcanedev\Support\ServiceProvider; -use Closure; -use Illuminate\Routing\Router; +use Illuminate\Routing\RoutingServiceProvider as ServiceProvider; +use Arcanedev\Localization\Routing\Router; /** - * Class RouterServiceProvider + * Class RoutingServiceProvider * * @package Arcanedev\Localization\Providers * @author ARCANEDEV */ -class RouterServiceProvider extends ServiceProvider +class RoutingServiceProvider extends ServiceProvider { /* ------------------------------------------------------------------------------------------------ | Properties @@ -38,10 +37,23 @@ class RouterServiceProvider extends ServiceProvider */ public function register() { - $router = $this->app['router']; + $this->registerRouter(); + } - $this->registerMiddlewares($router); - $this->registerMacros($router); + /* ------------------------------------------------------------------------------------------------ + | Router Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Register the router instance. + */ + protected function registerRouter() + { + $this->app['router'] = $this->app->share(function ($app) { + return new Router($app['events'], $app); + }); + + $this->registerMiddlewares($this->app['router']); } /** @@ -68,41 +80,23 @@ private function registerMiddleware(Router $router, $name, $class) { $router->middleware($name, $class); - if ($this->app['config']->get('localization.route.middleware.' . $name, false)) { + if ($this->getMiddleware($name)) { $this->middleware[] = $name; } } - /* ------------------------------------------------------------------------------------------------ - | Route Macros - | ------------------------------------------------------------------------------------------------ - */ /** - * Register the router macros. + * Get the middleware status. * - * @param Router $router - */ - private function registerMacros(Router $router) - { - $this->registerLocalizedGroupMacro($router); - } - - /** - * Register the 'localizedGroup' macro. + * @param string $name * - * @param Router $router + * @return bool */ - private function registerLocalizedGroupMacro(Router $router) + private function getMiddleware($name) { - $middleware = $this->middleware; + /** @var \Illuminate\Config\Repository $config */ + $config = $this->app['config']; - $router->macro('localizedGroup', function (Closure $callback) use ($router, $middleware) { - $attributes = [ - 'prefix' => localization()->setLocale(), - 'middleware' => $middleware, - ]; - - $router->group($attributes, $callback); - }); + return (bool) $config->get('localization.route.middleware.' . $name, false); } } diff --git a/src/Routing/Router.php b/src/Routing/Router.php new file mode 100644 index 0000000..fff6650 --- /dev/null +++ b/src/Routing/Router.php @@ -0,0 +1,117 @@ + + */ +class Router extends IlluminateRouter +{ + /* ------------------------------------------------------------------------------------------------ + | Route Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Create a route group with shared attributes. + * + * @param array $attributes + * @param \Closure $callback + */ + public function localizedGroup(Closure $callback, $attributes = []) + { + $attributes = array_merge($attributes, [ + 'prefix' => localization()->setLocale(), + 'middleware' => $this->getMiddleware(), + ]); + + $this->group($attributes, $callback); + } + + /** + * @param string $trans + * @param \Closure|array|string $action + * + * @return \Illuminate\Routing\Route + */ + public function transGet($trans, $action) + { + $uri = localization()->transRoute($trans); + + return $this->get($uri, $action); + } + + /** + * @param string $trans + * @param \Closure|array|string $action + * + * @return \Illuminate\Routing\Route + */ + public function transPost($trans, $action) + { + $uri = localization()->transRoute($trans); + + return $this->post($uri, $action); + } + + /** + * @param string $trans + * @param \Closure|array|string $action + * + * @return \Illuminate\Routing\Route + */ + public function transPut($trans, $action) + { + $uri = localization()->transRoute($trans); + + return $this->put($uri, $action); + } + + /** + * Register a new PATCH route with the router. + * + * @param string $trans + * @param \Closure|array|string $action + * + * @return \Illuminate\Routing\Route + */ + public function transPatch($trans, $action) + { + $uri = localization()->transRoute($trans); + + return $this->patch($uri, $action); + } + + /** + * Register a new DELETE route with the router. + * + * @param string $trans + * @param \Closure|array|string $action + * + * @return \Illuminate\Routing\Route + */ + public function transDelete($trans, $action) + { + $uri = localization()->transRoute($trans); + + return $this->delete($uri, $action); + } + + /** + * Register a new OPTIONS route with the router. + * + * @param string $trans + * @param \Closure|array|string $action + * + * @return \Illuminate\Routing\Route + */ + public function transOptions($trans, $action) + { + $uri = localization()->transRoute($trans); + + return $this->options($uri, $action); + } +} diff --git a/src/Traits/LocalizationKernelTrait.php b/src/Traits/LocalizationKernelTrait.php new file mode 100644 index 0000000..1096caa --- /dev/null +++ b/src/Traits/LocalizationKernelTrait.php @@ -0,0 +1,25 @@ + + * + * @property \Illuminate\Foundation\Application app + * @property \Arcanedev\Localization\Routing\Router router + */ +trait LocalizationKernelTrait +{ + /** + * Get the route dispatcher callback. + * + * @return \Closure + */ + protected function dispatchToRouter() + { + $this->router = $this->app['router']; + + return parent::dispatchToRouter(); + } +} diff --git a/src/Utilities/RouteTranslator.php b/src/Utilities/RouteTranslator.php index da555b1..7aef324 100644 --- a/src/Utilities/RouteTranslator.php +++ b/src/Utilities/RouteTranslator.php @@ -68,7 +68,7 @@ public function getCurrentRoute() /** * Set the current route. * - * @param string $currentRoute + * @param false|string $currentRoute * * @return self */ diff --git a/src/Utilities/Url.php b/src/Utilities/Url.php index 2197e60..8f99c93 100644 --- a/src/Utilities/Url.php +++ b/src/Utilities/Url.php @@ -1,4 +1,5 @@ + */ +class Kernel extends HttpKernel +{ + /* ------------------------------------------------------------------------------------------------ + | Traits + | ------------------------------------------------------------------------------------------------ + */ + use LocalizationKernelTrait; +} diff --git a/tests/TestCase.php b/tests/TestCase.php index df35193..5242af9 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -125,6 +125,20 @@ protected function refreshApplication($locale = false) $this->setRoutes($locale); } + /** + * Resolve application HTTP Kernel implementation. + * + * @param \Illuminate\Foundation\Application $app + * @return void + */ + protected function resolveApplicationHttpKernel($app) + { + $app->singleton( + 'Illuminate\Contracts\Http\Kernel', + Stubs\Http\Kernel::class + ); + } + /* ------------------------------------------------------------------------------------------------ | Other Functions | ------------------------------------------------------------------------------------------------ @@ -151,26 +165,26 @@ function () { app('router')->get('test', [ 'as' => 'test', function () { - return app('translator')->get('localization::routes.test_text'); + return app('translator')->get('localization::routes.test-text'); } ]); - app('router')->get(localization()->transRoute('localization::routes.about'), [ + app('router')->transGet('localization::routes.about', [ 'as' => 'about', function () { return localization()->getLocalizedURL('es') ? : "Not url available"; } ]); - app('router')->get(localization()->transRoute('localization::routes.view'), [ + app('router')->transGet('localization::routes.view', [ 'as' => 'view', function () { return localization()->getLocalizedURL('es') ? : "Not url available"; } ]); - app('router')->get(localization()->transRoute('localization::routes.view_project'), [ - 'as' => 'view_project', + app('router')->transGet('localization::routes.view-project', [ + 'as' => 'view-project', function () { return localization()->getLocalizedURL('es') ? : "Not url available"; } diff --git a/tests/fixtures/lang/en/routes.php b/tests/fixtures/lang/en/routes.php index a4836fc..8533b5a 100644 --- a/tests/fixtures/lang/en/routes.php +++ b/tests/fixtures/lang/en/routes.php @@ -3,7 +3,7 @@ return [ 'about' => 'about', 'view' => 'view/{id}', - 'view_project' => 'view/{id}/project/{project_id?}', + 'view-project' => 'view/{id}/project/{project_id?}', 'hello' => 'Hello world', - 'test_text' => 'Test text' + 'test-text' => 'Test text' ]; diff --git a/tests/fixtures/lang/es/routes.php b/tests/fixtures/lang/es/routes.php index 5859c0b..226d005 100644 --- a/tests/fixtures/lang/es/routes.php +++ b/tests/fixtures/lang/es/routes.php @@ -3,7 +3,7 @@ return [ 'about' => 'acerca', 'view' => 'ver/{id}', - 'view_project' => 'ver/{id}/proyecto/{project_id?}', + 'view-project' => 'ver/{id}/proyecto/{project_id?}', 'hello' => 'Hola mundo', - 'test_text' => 'Texto de prueba' + 'test-text' => 'Texto de prueba' ]; diff --git a/tests/fixtures/lang/fr/routes.php b/tests/fixtures/lang/fr/routes.php index ecfd953..75f9ac1 100644 --- a/tests/fixtures/lang/fr/routes.php +++ b/tests/fixtures/lang/fr/routes.php @@ -3,7 +3,7 @@ return [ 'about' => 'a-propos', 'view' => 'voir/{id}', - 'view_project' => 'voir/{id}/project/{project_id?}', + 'view-project' => 'voir/{id}/project/{project_id?}', 'hello' => 'Salut le monde', - 'test_text' => 'Texte de test' + 'test-text' => 'Texte de test' ];