Skip to content

Commit

Permalink
Merge pull request #66 from uwej711/dev_configure_plugins
Browse files Browse the repository at this point in the history
make it possible to configure plugins
  • Loading branch information
Seldaek authored Dec 4, 2016
2 parents c3ac9ec + adae4ea commit a2dbb81
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
14 changes: 14 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('plugins')
->canBeUnset()
->useAttributeAsKey('name')
->prototype('array')
->validate()
->ifTrue(function ($v) { return !empty($v['plugin_class']) && !empty($v['plugin_service']); })
->thenInvalid('Only either a plugin class or a plugin service can be set')
->end()
->children()
->scalarNode('plugin_class')->end()
->scalarNode('plugin_service')->end()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
15 changes: 15 additions & 0 deletions DependencyInjection/NelmioSolariumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
class NelmioSolariumExtension extends Extension
{
/**
* @param array $configs
* @param ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
Expand Down Expand Up @@ -123,6 +127,17 @@ public function load(array $configs, ContainerBuilder $container)
$clientDefinition->addMethodCall('setAdapter', array($clientOptions['adapter_class']));
}

if (isset($clientOptions['plugins'])) {
foreach ($clientOptions['plugins'] as $pluginName => $pluginOptions) {
if (isset($pluginOptions['plugin_class'])) {
$plugin = $pluginOptions['plugin_class'];
} else {
$plugin = new Reference($pluginOptions['plugin_service']);
}
$clientDefinition->addMethodCall('registerPlugin', array($pluginName, $plugin));
}
}

if ($isDebug) {
$logger = new Reference('solarium.data_collector');
$container->getDefinition($clientName)->addMethodCall('registerPlugin', array($clientName . '.logger', $logger));
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ or if you have configured a default client
$registry = $this->get('solarium.client_registry');
$default = $registry->getClient();
```
## Plugins

Solarium works with plugins. If you want to use your own plugins, you can register a plugin in the bundle configuration either with a service id or the plugin class:

```yaml
nelmio_solarium:
clients:
default:
plugins:
test_plugin_service:
plugin_service: plugin _service_id
test_plugin_classname:
plugin_class: Some\Plugin\TestPlugin
```

## Overriding Classes

Expand Down
29 changes: 28 additions & 1 deletion Tests/NelmioSolariumExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nelmio\SolariumBundle\DependencyInjection\NelmioSolariumExtension;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

class NelmioSolariumExtensionTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -162,6 +163,27 @@ public function testDefaultClient()
$this->assertInstanceOf('Nelmio\SolariumBundle\Tests\StubClient', $container->get('solarium.client.client2'));
}

public function testPlugins()
{
$config = array(
'clients' => array(
'client' => array(
'plugins' => array('plugin1' => array('plugin_service' => 'my_plugin'), 'plugin2' => array('plugin_class' => 'Nelmio\SolariumBundle\Tests\MyPluginClass'))
)
),
);

$container = $this->createCompiledContainerForConfig($config, true, array('my_plugin' => new Definition('Nelmio\SolariumBundle\Tests\MyPluginClass')));

$client = $container->get('solarium.client');
$plugin1 = $client->getPlugin('plugin1');
$plugin2 = $client->getPlugin('plugin2');

$this->assertInstanceOf('Nelmio\SolariumBundle\Tests\MyPluginClass', $plugin1);
$this->assertInstanceOf('Nelmio\SolariumBundle\Tests\MyPluginClass', $plugin2);
}


public function testDsnAndOtherParamsTogether()
{
$config = array(
Expand Down Expand Up @@ -471,10 +493,11 @@ public function testLoadBalancer()

}

private function createCompiledContainerForConfig($config, $debug = false)
private function createCompiledContainerForConfig($config, $debug = false, $extraServices = array())
{
$container = $this->createContainer($debug);
$container->registerExtension(new FrameworkExtension());
$container->addDefinitions($extraServices);
$container->registerExtension(new NelmioSolariumExtension());
$container->loadFromExtension('framework', array());
$container->loadFromExtension('nelmio_solarium', $config);
Expand Down Expand Up @@ -506,3 +529,7 @@ private function compileContainer(ContainerBuilder $container)
class StubClient extends \Solarium\Client
{
}

class MyPluginClass extends \Solarium\Core\Plugin\Plugin {

}

0 comments on commit a2dbb81

Please sign in to comment.