Skip to content

Commit

Permalink
Merge pull request #17 from jrobeson/use-config-from-container-extens…
Browse files Browse the repository at this point in the history
…ions

Implement configuration via container extensions
  • Loading branch information
vworldat committed Jul 28, 2014
2 parents f1201cf + 62ef0fb commit 91c3749
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 41 deletions.
51 changes: 24 additions & 27 deletions Controller/BaseStaticPageController.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

/*
* This file is part of the c33s\StaticPageContentBundle.
*
* (c) consistency <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This file is part of the c33s\StaticPageContentBundle.
*
* (c) consistency <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace c33s\StaticPageContentBundle\Controller;

Expand All @@ -23,17 +22,6 @@
*/
class BaseStaticPageController extends Controller
{
/**
* Should the Static content be sanboxed?
*
* Only works for the default Content Container
*
* http://twig.sensiolabs.org/doc/api.html#sandbox-extension
*
* @var Boolean
*/
protected $isSandboxed = false;

/**
* Returns the name of the Bundle, where the templates, which are
* containing the static content, are stored
Expand All @@ -42,7 +30,7 @@ class BaseStaticPageController extends Controller
*/
protected function getContentBundleName()
{
return 'c33sStaticPageContentBundle';
return $this->container->getParameter('c33s_static_pages.content_bundle');
}

/**
Expand All @@ -56,12 +44,21 @@ protected function getContentBundleName()
*/
protected function getContentFolderName()
{
return 'Content';
return $this->container->getParameter('c33s_static_pages.content_dir');
}

/**
* Should the Static content be sandboxed?
*
* Only works for the default Content Container
*
* http://twig.sensiolabs.org/doc/api.html#sandbox-extension
*
* @return bool
*/
protected function isSandboxed()
{
return $this->isSandboxed;
return $this->container->getParameter('c33s_static_pages.use_template_sandbox');
}

/**
Expand Down Expand Up @@ -98,7 +95,7 @@ protected function getContentLocation($contentName, $subfolder = "")
*/
protected function getTemplateExtension()
{
return '.html.twig';
return $this->container->getParameter('c33s_static_pages.template_extension');
}

/**
Expand All @@ -108,7 +105,7 @@ protected function getTemplateExtension()
*/
protected function getContainerLocation()
{
return 'c33sStaticPageContentBundle:Content:_content_container.html.twig';
return $this->container->getParameter('c33s_static_pages.wrapper_template');
}

/**
Expand All @@ -119,7 +116,7 @@ protected function getContainerLocation()
*/
protected function getBaseTemplateLocation()
{
return '::base.html.twig';
return $this->container->getParameter('c33s_static_pages.base_template');
}

/**
Expand Down Expand Up @@ -162,7 +159,7 @@ public function showAction($name, $subfolder="")
*/
public function isUsingTranslations()
{
return false;
return $this->container->getParameter('c33s_static_pages.prefer_locale_templates');
}

/**
Expand Down
62 changes: 62 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* This file is part of the c33s\StaticPageContentBundle.
*
* (c) consistency <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace c33s\StaticPageContentBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
/* @var Symfony\Component\Config\Definition\Builder\NodeDefinition */
$rootNode = $treeBuilder->root('c33s_static_pages');

$rootNode
->children()
->scalarNode('base_template')
->defaultValue('::base.html.twig')
->info('Template that the content wrapper template will extend from')
->end()
->scalarNode('template_extension')
->defaultValue('.html.twig')
->info('Template file name extension')
->end()
->scalarNode('content_dir')
->defaultValue('Content')
->info('Relative path from the views directory to a directory containing' . PHP_EOL .
'the page templates.')
->end()
->scalarNode('content_bundle')
->defaultValue('c33sStaticPageContentBundle')
->info('Bundle that holds the static pages')
->end()
->scalarNode('wrapper_template')
->defaultValue('c33sStaticPageContentBundle:Content:_content_container.html.twig')
->info('Template used to wrap your static pages')
->end()
->booleanNode('use_template_sandbox')
->defaultFalse()
->info('Should the pages use the twig sandbox extension?' . PHP_EOL .
'(http://twig.sensiolabs.org/doc/api.html#sandbox-extension)' . PHP_EOL .
'NOTE: it only works when using the default content template')
->end()
->booleanNode('prefer_locale_templates')
->defaultFalse()
->info('use templates for the requested locale if they exist. '. PHP_EOL .
'These templates should be placed in a folder named after the locale in the content directory.')
->end()
->end();

return $treeBuilder;
}
}
42 changes: 42 additions & 0 deletions DependencyInjection/StaticPagesExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* This file is part of the c33s\StaticPageContentBundle.
*
* (c) consistency <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace c33s\StaticPageContentBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;

class StaticPagesExtension extends ConfigurableExtension
{
/**
* {@inheritdoc}
*/
protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
{
$fileLocator = new FileLocator(__DIR__.'/../Resources/config');
$loader = new XmlFileLoader($container, $fileLocator);
$loader->load('services.xml');

foreach($mergedConfig as $name => $value) {
$container->setParameter($this->getAlias() . '.' . $name, $value);
}
}

/**
* {@inheritdoc}
*/
public function getAlias()
{
return 'c33s_static_pages';
}
}

14 changes: 14 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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">

<parameters>
<parameter key="c33s_static_pages.base_template"/>
<parameter key="c33s_static_pages.content_bundle"/>
<parameter key="c33s_static_pages.content_dir"/>
<parameter key="c33s_static_pages.template_extension"/>
<parameter key="c33s_static_pages.prefer_locale_templates"/>
<parameter key="c33s_static_pages.use_template_sandbox"/>
<parameter key="c33s_static_pages.wrapper_template"/>
</parameters>
</container>
41 changes: 41 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the c33s\StaticPageContentBundle.
*
* (c) consistency <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace c33s\StaticPageContentBundle\Tests\DependencyInjection;

use c33s\StaticPageContentBundle\DependencyInjection\Configuration;
use Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase;

class ConfigurationTest extends AbstractConfigurationTestCase
{
protected function getConfiguration()
{
return new Configuration();
}

/**
* @test
*/
public function itSetsDefaultConfiguration()
{
$this->assertProcessedConfigurationEquals(array(
array()
), array(
'base_template' => '::base.html.twig',
'content_bundle' => 'c33sStaticPageContentBundle',
'content_dir' => 'Content',
'wrapper_template' => 'c33sStaticPageContentBundle:Content:_content_container.html.twig',
'template_extension' => '.html.twig',
'use_template_sandbox' => false,
'prefer_locale_templates' => false
));
}
}

48 changes: 48 additions & 0 deletions Tests/DependencyInjection/StaticPagesExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* This file is part of the c33s\StaticPageContentBundle.
*
* (c) consistency <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace c33s\StaticPageContentBundle\Tests\DependencyInjection;

use c33s\StaticPageContentBundle\DependencyInjection\StaticPagesExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;

class StaticPagesExtensionTest extends AbstractExtensionTestCase
{
protected function getContainerExtensions()
{
return array(new StaticPagesExtension());
}

/**
* @test
*/
public function itSetsDefaultParameters()
{
$this->load();

$this->assertContainerBuilderHasParameter(
'c33s_static_pages.content_bundle',
'c33sStaticPageContentBundle'
);
}

/**
* @test
*/
public function itCanChangeParameters()
{
$config = array('content_dir' => 'pages');
$this->load($config);

$this->assertContainerBuilderHasParameter(
'c33s_static_pages.content_dir',
'pages'
);
}
}
7 changes: 7 additions & 0 deletions Tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
if (!@include(__DIR__ . '/../vendor/autoload.php')) {
die('You must set up the project dependencies, run the following commands:
wget http://getcomposer.org/composer.phar
php composer.phar install --dev
');
}
23 changes: 13 additions & 10 deletions c33sStaticPageContentBundle.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<?php

/*
* This file is part of the c33s\StaticPageContentBundle.
*
* (c) consistency <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* This file is part of the c33s\StaticPageContentBundle.
*
* (c) consistency <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace c33s\StaticPageContentBundle;

use c33s\StaticPageContentBundle\DependencyInjection\StaticPagesExtension;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class c33sStaticPageContentBundle extends Bundle
{
public function getContainerExtension()
{
return new StaticPagesExtension();
}
}
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
],
"require":
{
"php": ">=5.3.0",
"symfony/framework-bundle": ">=2.0.0"
"php": ">=5.3.3",
"symfony/framework-bundle": "~2.3"
},
"require-dev":
{
"phpunit/phpunit": "~4.1",
"matthiasnoback/symfony-dependency-injection-test": "0.*"
},
"autoload":
{
Expand All @@ -26,5 +31,3 @@
}
}
}


Loading

0 comments on commit 91c3749

Please sign in to comment.