Skip to content

Commit

Permalink
Simplify operator definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Kévin Gomez committed Feb 5, 2017
1 parent ec1652c commit 71aa493
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
12 changes: 9 additions & 3 deletions DependencyInjection/Compiler/OperatorsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds('rulerz.operator') as $id => $attributesSet) {
foreach ($attributesSet as $attributes) {
$executor = $container->getDefinition($attributes['compilation_target']);
if ($container->hasDefinition($target = 'rulerz.target.'.$attributes['target'])) {
$targetDefinition = $container->getDefinition($target);
} elseif ($container->hasDefinition($attributes['target'])) {
$targetDefinition = $container->getDefinition($attributes['target']);
} else {
throw new \LogicException('Unable to find service definition for compilation target: '.$attributes['compilation_target']);
}

if (!empty($attributes['inline']) && $attributes['inline']) {
$executor->addMethodCall('defineInlineOperator', [
$targetDefinition->addMethodCall('defineInlineOperator', [
$attributes['operator'], new Reference($id),
]);
} else {
$executor->addMethodCall('defineOperator', [
$targetDefinition->addMethodCall('defineOperator', [
$attributes['operator'], new Reference($id),
]);
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ services:
operator.array.like:
class: RulerZ\Operator\ArrayExecutor\Like
tags:
- { name: rulerz.operator, compilation_target: rulerz.target.native, operator: like }
- { name: rulerz.operator, target: native, operator: like }
```
In addition to the `rulerz.operator` tag, two other values are needed:
* `compilation_target`: the compilation target we want to register the operator for ;
* `target`: the compilation target we want to register the operator for ;
* `operator`: the name that will be given to the operator in rules.

**Important**: Operators registered as classes must implement the `__invoke`
Expand Down
12 changes: 7 additions & 5 deletions Tests/DependencyInjection/KPhoenRulerZExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use org\bovigo\vfs\vfsStream;

use KPhoen\RulerZBundle\DependencyInjection\KPhoenRulerZExtension;
use KPhoen\RulerZBundle\Validator\Constraints\RuleValidator;
use RulerZ\RulerZ;

class KPhoenRulerZExtensionTest extends AbstractExtensionTestCase
{
Expand Down Expand Up @@ -37,14 +39,14 @@ public function testItLoadsRulerz()
{
$this->load();

$this->assertContainerBuilderHasService('rulerz', 'RulerZ\RulerZ');
$this->assertContainerBuilderHasService('rulerz', RulerZ::class);
}

public function testItLoadsValidators()
{
$this->load();

$this->assertContainerBuilderHasService('rulerz.validator.unique.rule_validator', 'KPhoen\RulerZBundle\Validator\Constraints\RuleValidator');
$this->assertContainerBuilderHasService('rulerz.validator.unique.rule_validator', RuleValidator::class);
}

public function testItCreatesTheCacheDirectory()
Expand Down Expand Up @@ -76,9 +78,9 @@ public function testItLoadsTargetsDefinedInTheConfig()
],
]);

$this->assertContainerBuilderHasService('rulerz.compilation_target.pomm');
$this->assertContainerBuilderHasService('rulerz.compilation_target.doctrine');
$this->assertContainerBuilderNotHasService('rulerz.compilation_target.elastica');
$this->assertContainerBuilderHasService('rulerz.target.pomm');
$this->assertContainerBuilderHasService('rulerz.target.doctrine');
$this->assertContainerBuilderNotHasService('rulerz.target.elastica');
}

/**
Expand Down

0 comments on commit 71aa493

Please sign in to comment.