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

Symfony 5 compatibility and Switch to SyliusThemeBundle #19

Merged
merged 15 commits into from
Nov 19, 2020
Merged
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/Tests/Application/var/*
/vendor/
.env.local
.env.*.local
/composer.phar
/composer.lock
.php_cs.cache
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cache:

matrix:
include:
- php: 7.2
- php: 7.3
env:
- COMPOSER_FLAGS="--prefer-lowest --prefer-stable --prefer-dist --no-interaction"
- php: 7.4
Expand Down
13 changes: 6 additions & 7 deletions DependencyInjection/CompilerPass/ImageFormatCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Sulu\Bundle\ThemeBundle\DependencyInjection\CompilerPass;

use Liip\ThemeBundle\ActiveTheme;
use Sulu\Bundle\MediaBundle\DependencyInjection\AbstractImageFormatCompilerPass;
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
Expand All @@ -25,14 +25,13 @@ class ImageFormatCompilerPass extends AbstractImageFormatCompilerPass
*/
protected function getFiles(ContainerBuilder $container)
{
$files = [];

/** @var ActiveTheme $activeTheme */
$activeTheme = $container->get('liip_theme.active_theme');
/** @var ThemeRepositoryInterface $themeRepository */
$themeRepository = $container->get('sylius.repository.theme');
$bundles = $container->getParameter('kernel.bundles');
$configPath = 'config/image-formats.xml';

foreach ($activeTheme->getThemes() as $theme) {
$files = [];
foreach ($themeRepository->findAll() as $theme) {
foreach ($bundles as $bundleName => $bundle) {
$bundleReflection = new \ReflectionClass($bundle);
$fileName = $bundleReflection->getFileName();
Expand All @@ -44,7 +43,7 @@ protected function getFiles(ContainerBuilder $container)
$path = sprintf(
'%s/Resources/themes/%s/%s',
dirname($fileName),
$theme,
$theme->getName(),
$configPath
);

Expand Down

This file was deleted.

31 changes: 22 additions & 9 deletions EventListener/SetThemeEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,36 @@

namespace Sulu\Bundle\ThemeBundle\EventListener;

use Liip\ThemeBundle\ActiveTheme;
use Sulu\Bundle\PreviewBundle\Preview\Events\PreRenderEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Sylius\Bundle\ThemeBundle\Context\SettableThemeContext;
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
* Listener which applies the configured theme.
*/
class SetThemeEventListener
{
/**
* @var ActiveTheme
* @var ThemeRepositoryInterface
*/
private $activeTheme;
private $themeRepository;

public function __construct(ActiveTheme $activeTheme)
/**
* @var SettableThemeContext
*/
private $themeContext;

public function __construct(ThemeRepositoryInterface $themeRepository, SettableThemeContext $themeContext)
{
$this->activeTheme = $activeTheme;
$this->themeRepository = $themeRepository;
$this->themeContext = $themeContext;
}

/**
* Set the active theme if there is a portal.
*/
public function setActiveThemeOnRequest(GetResponseEvent $event): void
public function setActiveThemeOnRequest(RequestEvent $event): void
{
if (null === ($attributes = $event->getRequest()->get('_sulu'))
|| null === ($webspace = $attributes->getAttribute('webspace'))
Expand All @@ -42,14 +49,20 @@ public function setActiveThemeOnRequest(GetResponseEvent $event): void
return;
}

$this->activeTheme->setName($theme);
$theme = $this->themeRepository->findOneByName($theme);
if (null !== $theme) {
$this->themeContext->setTheme($theme);
}
}

/**
* Set the active theme for a preview rendering.
*/
public function setActiveThemeOnPreviewPreRender(PreRenderEvent $event): void
{
$this->activeTheme->setName($event->getAttribute('webspace')->getTheme());
$theme = $this->themeRepository->findOneByName($event->getAttribute('webspace')->getTheme());
if (null !== $theme) {
$this->themeContext->setTheme($theme);
}
}
}
156 changes: 153 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,155 @@
# SuluThemeBundle
<h1 align="center">SuluThemeBundle</h1>

[![Build Status](https://travis-ci.org/sulu/SuluThemeBundle.svg?branch=develop)](https://travis-ci.org/sulu/SuluThemeBundle)
<p align="center">
<a href="https://sulu.io/" target="_blank">
<img width="30%" src="https://sulu.io/uploads/media/800x/00/230-Official%20Bundle%20Seal.svg?v=2-6&inline=1" alt="Official Sulu Bundle Badge">
</a>
</p>

This package enables a basic theming for sulu. It uses liip/theme-bundle to extend the discovery feature of twig.
<p align="center">
<a href="https://github.com/sulu/SuluThemeBundle/releases" target="_blank">
<img src="https://img.shields.io/github/tag/sulu/SuluThemeBundle.svg" alt="GitHub tag (latest SemVer)">
</a>
<a href="https://github.com/sulu/SuluThemeBundle/blob/master/LICENSE" target="_blank">
<img src="https://img.shields.io/github/license/sulu/SuluThemeBundle.svg" alt="GitHub license">
</a>
<a href="https://github.com/sulu/sulu/releases" target="_blank">
<img src="https://img.shields.io/badge/sulu%20compatibility-%3E=2.0-52b6ca.svg" alt="Sulu compatibility">
</a>
</p>
<br/>

The **SuluThemeBundle** provides the functionality to add multiple themes for different look and feel using multiple
webspaces in the [Sulu](https://sulu.io/) content management system.

To achieve this, the bundle uses the [SyliusThemeBundle](https://github.com/Sylius/SyliusThemeBundle) to render different
twig templates and asset files. Each webspace can have it's own theme.

## 🚀&nbsp; Installation and Usage

### Install the bundle

Execute the following [composer](https://getcomposer.org/) command to add the bundle to the dependencies of your
project:

```bash
composer require sulu/theme-bundle
```

### Enable the bundle

Enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:

```php
return [
/* ... */
Sulu\Bundle\ThemeBundle\SuluThemeBundle::class => ['all' => true],
];
```


### Configure the SyliusThemeBundle

In order to use the bundle you have to add at least the following default configuration:

```yaml
# ./config/packages/sylius_theme.yaml

sylius_theme:
sources:
filesystem: ~
```

By default the bundle seeks for the themes in the `%kernel.project_dir%/themes` directory. This can be changed via the
yaml configuration:

```yaml
sylius_theme:
sources:
filesystem:
filename: theme.json #default is composer.json
directories:
- "%kernel.project_dir%/templates/themes"
```

For more detailed information about the configuration sources go to the [Sylius documentation](https://github.com/Sylius/SyliusThemeBundle/blob/master/docs/configuration_sources.md).
Prokyonn marked this conversation as resolved.
Show resolved Hide resolved

### Configure your themes

Every theme must have its own configuration file in form of a `composer.json`.
Copy link
Contributor

Choose a reason for hiding this comment

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

should we use composer.json or theme.json in the examples?

Go to the [Theme Configuration Reference](https://github.com/Sylius/SyliusThemeBundle/blob/master/docs/theme_configuration_reference.md)
for the detailed documentation about the configuration options.
The configuration file has to be placed in the specified directory of the `sylius_theme.yaml`.

The minimal configuration for a theme would be the following:

```json
// ./templates/themes/<theme-name>/theme.json

{
"name": "vendor/<theme-name>"
}
```

It is important, that the `name` matches the naming convention of composer.

### Create a theme
To create a theme you have to create a new directory in the templates/themes folder of your application with the name of
the new theme. In the newly created directory you have to add the theme configuration file `theme.json`.
See [Configure your themes](#configure-your-themes). Additonally you have to create the `templates` directory next to
the `theme.json`. Afterwards you have to fill this folder with all the used templates in the webspace.

This results in the following project structure:

```
ProjectName
├── composer.json
├── assets
├── bin
├── config
├── ...
├── templates
│ ├── themes
│ │ ├── <theme-name-1>
│ │ │ ├── templates
│ │ │ │ └── base.html.twig
│ │ │ └── theme.json
│ │ └── <theme-name-2>
│ │ ├── templates
│ │ │ └── base.html.twig
│ │ └── theme.json
| └── base.html.twig
├── ...
└── ...
```

### Add one of your themes to a webspace
alexander-schranz marked this conversation as resolved.
Show resolved Hide resolved
Each webspace can use a different theme. A theme can be enabled for a specific webspace by adding the theme-name
`<theme>vendor/theme-name</theme>` to your webspace:

```xml
<!-- ./config/webspaces/example.xml -->
<webspace xmlns="http://schemas.sulu.io/webspace/webspace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/webspace/webspace http://schemas.sulu.io/webspace/webspace-1.1.xsd">
<name>example.com</name>
<key>example</key>
...
<theme>vendor/theme-name</theme>
...
</webspace>
```

## ❤️&nbsp; Support and Contributions

The Sulu content management system is a **community-driven open source project** backed by various partner companies.
We are committed to a fully transparent development process and **highly appreciate any contributions**.

In case you have questions, we are happy to welcome you in our official [Slack channel](https://sulu.io/services-and-support).
If you found a bug or miss a specific feature, feel free to **file a new issue** with a respective title and description
on the the [sulu/SuluThemeBundle](https://github.com/sulu/SuluThemeBundle) repository.


## 📘&nbsp; License

The Sulu content management system is released under the under terms of the [MIT License](LICENSE).
3 changes: 2 additions & 1 deletion Resources/config/admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<services>
<service id="sulu_website.event_listener.set_theme"
class="Sulu\Bundle\ThemeBundle\EventListener\SetThemeEventListener">
<argument type="service" id="liip_theme.active_theme"/>
<argument type="service" id="sylius.repository.theme"/>
<argument type="service" id="sylius.theme.context.settable"/>

<tag name="kernel.event_listener" event="%sulu_preview.events.pre-render%"
method="setActiveThemeOnPreviewPreRender"/>
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/website.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<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"/>
<argument type="service" id="sylius.repository.theme"/>
<argument type="service" id="sylius.theme.context.settable"/>

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