1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of ocubom/html-bundle |
5
|
|
|
* |
6
|
|
|
* © Oscar Cubo Medina <https://ocubom.github.io> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ocubom\HtmlBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Ocubom\HtmlBundle\EventListener\AddHttpHeadersListener; |
15
|
|
|
use Ocubom\Twig\Extension\HtmlAttributesRuntime; |
16
|
|
|
use Ocubom\Twig\Extension\HtmlCompressRuntime; |
17
|
|
|
use Ocubom\Twig\Extension\HtmlExtension; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
20
|
|
|
|
21
|
|
|
class OcubomHtmlExtension extends Extension |
22
|
|
|
{ |
23
|
2 |
|
public function load(array $configs, ContainerBuilder $container): void |
24
|
|
|
{ |
25
|
|
|
// $loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../../config')); |
26
|
|
|
// $loader->load('services.php'); |
27
|
|
|
|
28
|
2 |
|
$configuration = new Configuration(); |
29
|
2 |
|
$config = $this->processConfiguration($configuration, $configs); |
30
|
2 |
|
$this->registerTwigHtmlExtension($container, $config); |
31
|
2 |
|
$this->registerHeadersListener($container, $config); |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
private function registerTwigHtmlExtension(ContainerBuilder $container, array $config): void |
35
|
|
|
{ |
36
|
2 |
|
$container->register(HtmlExtension::class) |
37
|
2 |
|
->addTag('twig.extension'); |
38
|
|
|
|
39
|
2 |
|
$container->register(HtmlAttributesRuntime::class) |
40
|
2 |
|
->addTag('twig.runtime'); |
41
|
|
|
|
42
|
2 |
|
$container->register(HtmlCompressRuntime::class) |
43
|
2 |
|
->setArguments([ |
44
|
2 |
|
$config['compress']['force'], |
45
|
2 |
|
$config['compress']['level'], |
46
|
2 |
|
]) |
47
|
2 |
|
->addTag('twig.runtime'); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
private function registerHeadersListener(ContainerBuilder $container, array $config): void |
51
|
|
|
{ |
52
|
|
|
// Filter enabled header rules |
53
|
2 |
|
$headers = array_filter($config['headers'], function (array $header): bool { |
54
|
1 |
|
return $header['enabled'] ? true : false; |
55
|
2 |
|
}); |
56
|
|
|
|
57
|
|
|
// Only register listener if some rule is defined |
58
|
2 |
|
if (count($headers) > 0) { |
59
|
1 |
|
$container->register(AddHttpHeadersListener::class) |
60
|
1 |
|
->setArguments(array_values($headers)) |
61
|
1 |
|
->addTag('kernel.event_subscriber'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|