|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Alpha\TwigBundle; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
10
|
|
|
|
|
11
|
|
|
class AlphaTwigBundle extends Bundle |
|
12
|
|
|
{ |
|
13
|
|
|
private const PARAM_TEMPLATE_MAPPING_DIR = 'alpha_twig.entity.template.mapping_dir'; |
|
14
|
|
|
private const PARAM_TEMPLATE_MAPPING_NAMESPACE = 'alpha_twig.entity.template.class'; |
|
15
|
|
|
private const DEFAULT_TEMPLATE_MAPPING_NAMESPACE = 'Alpha\TwigBundle\Entity'; |
|
16
|
|
|
|
|
17
|
|
|
public function build(ContainerBuilder $container): void |
|
18
|
|
|
{ |
|
19
|
|
|
$this->addRegisterMappingsPass($container); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private function addRegisterMappingsPass(ContainerBuilder $container): void |
|
23
|
|
|
{ |
|
24
|
|
|
$mappings = [ |
|
25
|
|
|
$this->resolveMappingDir($container) => $this->resolveMappingNamespace($container), |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
$container->addCompilerPass(DoctrineOrmMappingsPass::createYamlMappingDriver($mappings)); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function resolveMappingDir(ContainerBuilder $container): string |
|
32
|
|
|
{ |
|
33
|
|
|
if ($container->hasParameter(self::PARAM_TEMPLATE_MAPPING_DIR)) { |
|
34
|
|
|
return $container->getParameter(self::PARAM_TEMPLATE_MAPPING_DIR); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$realpath = realpath(__DIR__.'/Resources/config/doctrine'); |
|
38
|
|
|
if ($realpath === false) { |
|
39
|
|
|
throw new \RuntimeException('Cannot resolve the location of the mapping directory'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $realpath; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function resolveMappingNamespace(ContainerBuilder $container): string |
|
46
|
|
|
{ |
|
47
|
|
|
if ($container->hasParameter(self::PARAM_TEMPLATE_MAPPING_NAMESPACE)) { |
|
48
|
|
|
$namespace = preg_replace( |
|
49
|
|
|
'#^(.*)\\\\[^\\\\]+$#', |
|
50
|
|
|
'$1', |
|
51
|
|
|
$container->getParameter(self::PARAM_TEMPLATE_MAPPING_NAMESPACE) |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
return is_string($namespace) && !empty($namespace) ? $namespace : '\\'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return self::DEFAULT_TEMPLATE_MAPPING_NAMESPACE; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|