Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make redirect url fallback configurable #190

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
19 changes: 17 additions & 2 deletions Controller/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;

/**
* Theme controller.
Expand All @@ -40,18 +41,32 @@ class ThemeController
*/
protected $cookieOptions;

/**
* @var RouterInterface
*/
protected $router;

/**
* @var string
*/
protected $defaultRoute;

/**
* Theme controller construct.
*
* @param ActiveTheme $activeTheme active theme instance
* @param array $themes Available themes
* @param array|null $cookieOptions The options of the cookie we look for the theme to set
* @param RouterInterface $router
* @param string $defaultRoute
*/
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null)
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null, RouterInterface $router, $defaultRoute = '/')
Copy link
Contributor

Choose a reason for hiding this comment

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

default route probably should be the name of an actual route now, no?

so I think what we should probably do us to allow null for $defaultRoute and then when we do the redirect, check if its null and in that case fallback to the /

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, that's a good idea. I will change that. :)

{
$this->activeTheme = $activeTheme;
$this->themes = $themes;
$this->cookieOptions = $cookieOptions;
$this->router = $router;
$this->defaultRoute = $defaultRoute;
}

/**
Expand All @@ -73,7 +88,7 @@ public function switchAction(Request $request)

$this->activeTheme->setName($theme);

$url = $request->headers->get('Referer', '/');
$url = $request->headers->get('Referer', $this->router->generate($this->defaultRoute));
$response = new RedirectResponse($url);

if (!empty($this->cookieOptions)) {
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function getConfigTreeBuilder()
->prototype('scalar')->end()
->end()
->scalarNode('active_theme')->defaultNull()->end()
->scalarNode('redirect_fallback')->defaultValue('homepage')->end()
Copy link
Contributor

Choose a reason for hiding this comment

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

homepage may not exist for other people who are using this bundle, it's better to put /

Copy link
Author

Choose a reason for hiding this comment

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

Okay, fair point, I will change that to use null as default as prepared in ThemeController.php already.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

->arrayNode('path_patterns')
->addDefaultsIfNotSet()
->children()
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/LiipThemeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);

foreach (array('themes', 'active_theme', 'path_patterns', 'cache_warming') as $key) {
foreach (array('themes', 'active_theme', 'path_patterns', 'cache_warming', 'redirect_fallback') as $key) {
$container->setParameter($this->getAlias().'.'.$key, $config[$key]);
}

Expand Down
2 changes: 2 additions & 0 deletions Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<argument type="service" id="liip_theme.active_theme" />
<argument>%liip_theme.themes%</argument>
<argument>%liip_theme.cookie%</argument>
<argument type="service" id="router"/>
<argument>%liip_theme.redirect_fallback%</argument>
</service>
</services>
</container>
13 changes: 12 additions & 1 deletion Tests/UseCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Liip\ThemeBundle\EventListener\ThemeRequestListener;
use Liip\ThemeBundle\Controller\ThemeController;
use Liip\ThemeBundle\ActiveTheme;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Router;

/**
* Bundle Functional tests.
Expand Down Expand Up @@ -133,9 +135,13 @@ public function testThemeAction($config, $assertion, $hasAlreadyACookie = true)
$request = $this->getMockRequest('cookie');
}

$router = $this->getMockBuilder(Router::class)
->disableOriginalConstructor()
->getMock();

$controller = false;
if ($config['load_controllers']) {
$controller = new ThemeController($activeTheme, $config['themes'], $config['cookie']);
$controller = new ThemeController($activeTheme, $config['themes'], $config['cookie'], $router, $config['redirect_fallback']);
}

$listener = new ThemeRequestListener($activeTheme, $config['cookie'], $device);
Expand Down Expand Up @@ -178,6 +184,7 @@ public function dataProvider()
// all-in Controller wins over Cookie and Autodetection
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => true,
Expand All @@ -194,6 +201,7 @@ public function dataProvider()
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => true,
Expand All @@ -210,6 +218,7 @@ public function dataProvider()
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => true,
'load_controllers' => false,
Expand All @@ -226,6 +235,7 @@ public function dataProvider()
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => false,
'load_controllers' => true,
Expand All @@ -242,6 +252,7 @@ public function dataProvider()
array(
array(
'themes' => array('default', 'controller', 'cookie', 'autodetect'),
'redirect_fallback' => 'homepage',
'active_theme' => 'default',
'autodetect_theme' => false,
'load_controllers' => true,
Expand Down