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

Implemented logic for liip-theme bundle #1

Merged
merged 3 commits into from
Jun 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
preset: symfony

enabled:
- concat_with_spaces
- ordered_use
- short_array_syntax

disabled:
- concat_without_spaces
- phpdoc_align
- phpdoc_indent
- phpdoc_to_comment
- blankline_after_open_tag
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
sudo: false

language: php

cache:
directories:
- $HOME/.composer/cache
- downloads

matrix:
include:
- php: 5.5
- php: 7.0

before_script:
- phpenv config-rm xdebug.ini
- composer self-update
- composer install --prefer-dist --no-interaction

script:
- vendor/bin/phpunit

notifications:
slack:
secure: "Gd3/1e0pBKvJv1UhWpBkWijJpmSWlarg6uPBJO0h4z1IpkZjd++jOjhmOQ7n+yMfuapQuJTcVOK0yIWu7orJoGAKFkBlMEIrLk1xMAG9phjjMLUO0FWgcQ3eVW5mTyfMBtClz4OL5wXckw17ohtXHDK8qnI0Hz9Qj8Rqgf2OZhM="
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CHANGELOG for Sulu
==================

* dev-develop
* FEATURE #1 Introduced liip/theme-bundle to enable theming
104 changes: 104 additions & 0 deletions DependencyInjection/CompilerPass/ImageFormatCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ThemeBundle\DependencyInjection\CompilerPass;

use Sulu\Bundle\MediaBundle\Media\FormatLoader\XmlFormatLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class ImageFormatCompilerPass.
*/
class ImageFormatCompilerPass implements CompilerPassInterface
{
/**
* @var ContainerBuilder
*/
protected $container;

/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
$this->container = $container;

$formats = $this->loadThemeFormats(
$container->getParameter('sulu_media.format_manager.default_imagine_options')
);
if ($container->hasParameter('sulu_media.image.formats')) {
$formats = array_merge($container->getParameter('sulu_media.image.formats'), $formats);
}

$container->setParameter('sulu_media.image.formats', $formats);
}

/**
* @param array $defaultOptions
*
* @return array
*/
protected function loadThemeFormats($defaultOptions)
{
$activeFormats = [];
$activeTheme = $this->container->get('liip_theme.active_theme');
$bundles = $this->container->getParameter('kernel.bundles');
$configPaths = $this->container->getParameter('sulu_media.format_manager.config_paths');
$defaultConfigPath = 'config/image-formats.xml';

foreach ($activeTheme->getThemes() as $theme) {
foreach ($bundles as $bundleName => $bundle) {
$reflector = new \ReflectionClass($bundle);
$configPath = $defaultConfigPath;
if (isset($configPaths[$theme])) {
$configPath = $configPaths[$theme];
}
$fullPath = sprintf(
'%s/Resources/themes/%s/%s',
dirname($reflector->getFileName()),
$theme,
$configPath
);

if (file_exists($fullPath)) {
$this->setFormatsFromFile($fullPath, $activeFormats, $defaultOptions);
}
}
}

return $activeFormats;
}

/**
* @param $fullPath
* @param $activeFormats
* @param $defaultOptions
*/
protected function setFormatsFromFile($fullPath, &$activeFormats, $defaultOptions)
{
$folder = dirname($fullPath);
$fileName = basename($fullPath);

$locator = new FileLocator($folder);
$loader = new XmlFormatLoader($locator);
$loader->setDefaultOptions($defaultOptions);
$themeFormats = $loader->load($fileName);
foreach ($themeFormats as $format) {
if (isset($format['name']) && !array_key_exists($format['name'], $activeFormats)) {
$activeFormats[$format['name']] = $format;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ThemeBundle\DependencyInjection\CompilerPass;

use Sulu\Bundle\ThemeBundle\StructureProvider\WebspaceStructureProvider;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class WebspaceStructureProviderCompilerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('sulu.content.webspace_structure_provider')) {
return;
}

$definition = $container->getDefinition('sulu.content.webspace_structure_provider');
$definition->setClass(WebspaceStructureProvider::class);
$definition->addArgument(new Reference('sulu_core.webspace.webspace_manager'));
$definition->addArgument(new Reference('liip_theme.active_theme'));
}
}
34 changes: 34 additions & 0 deletions DependencyInjection/SuluThemeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ThemeBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* This is the class that loads and manages your bundle configuration.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class SuluThemeExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load(sprintf('%s.xml', $container->getParameter('sulu.context')));
}
}
63 changes: 63 additions & 0 deletions EventListener/SetThemeEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ThemeBundle\EventListener;

use Liip\ThemeBundle\ActiveTheme;
use Sulu\Bundle\PreviewBundle\Preview\Events\PreRenderEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
* Listener which applies the configured theme.
*/
class SetThemeEventListener
Copy link
Member

Choose a reason for hiding this comment

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

use kernel event listener (request analyzer isn't lazy any more)

{
/**
* @var ActiveTheme
*/
private $activeTheme;

/**
* @param ActiveTheme $activeTheme
*/
public function __construct(ActiveTheme $activeTheme)
{
$this->activeTheme = $activeTheme;
}

/**
* Set the active theme if there is a portal.
*
* @param GetResponseEvent $event
*/
public function setActiveThemeOnRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()
|| null === ($attributes = $event->getRequest()->get('_sulu'))
|| null === ($webspace = $attributes->getAttribute('webspace'))
|| null === ($theme = $webspace->getTheme())
) {
return;
}

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

/**
* Set the active theme for a preview rendering.
*
* @param PreRenderEvent $event
*/
public function setActiveThemeOnPreviewPreRender(PreRenderEvent $event)
{
$this->activeTheme->setName($event->getAttribute('webspace')->getTheme());
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SuluThemeBundle

[![Build Status](https://travis-ci.org/sulu/SuluThemeBundle.svg?branch=develop)](https://travis-ci.org/sulu/SuluThemeBundle)

This package enables a basic theming for sulu. It uses liip/theme-bundle to extend the discovery feature of twig.
14 changes: 14 additions & 0 deletions Resources/config/admin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
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">
<services>
<service id="sulu_website.event_listener.set_theme"
class="Sulu\Bundle\ThemeBundle\EventListener\SetThemeEventListener">
<argument type="service" id="liip_theme.active_theme"/>

<tag name="kernel.event_listener" event="%sulu_preview.events.pre-render%"
method="setActiveThemeOnPreviewPreRender"/>
</service>
</services>
</container>
14 changes: 14 additions & 0 deletions Resources/config/website.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
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">

<services>
<service id="sulu_theme.event_listener.set_theme"
class="Sulu\Bundle\ThemeBundle\EventListener\SetThemeEventListener">
<argument type="service" id="liip_theme.active_theme"/>

<tag name="kernel.event_listener" event="kernel.request" method="setActiveThemeOnRequest"/>
</service>
</services>
</container>
Loading