Skip to content

Commit

Permalink
Merge pull request #2 from ARCANEDEV/develop
Browse files Browse the repository at this point in the history
Adding a custom router for localized routes
  • Loading branch information
arcanedev-maroc committed Sep 17, 2015
2 parents 39c38b9 + b341d64 commit a9b5a1d
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/Contracts/LocalizationInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace Arcanedev\Localization\Contracts;

use Arcanedev\Localization\Entities\LocaleCollection;
use Arcanedev\Localization\Exceptions\UndefinedSupportedLocalesException;
use Arcanedev\Localization\Exceptions\UnsupportedLocaleException;
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/RouteTranslatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getCurrentRoute();
/**
* Set the current route.
*
* @param string $currentRoute
* @param false|string $currentRoute
*
* @return self
*/
Expand Down
2 changes: 1 addition & 1 deletion src/LocalizationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function register()

$this->app->register(Providers\UtilitiesServiceProvider::class);
$this->registerLocalization();
$this->app->register(Providers\RouterServiceProvider::class);
$this->app->register(Providers\RoutingServiceProvider::class);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Middleware/LocaleCookieRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,4 @@ public function handle(Request $request, Closure $next)

return $next($request);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
class RouterServiceProvider extends ServiceProvider
class RoutingServiceProvider extends ServiceProvider
{
/* ------------------------------------------------------------------------------------------------
| Properties
Expand All @@ -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']);
}

/**
Expand All @@ -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);
}
}
117 changes: 117 additions & 0 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php namespace Arcanedev\Localization\Routing;

use Closure;
use Illuminate\Routing\Router as IlluminateRouter;

/**
* Class Router
*
* @package Arcanedev\Localization\Routing
* @author ARCANEDEV <[email protected]>
*/
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);
}
}
25 changes: 25 additions & 0 deletions src/Traits/LocalizationKernelTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php namespace Arcanedev\Localization\Traits;

/**
* Class LocalizationKernelTrait
*
* @package Arcanedev\Localization\Traits
* @author ARCANEDEV <[email protected]>
*
* @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();
}
}
2 changes: 1 addition & 1 deletion src/Utilities/RouteTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getCurrentRoute()
/**
* Set the current route.
*
* @param string $currentRoute
* @param false|string $currentRoute
*
* @return self
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Utilities/Url.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace Arcanedev\Localization\Utilities;

use Arcanedev\Localization\Contracts\UrlInterface;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
Expand Down Expand Up @@ -145,6 +146,8 @@ public static function unparse($parsed)
/**
* Extract Attributes From Router.
*
* @param Router $router
*
* @return array
*/
private static function extractAttributesFromCurrentRoute(Router $router)
Expand Down
19 changes: 19 additions & 0 deletions tests/Stubs/Http/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php namespace Arcanedev\Localization\Tests\Stubs\Http;

use Arcanedev\Localization\Traits\LocalizationKernelTrait;
use Orchestra\Testbench\Http\Kernel as HttpKernel;

/**
* Class Kernel
*
* @package Arcanedev\Localization\Tests\Stubs\Http
* @author ARCANEDEV <[email protected]>
*/
class Kernel extends HttpKernel
{
/* ------------------------------------------------------------------------------------------------
| Traits
| ------------------------------------------------------------------------------------------------
*/
use LocalizationKernelTrait;
}
24 changes: 19 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
| ------------------------------------------------------------------------------------------------
Expand All @@ -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";
}
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/lang/en/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
4 changes: 2 additions & 2 deletions tests/fixtures/lang/es/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
Loading

0 comments on commit a9b5a1d

Please sign in to comment.