Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Nelmio SolariumBundle. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\SolariumBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
|
||
/** | ||
* @author Igor Wiedler <igor@wiedler.ch> | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('nelmio_solarium'); | ||
|
||
$rootNode | ||
->children() | ||
->scalarNode('default_client')->cannotBeEmpty()->defaultValue('default')->end() | ||
->arrayNode('clients') | ||
->requiresAtLeastOneElement() | ||
->prototype('array') | ||
->prototype('variable') | ||
->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} | ||
<?php | ||
|
||
/* | ||
* This file is part of the Nelmio SolariumBundle. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\SolariumBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
|
||
/** | ||
* @author Igor Wiedler <igor@wiedler.ch> | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('nelmio_solarium'); | ||
|
||
$rootNode | ||
->children() | ||
->scalarNode('default_client')->cannotBeEmpty()->defaultValue('default')->end() | ||
->arrayNode('clients') | ||
This comment has been minimized.
Sorry, something went wrong. |
||
->canBeUnset() | ||
->prototype('array') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('client_class')->cannotBeEmpty()->defaultValue('Solarium_Client')->end() | ||
->scalarNode('adapter_class')->cannotBeEmpty()->defaultValue('Solarium_Client_Adapter_Http')->end() | ||
->scalarNode('host')->defaultValue('127.0.0.1')->end() | ||
->scalarNode('port')->defaultValue(8983)->end() | ||
->scalarNode('path')->defaultValue('/solr')->end() | ||
->scalarNode('timeout')->defaultValue(5)->end() | ||
->arrayNode('cores') | ||
This comment has been minimized.
Sorry, something went wrong.
cjunge
|
||
->useAttributeAsKey('key') | ||
->canBeUnset() | ||
->prototype('scalar') | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Nelmio SolariumBundle. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\SolariumBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
|
||
/** | ||
* @author Igor Wiedler <igor@wiedler.ch> | ||
*/ | ||
class NelmioSolariumExtension extends Extension | ||
{ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$processor = new Processor(); | ||
$configuration = new Configuration(); | ||
$config = $processor->processConfiguration($configuration, $configs); | ||
|
||
if ($container->getParameter('kernel.debug') === true) { | ||
$is_debug = true; | ||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('logger.xml'); | ||
} else { | ||
$is_debug = false; | ||
} | ||
|
||
$default_client = $config['default_client']; | ||
|
||
foreach ($config['clients'] as $name => $client_options) { | ||
$client_name = sprintf('solarium.client.%s', $name); | ||
$adapter_name = sprintf('solarium.client.adapter.%s', $name); | ||
|
||
if (isset($client_options['client_class'])) { | ||
$client_class = $client_options['client_class']; | ||
unset($client_options['client_class']); | ||
} else { | ||
$client_class = 'Solarium_Client'; | ||
} | ||
|
||
if (isset($client_options['adapter_class'])) { | ||
$adapter_class = $client_options['adapter_class']; | ||
unset($client_options['adapter_class']); | ||
} else { | ||
$adapter_class = 'Solarium_Client_Adapter_Http'; | ||
} | ||
|
||
$clientDefinition = new Definition($client_class); | ||
$container->setDefinition($client_name, $clientDefinition); | ||
|
||
if ($name == $default_client) { | ||
$container->setAlias('solarium.client', $client_name); | ||
} | ||
|
||
$container | ||
->setDefinition($adapter_name, new Definition($adapter_class)) | ||
->setArguments(array($client_options)); | ||
|
||
$adapter = new Reference($adapter_name); | ||
$container->getDefinition($client_name)->addMethodCall('setAdapter', array($adapter)); | ||
|
||
if ($is_debug) { | ||
$logger = new Reference('solarium.data_collector'); | ||
$container->getDefinition($client_name)->addMethodCall('registerPlugin', array($client_name . '.logger', $logger)); | ||
} | ||
} | ||
} | ||
<?php | ||
|
||
/* | ||
* This file is part of the Nelmio SolariumBundle. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\SolariumBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
|
||
/** | ||
* @author Igor Wiedler <igor@wiedler.ch> | ||
*/ | ||
class NelmioSolariumExtension extends Extension | ||
{ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$processor = new Processor(); | ||
$configuration = new Configuration(); | ||
$config = $processor->processConfiguration($configuration, $configs); | ||
|
||
if ($container->getParameter('kernel.debug') === true) { | ||
$is_debug = true; | ||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('logger.xml'); | ||
} else { | ||
$is_debug = false; | ||
} | ||
|
||
$default_client = $config['default_client']; | ||
if (!count($config['clients'])) { | ||
$config['clients'][$default_client] = array(); | ||
} elseif (count($config['clients']) === 1) { | ||
$default_client = key($config['clients']); | ||
} | ||
|
||
foreach ($config['clients'] as $name => $client_options) { | ||
$client_name = sprintf('solarium.client.%s', $name); | ||
$adapter_name = sprintf('solarium.client.adapter.%s', $name); | ||
|
||
if (isset($client_options['client_class'])) { | ||
$client_class = $client_options['client_class']; | ||
unset($client_options['client_class']); | ||
} else { | ||
$client_class = 'Solarium_Client'; | ||
} | ||
|
||
if (isset($client_options['adapter_class'])) { | ||
$adapter_class = $client_options['adapter_class']; | ||
unset($client_options['adapter_class']); | ||
} else { | ||
$adapter_class = 'Solarium_Client_Adapter_Http'; | ||
} | ||
|
||
$clientDefinition = new Definition($client_class); | ||
$container->setDefinition($client_name, $clientDefinition); | ||
|
||
if ($name == $default_client) { | ||
$container->setAlias('solarium.client', $client_name); | ||
} | ||
|
||
$container | ||
->setDefinition($adapter_name, new Definition($adapter_class)) | ||
->setArguments(array($client_options)); | ||
|
||
$adapter = new Reference($adapter_name); | ||
$container->getDefinition($client_name)->addMethodCall('setAdapter', array($adapter)); | ||
|
||
if ($is_debug) { | ||
$logger = new Reference('solarium.data_collector'); | ||
$container->getDefinition($client_name)->addMethodCall('registerPlugin', array($client_name . '.logger', $logger)); | ||
} | ||
} | ||
} | ||
} |
There is a bug here when using multiple config files to define the parameters for the same client name. If I have a client "default" in config.yml, and "default" in config_test.yml then the settings from config_test are not added to the settings in config. The processed settings appear as 2 parts in the array, with no name defined, just numeric indexes.
Adding ->useAttributeAsKey('name') seems to fix this, and it behaves correctly.
I don't see how having 2 clients defined, with the second one being the default could've worked without that settings, as the clients array don't have named indexes to look up.