Skip to content

Commit

Permalink
Merge pull request #140 from WandiParis/fix-issue-137
Browse files Browse the repository at this point in the history
fix(137): change FQN class in services + rename id of the service + tweak tests + update docs
  • Loading branch information
greg0ire authored Mar 22, 2020
2 parents ccd86f4 + 0ae3564 commit 8334b52
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ the `GetLabel` class and invoke it.
use Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel;

$label = new GetLabel();
$label($value, 'Your\Enum\Class');
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class);
```

To enable translation, require the `symfony/translation` component
Expand All @@ -169,26 +169,26 @@ use Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel;
use Symfony\Contracts\Translation\TranslationInterface;

$label = new GetLabel($translator);
$label($value, 'Your\Enum\Class');
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class);
```

If you're using Symfony, tag the service and simply inject it.
If you're using Symfony, alias the service and simply inject it.
If translations are enabled, the `TranslatorInterface` will be automatically injected.

```yaml
services:
# ...
Greg0ire\Enum\Bridge\Symfony\Translator\Label: "@greg0ire_enum.symfony.translator.label"
Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel: "@greg0ire_enum.symfony.translator.get_label"
```
```php
public function index(GetLabel $label)
{
$label($value, 'Your\Enum\Class');
$label($value, 'Your\Enum\Class', 'another_domain'); // Change the translation domain
$label($value, 'Your\Enum\Class', false); // Disable translation. In this case the class prefix wont be added
$label($value, 'Your\Enum\Class', false, true); // Disable translation but keep class prefix
$label($value, 'Your\Enum\Class', false, true, '.'); // Disable translation but keep class prefix with a custom separator
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class);
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class, 'another_domain'); // Change the translation domain
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class, false); // Disable translation. In this case the class prefix wont be added
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class, false, true); // Disable translation but keep class prefix
$label(Your\Enum\Class::VALUE, Your\Enum\Class::class, false, true, '.'); // Disable translation but keep class prefix with a custom separator
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class TranslatorCompilerPass implements CompilerPassInterface
public function process(ContainerBuilder $container): void
{
if (class_exists(AbstractExtension::class) && $container->hasDefinition('translator.default')) {
$container->getDefinition('greg0ire_enum.symfony.translator.label')
$container->getDefinition('greg0ire_enum.symfony.translator.get_label')
->addArgument(new Reference('translator.default'));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<services>
<defaults public="false" />

<service id="greg0ire_enum.symfony.translator.label" class="Greg0ire\Enum\Bridge\Symfony\Translator\Label" public="true">
<service id="greg0ire_enum.symfony.translator.get_label" class="Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel" public="true">
<argument type="service" id="translator.default" />
</service>

Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<services>
<service id="greg0ire_enum.twig.extension.enum" class="Greg0ire\Enum\Bridge\Twig\Extension\EnumExtension">
<tag name="twig.extension"/>
<argument type="service" id="greg0ire_enum.symfony.translator.get_label" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testLabelServiceHasTheTranslator()
$compilerPass->process($this->container);

$this->assertContainerBuilderHasServiceDefinitionWithArgument(
'greg0ire_enum.symfony.translator.label',
'greg0ire_enum.symfony.translator.get_label',
0,
new Reference('translator.default')
);
Expand Down

0 comments on commit 8334b52

Please sign in to comment.