|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gnutix\Twig\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Bundle\TwigBundle\DependencyInjection\Configuration as SymfonyTwigConfiguration; |
|
8
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
9
|
|
|
use Symfony\Component\Config\FileLocator; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
14
|
|
|
use Twig\Environment; |
|
15
|
|
|
use Twig\Loader\FilesystemLoader; |
|
16
|
|
|
|
|
17
|
|
|
final class Extension implements ExtensionInterface |
|
18
|
|
|
{ |
|
19
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
|
20
|
|
|
{ |
|
21
|
|
|
$configProcessor = new Processor(); |
|
22
|
|
|
$config = $configProcessor->processConfiguration(new SymfonyTwigConfiguration(), $configs); |
|
23
|
|
|
|
|
24
|
|
|
// Create Twig's filesystem loader definition |
|
25
|
|
|
$twigLoaderDefinition = $container->register('twig.loader.filesystem', FilesystemLoader::class); |
|
26
|
|
|
|
|
27
|
|
|
// Add the paths from the configuration |
|
28
|
|
|
foreach ($config['paths'] as $path => $namespace) { |
|
29
|
|
|
$twigLoaderDefinition->addMethodCall('addPath', $namespace ? [$path, $namespace] : [$path]); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// Create Twig's environment definition |
|
33
|
|
|
$twigDefinition = $container->register('twig', Environment::class) |
|
34
|
|
|
->setPublic(true) |
|
35
|
|
|
->addArgument(new Reference('twig.loader.filesystem')); |
|
36
|
|
|
|
|
37
|
|
|
// Add the global variables |
|
38
|
|
|
foreach ($config['globals'] as $key => $global) { |
|
39
|
|
|
// Allows to reference services |
|
40
|
|
|
if (isset($global['type']) && 'service' === $global['type']) { |
|
41
|
|
|
$global['value'] = new Reference($global['id']); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$twigDefinition->addMethodCall('addGlobal', [$key, $global['value']]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Add the Twig's options argument |
|
48
|
|
|
$twigOptions = $config; |
|
49
|
|
|
foreach (['form', 'globals', 'extensions'] as $key) { |
|
50
|
|
|
unset($twigOptions[$key]); |
|
51
|
|
|
} |
|
52
|
|
|
$twigDefinition->addArgument($twigOptions); |
|
53
|
|
|
|
|
54
|
|
|
// Load the services |
|
55
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
56
|
|
|
$loader->load('services.yml'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getNamespace(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return ''; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getXsdValidationBasePath(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return ''; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getAlias(): string |
|
70
|
|
|
{ |
|
71
|
|
|
return 'twig'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|