Skip to content

Commit

Permalink
Make config more strict & light for injections
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Jul 9, 2016
1 parent 8aadd3a commit a64e6ea
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 48 deletions.
12 changes: 3 additions & 9 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ public function getConfigTreeBuilder()
->scalarNode('page_class')
->isRequired()
->validate()
->always()
->ifString()
->then(function ($value) {
if (!$value) {
return null;
}
if (!class_exists($value) || !is_a($value, 'Orbitale\Bundle\CmsBundle\Entity\Page', true)) {
throw new InvalidConfigurationException(sprintf(
'Page class must be a valid class extending %s. "%s" given.',
Expand All @@ -53,11 +50,8 @@ public function getConfigTreeBuilder()
->scalarNode('category_class')
->isRequired()
->validate()
->always()
->ifString()
->then(function ($value) {
if (!$value) {
return null;
}
if (!class_exists($value) || !is_a($value, 'Orbitale\Bundle\CmsBundle\Entity\Category', true)) {
throw new InvalidConfigurationException(sprintf(
'Category class must be a valid class extending %s. "%s" given.',
Expand Down Expand Up @@ -101,7 +95,7 @@ public function getConfigTreeBuilder()
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')->defaultFalse()->end()
->scalarNode('ttl')->defaultValue('300')->end()
->integerNode('ttl')->defaultValue(300)->end()
->end()
->end()
->end();
Expand Down
11 changes: 5 additions & 6 deletions DependencyInjection/OrbitaleCmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function load(array $configs, ContainerBuilder $container)
foreach ($config['layouts'] as $name => $layout) {
$config['layouts'][$name] = array_merge(array(
'name' => $name,
'assets_css' => '',
'assets_js' => '',
'assets_css' => [],
'assets_js' => [],
'host' => '',
'pattern' => '',
), $layout);
Expand All @@ -58,10 +58,9 @@ public function load(array $configs, ContainerBuilder $container)
}
});

$container->setParameter('orbitale_cms.page_class', $config['page_class']);
$container->setParameter('orbitale_cms.category_class', $config['category_class']);

$container->setParameter('orbitale_cms.config', $config);
foreach ($config as $key => $value) {
$container->setParameter('orbitale_cms.'.$key, $value);
}

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
16 changes: 8 additions & 8 deletions EventListener/LayoutsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ class LayoutsListener implements EventSubscriberInterface
/**
* @var array
*/
private $config;
private $layouts;

/**
* @var TwigEngine
*/
private $templating;

public function __construct(array $config, TwigEngine $templating)
public function __construct(array $layouts, TwigEngine $templating)
{
$this->config = $config;
$this->layouts = $layouts;
$this->templating = $templating;
}

Expand All @@ -39,9 +39,9 @@ public function __construct(array $config, TwigEngine $templating)
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array('setRequestLayout', 1),
);
return [
KernelEvents::REQUEST => ['setRequestLayout', 1],
];
}

public function setRequestLayout(GetResponseEvent $event)
Expand All @@ -52,7 +52,7 @@ public function setRequestLayout(GetResponseEvent $event)
$path = $request->getPathInfo();
$host = $request->getHost();

$layouts = $this->config['layouts'];
$layouts = $this->layouts;

// As a layout must be set, we force it to be empty if no layout is properly configured.
// Then this will throw an exception, and the user will be warned of the "no-layout" config problem.
Expand All @@ -63,7 +63,7 @@ public function setRequestLayout(GetResponseEvent $event)
if ($layoutConfig['host'] && $host === $layoutConfig['host']) {
$match = true;
}
if ($layoutConfig['pattern'] && preg_match('~'.$layoutConfig['pattern'].'~', $path)) {
if ($layoutConfig['pattern'] && preg_match('~' . $layoutConfig['pattern'] . '~', $path)) {
$match = true;
}
if ($match) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* This class is used to allow all CmsBundle's repositories to use Doctrine cache.
* @author Sandor Farkas <farkas.berlin@gmail.com>
*/
class AbstractRepository extends EntityRepository
class AbstractCmsRepository extends EntityRepository
{
/**
* @var bool
Expand All @@ -30,11 +30,11 @@ class AbstractRepository extends EntityRepository
protected $cacheTtl;

/**
* @param array $config
* @param array $cacheConfig
*/
public function setConfig(array $config)
public function setConfig(array $cacheConfig)
{
$this->cacheEnabled = $config['cache']['enabled'];
$this->cacheTtl = $config['cache']['ttl'];
$this->cacheEnabled = $cacheConfig['enabled'];
$this->cacheTtl = $cacheConfig['ttl'];
}
}
2 changes: 1 addition & 1 deletion Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Orbitale\Bundle\CmsBundle\Entity\Category;

class CategoryRepository extends AbstractRepository
class CategoryRepository extends AbstractCmsRepository
{
/**
* @param array $slugs
Expand Down
2 changes: 1 addition & 1 deletion Repository/PageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Orbitale\Bundle\CmsBundle\Entity\Category;
use Orbitale\Bundle\CmsBundle\Entity\Page;

class PageRepository extends AbstractRepository
class PageRepository extends AbstractCmsRepository
{
/**
* @param Category $category
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ services:
orbitale_cms.listeners.layouts:
class: Orbitale\Bundle\CmsBundle\EventListener\LayoutsListener
arguments:
config: '%orbitale_cms.config%'
config: '%orbitale_cms.layouts%'
templating: "@templating"
tags:
- { name: kernel.event_subscriber }

orbitale_cms.twig.extension:
class: Orbitale\Bundle\CmsBundle\Twig\CmsExtension
arguments:
- '%orbitale_cms.config%'
- '%orbitale_cms.design%'
tags: [ { name: twig.extension } ]

orbitale_cms.doctrine_mapping_listener:
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services_v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
arguments:
- "%orbitale_cms.page_class%"
calls:
- [ 'setConfig', ['%orbitale_cms.config%'] ]
- [ 'setConfig', ['%orbitale_cms.cache%'] ]

orbitale_cms.category_repository:
class: Orbitale\Bundle\CmsBundle\Repository\CategoryRepository
Expand All @@ -15,4 +15,4 @@ services:
arguments:
- "%orbitale_cms.category_class%"
calls:
- [ 'setConfig', ['%orbitale_cms.config%'] ]
- [ 'setConfig', ['%orbitale_cms.cache%'] ]
4 changes: 2 additions & 2 deletions Resources/config/services_v3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ services:
arguments:
- "%orbitale_cms.page_class%"
calls:
- [ 'setConfig', ['%orbitale_cms.config%'] ]
- [ 'setConfig', ['%orbitale_cms.cache%'] ]

orbitale_cms.category_repository:
class: Orbitale\Bundle\CmsBundle\Repository\CategoryRepository
factory: ['@doctrine.orm.entity_manager', 'getRepository']
arguments:
- "%orbitale_cms.category_class%"
calls:
- [ 'setConfig', ['%orbitale_cms.config%'] ]
- [ 'setConfig', ['%orbitale_cms.cache%'] ]
14 changes: 6 additions & 8 deletions Resources/views/Front/index.html.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{% set layout = app.request.attributes.get('_orbitale_cms_layout') -%}
{% extends layout.resource %}

{% set _design_options = orbitale_cms_config.design %}

{% block stylesheets %}
{{ parent() }}
{% for asset_css in layout.assets_css %}
Expand All @@ -24,22 +22,22 @@
{% block title %}{{- page.title -}}{% endblock %}

{% block orbitale_cms_breadcrumbs %}
<div id="breadcrumbs" class="{{ _design_options.breadcrumbs_class }}">
<div id="breadcrumbs" class="{{ orbitale_cms_design.breadcrumbs_class }}">
{% set slugsArray = [] %}
{% if page.homepage %}
<span class="{{ _design_options.breadcrumbs_current_class }}">{{ 'home' | trans({}, 'OrbitaleCms') }} - {{ page.title }}</span>
<span class="{{ orbitale_cms_design.breadcrumbs_current_class }}">{{ 'home' | trans({}, 'OrbitaleCms') }} - {{ page.title }}</span>
{% else %}
<a href="{{ path('orbitale_cms_page') }}" class="{{ _design_options.breadcrumbs_link_class }}">{{ 'home' | trans({}, 'OrbitaleCms') }}</a>
<a href="{{ path('orbitale_cms_page') }}" class="{{ orbitale_cms_design.breadcrumbs_link_class }}">{{ 'home' | trans({}, 'OrbitaleCms') }}</a>
{% for key, breadcrumbPage in pages|reverse %}

<span class="{{ _design_options.breadcrumbs_separator_class }}">{{ _design_options.breadcrumbs_separator }}</span>
<span class="{{ orbitale_cms_design.breadcrumbs_separator_class }}">{{ orbitale_cms_design.breadcrumbs_separator }}</span>

{% set slugsArray = slugsArray | merge({ (key): breadcrumbPage.slug }) %}

{% if breadcrumbPage.id == page.id %}
<span class="{{ _design_options.breadcrumbs_current_class }}" >{{ breadcrumbPage.title }}</span>
<span class="{{ orbitale_cms_design.breadcrumbs_current_class }}" >{{ breadcrumbPage.title }}</span>
{% else %}
<a href="{{ path('orbitale_cms_page', {slugs: slugsArray|join('/')}) }}" class="{{ _design_options.breadcrumbs_link_class }}">
<a href="{{ path('orbitale_cms_page', {slugs: slugsArray|join('/')}) }}" class="{{ orbitale_cms_design.breadcrumbs_link_class }}">
{{ breadcrumbPage.title }}
</a>
{% endif %}
Expand Down
8 changes: 4 additions & 4 deletions Twig/CmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class CmsExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInt
/**
* @var array
*/
protected $cmsConfig;
protected $designConfig;

public function __construct(array $cmsConfig)
public function __construct(array $designConfig)
{
$this->cmsConfig = $cmsConfig;
$this->designConfig = $designConfig;
}

/**
Expand All @@ -34,7 +34,7 @@ public function getName()
public function getGlobals()
{
return array(
'orbitale_cms_config' => $this->cmsConfig,
'orbitale_cms_design' => $this->designConfig,
);
}
}

0 comments on commit a64e6ea

Please sign in to comment.