From cd4a4b8af6c55fe6774dc8e54d77b0130ba07d1b Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 19 Nov 2020 15:00:04 +0100 Subject: [PATCH] Add abandoned note and upgrade to sylius theme bundle --- README.md | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 1 + 2 files changed, 146 insertions(+) diff --git a/README.md b/README.md index 9912846..a8fa105 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ Theme Bundle ============ +> This project is not longer maintained and recommends to use [SyliusThemeBundle](https://github.com/Sylius/SyliusThemeBundle) instead for support of Symfony 5 and Twig 3. +> For migration have a look at [Migrate to SyliusThemeBundle](#migrate-to-syliusthemebundle). + This bundle provides you the possibility to add themes to each bundle. In your bundle directory it will look under `Resources/themes/` or fall back to the normal Resources/views if no matching file was found. @@ -304,6 +307,148 @@ liip_theme: device_detection: my_devcice_detection ```` +## Migrate to SyliusThemeBundle + +This will show you the stepts to switch from the [LiipThemeBundle](https://github.com/liip/LiipThemeBundle) to [SyliusThemeBundle](https://github.com/Sylius/SyliusThemeBundle). + +#### Remove the old theme bundle and install the SyliusThemeBundle: + +```bash +# Remove old theme-bundle +composer remove liip/theme-bundle --no-update + +# Install new theme-bundle +composer require sylius/theme-bundle:"^2.0" +``` + +#### Remove old configuration + +The old `liip_theme.yaml` configuration needs to be removed: + +```diff +-liip_theme: +- themes: ['awesome'] +- active_theme: 'awesome' +``` + +In the next step you see how you configure the **awesome** theme using the SyliusThemeBundle. + +#### Configure the SyliusThemeBundle: + +In order to use the bundle you have to add 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 and looks for a configuration +file named `composer.json`. This can be changed via the yaml configuration: + +```yaml +sylius_theme: + sources: + filesystem: + filename: theme.json +``` + +#### Convert Theme Configuration + +In the SyliusThemeBundle every theme must have its own configuration file in form of a `theme.json`. +Add a `theme.json` file and add the following minimal configuration: + +```diff +{ + "name": "app/awesome" +} +``` + +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. + +Most likely you have to change the theme name. It is important, that the `name` matches the naming convention of composer (`vendor/name`). +Furthermore the `theme.json` has to be moved into the directory for this specific theme. + +For example: `%kernel.project_dir%/themes/awesome/theme.json` + +#### Update project structure + +Your templates have to be placed in a `templates` directory next to the `theme.json` file. + +For example: `%kernel.project_dir%/themes//templates` + +This results in the following project structure: + +``` +ProjectName +├── composer.json +├── assets +├── bin +├── config +├── templates +├── themes +│ ├── awesome +│ │ ├── templates +│ │ │ └── base.html.twig +│ │ └── theme.json +│ └── +│ ├── templates +│ │ └── base.html.twig +│ └── theme.json +├── ... +└── ... +``` + +As you can see in the project structure, each theme must have their own `theme.json` configuration file next to the +templates directory. + +#### Create ThemeRequestListener + +You need to create a ThemeRequestListener to set the theme based on the current `$request` data: + +```php +use Sylius\Bundle\ThemeBundle\Context\SettableThemeContext; +use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\HttpKernelInterface; + +final class ThemeRequestListener +{ + /** @var ThemeRepositoryInterface */ + private $themeRepository; + + /** @var SettableThemeContext */ + private $themeContext; + + public function __construct(ThemeRepositoryInterface $themeRepository, SettableThemeContext $themeContext) + { + $this->themeRepository = $themeRepository; + $this->themeContext = $themeContext; + } + + public function onKernelRequest(GetResponseEvent $event): void + { + if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { + // don't do anything if it's not the master request + return; + } + + $themeName = 'app/awesome'; + + // here you can set the $themeName based on $event->getRequest() object + + $this->themeContext->setTheme( + $this->themeRepository->findOneByName($themeName) + ); + } +} +``` + +Have a look also at the [SyliusThemeBundle Documentation](https://github.com/Sylius/SyliusThemeBundle/tree/master/docs/index.md). + ## Contribution Active contribution and patches are very welcome. To keep things in shape we diff --git a/composer.json b/composer.json index fbe6fca..0d1b5d8 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "conflict": { "sebastian/comparator": "1.2.3" }, + "abandoned": "sylius/theme-bundle", "extra": { "branch-alias": { "dev-master": "1.4-dev"