Skip to content

Commit

Permalink
Integrate SiteAccess design selection into core semantic config
Browse files Browse the repository at this point in the history
  • Loading branch information
lolautruche committed May 12, 2017
1 parent 36e2b8e commit 731212f
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 86 deletions.
47 changes: 14 additions & 33 deletions bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,22 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->arrayNode('design')
->addDefaultsIfNotSet()
->children()
->arrayNode('list')
->useAttributeAsKey('design_name')
->example(['my_design' => ['theme1', 'theme2']])
->prototype('array')
->info('A design is a labeled collection of themes. Theme order defines the fallback order.')
->prototype('scalar')->end()
->end()
->end()
->arrayNode('override_paths')
->info('Directories to add to the override list for templates. Those directories will be checked before theme directories.')
->prototype('scalar')->end()
->end()
->booleanNode('disable_assets_pre_resolution')
->info('If set to true, assets path won\'t be pre-resolved at compile time.')
->defaultValue('%kernel.debug%')
->end()
->arrayNode('design_list')
->useAttributeAsKey('design_name')
->example(['my_design' => ['theme1', 'theme2']])
->prototype('array')
->info('A design is a labeled collection of themes. Theme order defines the fallback order.')
->prototype('scalar')->end()
->end()
->end()
->arrayNode('template_override_paths')
->info('Directories to add to the override list for templates. Those directories will be checked before theme directories.')
->prototype('scalar')->end()
->end()
->booleanNode('disable_assets_pre_resolution')
->info('If set to true, assets path won\'t be pre-resolved at compile time.')
->defaultValue('%kernel.debug%')
->end()
->arrayNode('phpstorm')
->addDefaultsIfNotSet()
->children()
Expand All @@ -54,20 +49,6 @@ public function getConfigTreeBuilder()
->end()
->end();

$systemNode = $this->generateScopeBaseNode($rootNode);
$systemNode
->scalarNode('design')
->cannotBeEmpty()
->info('Name of the design to use. Must be one of the declared ones in the "design" key.')
->end()
->arrayNode('twig_globals')
->info('Variables available in all Twig templates for current SiteAccess.')
->normalizeKeys(false)
->useAttributeAsKey('variable_name')
->example(array('foo' => '"bar"', 'pi' => 3.14))
->prototype('variable')->end()
->end();

return $treeBuilder;
}
}
41 changes: 41 additions & 0 deletions bundle/DependencyInjection/DesignConfigParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the EzPlatformDesignEngine package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\EzPlatformDesignEngineBundle\DependencyInjection;

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;

class DesignConfigParser implements ParserInterface
{
public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerInterface $contextualizer)
{
if (isset($scopeSettings['design'])) {
$contextualizer->setContextualParameter('design', $currentScope, $scopeSettings['design']);
}
}

public function preMap(array $config, ContextualizerInterface $contextualizer)
{
}

public function postMap(array $config, ContextualizerInterface $contextualizer)
{
}

public function addSemanticConfig(NodeBuilder $nodeBuilder)
{
$nodeBuilder
->scalarNode('design')
->cannotBeEmpty()
->info('Name of the design to use. Must be declared in ezdesign.design_list')
->end();
}
}
28 changes: 5 additions & 23 deletions bundle/DependencyInjection/EzPlatformDesignEngineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,14 @@ public function load(array $configs, ContainerBuilder $container)

private function configureDesigns(array $config, ConfigurationProcessor $processor, ContainerBuilder $container)
{
// Always add _base theme to the list.
foreach ($config['design']['list'] as $design => &$themes) {
$themes[] = '_base';
}
$container->setParameter('ezdesign.design_list', $config['design']['list']);
$container->setParameter('ezdesign.templates_override_paths', $config['design']['override_paths']);
$container->setParameter('ezdesign.asset_resolution.disabled', $config['design']['disable_assets_pre_resolution']);
// Always add "standard" design to the list (defaults to application level & override paths only)
$config['design_list'] += ['standard' => []];
$container->setParameter('ezdesign.design_list', $config['design_list']);
$container->setParameter('ezdesign.templates_override_paths', $config['template_override_paths']);
$container->setParameter('ezdesign.asset_resolution.disabled', $config['disable_assets_pre_resolution']);

// PHPStorm settings
$container->setParameter('ezdesign.phpstorm.enabled', $config['phpstorm']['enabled']);
$container->setParameter('ezdesign.phpstorm.twig_config_path', $config['phpstorm']['twig_config_path']);

// SiteAccess aware settings
$processor->mapConfig(
$config,
function ($scopeSettings, $currentScope, ContextualizerInterface $contextualizer) use ($config) {
if (isset($scopeSettings['design'])) {
if (!isset($config['design']['list'][$scopeSettings['design']])) {
throw new InvalidArgumentException(
"Selected design for $currentScope '{$scopeSettings['design']}' is invalid. Did you forget to define it?"
);
}

$contextualizer->setContextualParameter('design', $currentScope, $scopeSettings['design']);
}
}
);
}
}
7 changes: 7 additions & 0 deletions bundle/EzPlatformDesignEngineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use EzSystems\EzPlatformDesignEngineBundle\DependencyInjection\Compiler\AssetThemePass;
use EzSystems\EzPlatformDesignEngineBundle\DependencyInjection\Compiler\PHPStormPass;
use EzSystems\EzPlatformDesignEngineBundle\DependencyInjection\Compiler\TwigThemePass;
use EzSystems\EzPlatformDesignEngineBundle\DependencyInjection\DesignConfigParser;
use EzSystems\EzPlatformDesignEngineBundle\DependencyInjection\EzPlatformDesignEngineExtension;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -23,6 +24,12 @@ class EzPlatformDesignEngineBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);

/** @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\EzPublishCoreExtension $eZExtension */
$eZExtension = $container->getExtension('ezpublish');
$eZExtension->addConfigParser(new DesignConfigParser());
$eZExtension->addDefaultSettings(__DIR__ . '/Resources/config', ['default_settings.yml']);

$container->addCompilerPass(new TwigThemePass());
$container->addCompilerPass(new AssetThemePass(), PassConfig::TYPE_OPTIMIZE);
$container->addCompilerPass(new AssetPathResolutionPass(), PassConfig::TYPE_OPTIMIZE);
Expand Down
3 changes: 1 addition & 2 deletions bundle/Resources/config/default_settings.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
parameters:
ezdesign.default.design: ~
ezdesign.default.twig_globals: {}
ezsettings.default.design: standard
5 changes: 2 additions & 3 deletions bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ parameters:
services:
ezdesign.template_name_resolver:
class: EzSystems\EzPlatformDesignEngine\Templating\ThemeTemplateNameResolver
arguments: ["$design;ezdesign$"]
lazy: true
arguments: ["$design$"]

ezdesign.template_path_registry:
class: EzSystems\EzPlatformDesignEngine\Templating\TemplatePathRegistry
Expand Down Expand Up @@ -54,4 +53,4 @@ services:
- "@ezdesign.asset_path_resolver"
- "@assets._default_package"
calls:
- [setCurrentDesign, ["$design;ezdesign"]]
- [setCurrentDesign, ["$design$"]]
9 changes: 4 additions & 5 deletions doc/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ This behavior can however be controlled by `disable_assets_pre_resolution` setti

```yaml
# ezplatform_prod.yml
ezpublish:
design:
# Force runtime resolution
# Default value is '%kernel.debug%'
disable_assets_pre_resolution: true
ezdesign:
# Force runtime resolution
# Default value is '%kernel.debug%'
disable_assets_pre_resolution: true
```
37 changes: 31 additions & 6 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,44 @@ To define and use a design, you need to:

Here is a simple example:
```yaml
# ezplatform.yml
ezdesign:
# You declare every available designs under "design_list".
design_list:
# my_design will be composed of "theme1" and "theme2"
# "theme1" will be the first tried. If the template cannot be found in "theme1", "theme2" will be tried out.
my_design: [theme1, theme2]

ezpublish:
design:
# You declare every available designs under "list".
list:
# my_design will be composed of "theme1" and "theme2"
# "theme1" will be the first tried. If the template cannot be found in "theme1", "theme2" will be tried out.
my_design: [theme1, theme2]
# ...
system:
my_siteaccess:
# my_siteaccess will use "my_design"
design: my_design
```
> **Note**: Default design for a SiteAccess is `standard` which contains no themes.
> If one is using `@ezdesign` Twig namespace and/or `ezdesign` asset package, the system will always fallback to
> application level and override directories for templates/assets lookup.

## Usage
* [Usage with templates](templates.md)
* [Usage with assets](assets.md)

## Referencing current design
It is possible to reference current design in order to inject it into a service.
To do so, one just need to reference `$design$` dynamic setting:

```yaml
services:
my_service:
class: Foo\Bar
arguments: ["$design$"]
```

It is also possible to use the `ConfigResolver` service (`ezpublish.config.resolver`):

```php
// In a controller
$currentDesign = $this->getConfigResolver->getParameter('design');
```
26 changes: 12 additions & 14 deletions doc/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ Default fallback order is the following:
It is possible to add addition global override directories, similar to `app/Resources/views/`.

```yaml
ezpublish:
design:
template_override_paths:
- "%kernel.root_dir%/another_override_directory"
- "/some/other/directory"
ezdesign:
template_override_paths:
- "%kernel.root_dir%/another_override_directory"
- "/some/other/directory"
```

> `app/Resources/views/` will **always** be the top level override directory.
Expand All @@ -68,13 +67,12 @@ if your PHPStorm project root doesn't match your Symfony project root.

Default config:
```yaml
ezpublish:
design:
phpstorm:
# Activates PHPStorm support
enabled: '%kernel.debug%'
# Path where to store PHPStorm configuration file for additional Twig namespaces (ide-twig.json).
twig_config_path: '%kernel.root_dir%/..'
ezdesign:
phpstorm:
# Activates PHPStorm support
enabled: '%kernel.debug%'
# Path where to store PHPStorm configuration file for additional Twig namespaces (ide-twig.json).
twig_config_path: '%kernel.root_dir%/..'
```

0 comments on commit 731212f

Please sign in to comment.