]> Raphaël G. Git Repositories - packbundle/blob - Extension/PackExtension.php
Import contact form
[packbundle] / Extension / PackExtension.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
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 Rapsys\PackBundle\Extension;
13
14 use Psr\Container\ContainerInterface;
15
16 use Rapsys\PackBundle\Parser\TokenParser;
17 use Rapsys\PackBundle\RapsysPackBundle;
18 use Rapsys\PackBundle\Util\IntlUtil;
19 use Rapsys\PackBundle\Util\SluggerUtil;
20
21 use Symfony\Component\Routing\RouterInterface;
22 use Symfony\Component\HttpKernel\Config\FileLocator;
23
24 use Twig\Extension\AbstractExtension;
25
26 /**
27 * {@inheritdoc}
28 */
29 class PackExtension extends AbstractExtension {
30 /**
31 * Config array
32 */
33 protected array $config;
34
35 /**
36 * The stream context instance
37 */
38 protected mixed $ctx;
39
40 /**
41 * Creates pack extension
42 *
43 * {@inheritdoc}
44 *
45 * @link https://twig.symfony.com/doc/2.x/advanced.html
46 *
47 * @param ContainerInterface $container The ContainerInterface instance
48 * @param IntlUtil $intl The IntlUtil instance
49 * @param FileLocator $locator The FileLocator instance
50 * @param RouterInterface $router The RouterInterface instance
51 * @param SluggerUtil $slugger The SluggerUtil instance
52 */
53 public function __construct(protected ContainerInterface $container, protected IntlUtil $intl, protected FileLocator $locator, protected RouterInterface $router, protected SluggerUtil $slugger) {
54 //Retrieve config
55 $this->config = $container->getParameter(RapsysPackBundle::getAlias());
56
57 //Set ctx
58 $this->ctx = stream_context_create($this->config['context']);
59 }
60
61 /**
62 * Returns a filter array to add to the existing list.
63 *
64 * @return \Twig\TwigFilter[]
65 */
66 public function getTokenParsers(): array {
67 return [
68 new TokenParser($this->container, $this->locator, $this->router, $this->slugger, $this->config, $this->ctx, 'css', 'stylesheet'),
69 new TokenParser($this->container, $this->locator, $this->router, $this->slugger, $this->config, $this->ctx, 'js', 'javascript'),
70 new TokenParser($this->container, $this->locator, $this->router, $this->slugger, $this->config, $this->ctx, 'img', 'image')
71 ];
72 }
73
74 /**
75 * Returns a filter array to add to the existing list.
76 *
77 * @return \Twig\TwigFilter[]
78 */
79 public function getFilters(): array {
80 return [
81 new \Twig\TwigFilter('base64_decode', 'base64_decode'),
82 new \Twig\TwigFilter('base64_encode', 'base64_encode'),
83 new \Twig\TwigFilter('download', 'file_get_contents', [false, null]),
84 new \Twig\TwigFilter('hash', [$this->slugger, 'hash']),
85 new \Twig\TwigFilter('intlcurrency', [$this->intl, 'currency']),
86 new \Twig\TwigFilter('intldate', [$this->intl, 'date'], ['needs_environment' => true]),
87 new \Twig\TwigFilter('intlnumber', [$this->intl, 'number']),
88 new \Twig\TwigFilter('lcfirst', 'lcfirst'),
89 new \Twig\TwigFilter('short', [$this->slugger, 'short']),
90 new \Twig\TwigFilter('slug', [$this->slugger, 'slug']),
91 new \Twig\TwigFilter('ucfirst', 'ucfirst'),
92 new \Twig\TwigFilter('unshort', [$this->slugger, 'unshort'])
93 ];
94 }
95 }