Skip to content

Commit

Permalink
Merge pull request #180 from liip/symfony-4
Browse files Browse the repository at this point in the history
improved Symfony 4 support and various cleanups
  • Loading branch information
lsmith77 authored Dec 17, 2017
2 parents 4dc77f8 + 58af435 commit 10ac3ae
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ matrix:
- php: 7.1
env: DEPENDENCIES=beta
allow_failures:
- php: 5.6
- php: 7.2
env: SYMFONY_VERSION=@dev;ASSETIC=skip

before_install:
Expand Down
30 changes: 16 additions & 14 deletions Controller/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ThemeController
/**
* Options of the cookie to store active theme.
*
* @var array
* @var array|null
*/
protected $cookieOptions;

Expand All @@ -45,9 +45,9 @@ class ThemeController
*
* @param ActiveTheme $activeTheme active theme instance
* @param array $themes Available themes
* @param array $cookieOptions The options of the cookie we look for the theme to set
* @param array|null $cookieOptions The options of the cookie we look for the theme to set
*/
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions)
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null)
{
$this->activeTheme = $activeTheme;
$this->themes = $themes;
Expand All @@ -74,19 +74,21 @@ public function switchAction(Request $request)
$this->activeTheme->setName($theme);

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

$cookie = new Cookie(
$this->cookieOptions['name'],
$theme,
time() + $this->cookieOptions['lifetime'],
$this->cookieOptions['path'],
$this->cookieOptions['domain'],
(bool) $this->cookieOptions['secure'],
(bool) $this->cookieOptions['http_only']
);
if (!empty($this->cookieOptions)) {
$cookie = new Cookie(
$this->cookieOptions['name'],
$theme,
time() + $this->cookieOptions['lifetime'],
$this->cookieOptions['path'],
$this->cookieOptions['domain'],
(bool) $this->cookieOptions['secure'],
(bool) $this->cookieOptions['http_only']
);

$response = new RedirectResponse($url);
$response->headers->setCookie($cookie);
$response->headers->setCookie($cookie);
}

return $response;
}
Expand Down
8 changes: 0 additions & 8 deletions DependencyInjection/Compiler/TemplateResourcesPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ public function process(ContainerBuilder $container)

protected function setBundleDirectoryResources(ContainerBuilder $container, $engine, $bundleDirName, $bundleName)
{
if (!$container->hasDefinition('assetic.'.$engine.'_directory_resource.'.$bundleName)) {
throw new LogicException('The LiipThemeBundle must be registered after the AsseticBundle in the application Kernel.');
}

$resources = $container->getDefinition('assetic.'.$engine.'_directory_resource.'.$bundleName)->getArgument(0);
$themes = $container->getParameter('liip_theme.themes');
foreach ($themes as $theme) {
Expand All @@ -70,10 +66,6 @@ protected function setBundleDirectoryResources(ContainerBuilder $container, $eng

protected function setAppDirectoryResources(ContainerBuilder $container, $engine)
{
if (!$container->hasDefinition('assetic.'.$engine.'_directory_resource.kernel')) {
throw new LogicException('The LiipThemeBundle must be registered after the AsseticBundle in the application Kernel.');
}

$themes = $container->getParameter('liip_theme.themes');
foreach ($themes as $key => $theme) {
$themes[$key] = $container->getParameter('kernel.root_dir').'/Resources/themes/'.$theme;
Expand Down
File renamed without changes.
8 changes: 5 additions & 3 deletions LiipThemeBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Liip\ThemeBundle;

use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Liip\ThemeBundle\DependencyInjection\Compiler\ThemeCompilerPass;
Expand All @@ -22,8 +23,9 @@ class LiipThemeBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ThemeCompilerPass());
$container->addCompilerPass(new TemplateResourcesPass());
$container->addCompilerPass(new AsseticTwigFormulaPass());

$container->addCompilerPass(new ThemeCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
$container->addCompilerPass(new TemplateResourcesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
$container->addCompilerPass(new AsseticTwigFormulaPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
}
}
6 changes: 1 addition & 5 deletions Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="liip_theme.theme_controller.class">Liip\ThemeBundle\Controller\ThemeController</parameter>
</parameters>

<services>
<service id="liip_theme.theme_controller" class="%liip_theme.theme_controller.class%">
<service id="liip_theme.theme_controller" class="Liip\ThemeBundle\Controller\ThemeController" public="true">
<argument type="service" id="liip_theme.active_theme" />
<argument>%liip_theme.themes%</argument>
<argument>%liip_theme.cookie%</argument>
Expand Down
18 changes: 6 additions & 12 deletions Resources/config/templating.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,41 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="liip_theme.templating_locator.class">Liip\ThemeBundle\Locator\TemplateLocator</parameter>
<parameter key="liip_theme.file_locator.class">Liip\ThemeBundle\Locator\FileLocator</parameter>
<parameter key="liip_theme.filesystem_loader.class">Liip\ThemeBundle\Twig\Loader\FilesystemLoader</parameter>
<parameter key="liip_theme.active_theme.class">Liip\ThemeBundle\ActiveTheme</parameter>
<parameter key="liip_theme.cache_warmer.class">Liip\ThemeBundle\CacheWarmer\TemplatePathsCacheWarmer</parameter>
<parameter key="liip_theme.theme_auto_detect.class">Liip\ThemeBundle\Helper\DeviceDetection</parameter>
<parameter key="templating.finder.class">Liip\ThemeBundle\CacheWarmer\TemplateFinder</parameter>
</parameters>

<services>

<service id="liip_theme.templating.cache_warmer.template_paths" class="%liip_theme.cache_warmer.class%" public="false">
<service id="liip_theme.templating.cache_warmer.template_paths" class="Liip\ThemeBundle\CacheWarmer\TemplatePathsCacheWarmer" public="false">
<argument type="service" id="templating.finder" />
<argument type="service" id="liip_theme.templating_locator" />
<argument type="service" id="liip_theme.active_theme" />
</service>

<service id="templating.finder" class="%templating.finder.class%" public="false">
<service id="templating.finder" class="Liip\ThemeBundle\CacheWarmer\TemplateFinder" public="false">
<argument type="service" id="kernel" />
<argument type="service" id="templating.filename_parser" />
<argument>%kernel.root_dir%/Resources</argument>
<argument type="service" id="liip_theme.active_theme"/>
</service>

<service id="liip_theme.templating_locator" class="%liip_theme.templating_locator.class%" public="false">
<service id="liip_theme.templating_locator" class="Liip\ThemeBundle\Locator\TemplateLocator" public="false">
<argument type="service" id="liip_theme.file_locator" />
<argument>%kernel.cache_dir%</argument>
<argument type="service" id="liip_theme.active_theme" />
</service>

<service id="liip_theme.file_locator" class="%liip_theme.file_locator.class%" public="false">
<service id="liip_theme.file_locator" class="Liip\ThemeBundle\Locator\FileLocator" public="false">
<argument type="service" id="kernel" />
<argument type="service" id="liip_theme.active_theme" />
<argument>%kernel.root_dir%/Resources</argument>
<argument type="collection" />
<argument>%liip_theme.path_patterns%</argument>
</service>

<service id="liip_theme.theme_auto_detect" class="%liip_theme.theme_auto_detect.class%" />
<service id="liip_theme.theme_auto_detect" class="Liip\ThemeBundle\Helper\DeviceDetection" />

<service id="liip_theme.active_theme" class="%liip_theme.active_theme.class%">
<service id="liip_theme.active_theme" class="Liip\ThemeBundle\ActiveTheme">
<argument>%liip_theme.active_theme%</argument>
<argument>%liip_theme.themes%</argument>
<argument type="service" id="liip_theme.theme_auto_detect" />
Expand Down
6 changes: 1 addition & 5 deletions Resources/config/theme_request_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="liip_theme.theme_request_listener.class">Liip\ThemeBundle\EventListener\ThemeRequestListener</parameter>
</parameters>

<services>
<service id="liip_theme.theme_request_listener" class="%liip_theme.theme_request_listener.class%">
<service id="liip_theme.theme_request_listener" class="Liip\ThemeBundle\EventListener\ThemeRequestListener">
<argument type="service" id="liip_theme.active_theme" />
<argument>%liip_theme.cookie%</argument>
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
Expand Down
4 changes: 2 additions & 2 deletions Tests/DependencyInjection/Compiler/ThemeCompilerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function testProcess()
->method('getParameter')
->will($this->returnValueMap(
array(
array('liip_theme.cache_warming', true),
array('liip_theme.filesystem_loader.class', 'Liip\ThemeBundle\Twig\Loader\FilesystemLoader'),
array('liip_theme.cache_warming', true),
array('liip_theme.filesystem_loader.class', 'Liip\ThemeBundle\Twig\Loader\FilesystemLoader'),
)
)
)
Expand Down

0 comments on commit 10ac3ae

Please sign in to comment.