1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pgs\HashIdBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Hashids\Hashids; |
6
|
|
|
use Pgs\HashIdBundle\ParametersProcessor\Converter\HashidsConverter; |
7
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
8
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
|
13
|
|
|
class HashidsConverterCompilerPass implements CompilerPassInterface |
14
|
|
|
{ |
15
|
2 |
|
public function process(ContainerBuilder $container) |
16
|
|
|
{ |
17
|
2 |
|
if (!class_exists(Hashids::class)) { |
18
|
|
|
return; |
19
|
|
|
} |
20
|
|
|
|
21
|
2 |
|
$this->registerHashidsService($container); |
22
|
2 |
|
$this->registerHashidsConverter($container); |
23
|
2 |
|
$this->setConverterAsDefault($container); |
24
|
2 |
|
} |
25
|
|
|
|
26
|
2 |
|
protected function registerHashidsService(ContainerBuilder $container): void |
27
|
|
|
{ |
28
|
2 |
|
$hashidsDefinition = new Definition( |
29
|
2 |
|
Hashids::class, |
30
|
|
|
[ |
31
|
2 |
|
$container->getParameter('pgs_hash_id.converter.hashids.salt'), |
32
|
2 |
|
$container->getParameter('pgs_hash_id.converter.hashids.min_hash_length'), |
33
|
2 |
|
$container->getParameter('pgs_hash_id.converter.hashids.alphabet'), |
34
|
|
|
] |
35
|
|
|
); |
36
|
2 |
|
$hashidsDefinition->setPublic(false); |
37
|
|
|
|
38
|
2 |
|
$container->addDefinitions(['pgs_hash_id.hashids' => $hashidsDefinition]); |
39
|
2 |
|
} |
40
|
|
|
|
41
|
2 |
|
protected function registerHashidsConverter(ContainerBuilder $container): void |
42
|
|
|
{ |
43
|
2 |
|
$hashidsConverterDefinition = new Definition( |
44
|
2 |
|
HashidsConverter::class, |
45
|
|
|
[ |
46
|
2 |
|
new Reference('pgs_hash_id.hashids'), |
47
|
|
|
] |
48
|
|
|
); |
49
|
|
|
|
50
|
2 |
|
$container->addDefinitions(['pgs_hash_id.converter.hashids' => $hashidsConverterDefinition]); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
2 |
|
protected function setConverterAsDefault(ContainerBuilder $container): void |
54
|
|
|
{ |
55
|
2 |
|
$converterServiceId = 'pgs_hash_id.converter'; |
56
|
2 |
|
if (!$container->hasAlias($converterServiceId)) { |
57
|
2 |
|
$converterServiceAlias = new Alias('pgs_hash_id.converter.hashids'); |
58
|
2 |
|
$container->addAliases([$converterServiceId => $converterServiceAlias]); |
59
|
|
|
} |
60
|
2 |
|
} |
61
|
|
|
} |
62
|
|
|
|