Skip to content

Instantly share code, notes, and snippets.

@danielpereirabp
Last active June 16, 2016 14:47
Show Gist options
  • Save danielpereirabp/f255458f7d1b2b428848412034b199ef to your computer and use it in GitHub Desktop.
Save danielpereirabp/f255458f7d1b2b428848412034b199ef to your computer and use it in GitHub Desktop.
Symfony2 Custom Configuration
<?php
/**
* application/src/App/CoreBundle/DependencyInjection/AppCoreExtension.php
*
*/
namespace App\CoreBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class AppCoreExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
if (!isset($config['amazon_s3']['aws_key'])) {
throw new \InvalidArgumentException('The option "app.amazon_s3.aws_key" must be set.');
}
$container->setParameter('app.amazon_s3.aws_key', $config['amazon_s3']['aws_key']);
if (!isset($config['amazon_s3']['aws_secret_key'])) {
throw new \InvalidArgumentException('The option "app.amazon_s3.aws_secret_key" must be set.');
}
$container->setParameter('app.amazon_s3.aws_secret_key', $config['amazon_s3']['aws_secret_key']);
if (!isset($config['test_variable'])) {
throw new \InvalidArgumentException('The option "app.test_variable" must be set.');
}
$container->setParameter('app.test_variable', $config['test_variable']);
}
}
<?php
/**
* application/app/AppKernel.php
*
*/
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new FOS\ElasticaBundle\FOSElasticaBundle(),
new FOS\JsRoutingBundle\FOSJsRoutingBundle(),
new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
new App\CoreBundle\AppCoreBundle(),
new JMS\SerializerBundle\JMSSerializerBundle(),
new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),
new Liip\ThemeBundle\LiipThemeBundle(),
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
new FOS\RestBundle\FOSRestBundle(),
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
new Lsw\MemcacheBundle\LswMemcacheBundle(),
new Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle(),
new Misd\GuzzleBundle\MisdGuzzleBundle(),
new Gregwar\CaptchaBundle\GregwarCaptchaBundle(),
);
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
$envParameters = $this->getEnvParameters();
$loader->load(function($container) use($envParameters) {
$container->getParameterBag()->add($envParameters);
});
}
public function getCacheDir()
{
return '/tmp/application/cache/'.$this->environment;
}
public function getLogDir()
{
return '/tmp/application/logs';
}
}
# application/app/config/config.yml
app_core:
amazon_s3:
aws_key: "%amazon_aws_key%"
aws_secret_key: "%amazon_aws_secret_key%"
test_variable: "%test.variable%"
<?php
/**
* application/src/App/CoreBundle/DependencyInjection/Configuration.php
*
*/
namespace App\CoreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface {
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder() {
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('app_core');
$rootNode
->children()
->arrayNode('amazon_s3')
->children()
->scalarNode('aws_key')->end()
->scalarNode('aws_secret_key')->end()
->end()
->end()
->scalarNode('test_variable')->end()
->end();
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment