]> Raphaƫl G. Git Repositories - packbundle/blob - Twig/PackExtension.php
Remove type hinting not supported in php < 7.0
[packbundle] / Twig / PackExtension.php
1 <?php
2 // src/Rapsys/PackBundle/Twig/PackExtension.php
3 namespace Rapsys\PackBundle\Twig;
4
5 use Symfony\Component\HttpKernel\Config\FileLocator;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Symfony\Component\Asset\Packages;
8 use Twig\TwigFilter;
9
10 class PackExtension extends \Twig_Extension {
11 public function __construct(FileLocator $fileLocator, ContainerInterface $containerInterface, Packages $assetsPackages) {
12 //Set file locator
13 $this->fileLocator = $fileLocator;
14 //Set container interface
15 $this->containerInterface = $containerInterface;
16 //Set assets packages
17 $this->assetsPackages = $assetsPackages;
18
19 //Set default prefix
20 //XXX: require symfony 3.3
21 $this->prefix = $this->containerInterface->getParameter('kernel.project_dir').'/web/';
22
23 //Set default coutput
24 $this->coutput = 'css/*.pack.css';
25 //Set default joutput
26 $this->joutput = 'js/*.pack.js';
27 //Set default ioutput
28 $this->ioutput = 'img/*.pack.jpg';
29
30 //Set default cfilter
31 $this->cfilter = array('Rapsys\PackBundle\Twig\Filter\CPackFilter');
32 //Set default jfilter
33 $this->jfilter = array('Rapsys\PackBundle\Twig\Filter\JPackFilter');
34 //Set default ifilter
35 $this->ifilter = array('Rapsys\PackBundle\Twig\Filter\IPackFilter');
36
37 //Load configuration
38 if ($containerInterface->hasParameter('rapsys_pack')) {
39 if ($parameters = $containerInterface->getParameter('rapsys_pack')) {
40 foreach($parameters as $k => $v) {
41 if (isset($this->$k) && !empty($v)) {
42 $this->$k = $v;
43 }
44 }
45 }
46 }
47
48 //Fix prefix
49 $this->prefix = $this->fileLocator->locate($this->prefix);
50 }
51
52 public function getTokenParsers() {
53 return array(
54 new PackTokenParser($this->fileLocator, $this->containerInterface, $this->assetsPackages, $this->prefix, 'stylesheet', $this->coutput, $this->cfilter),
55 new PackTokenParser($this->fileLocator, $this->containerInterface, $this->assetsPackages, $this->prefix, 'javascript', $this->joutput, $this->jfilter),
56 new PackTokenParser($this->fileLocator, $this->containerInterface, $this->assetsPackages, $this->prefix, 'image', $this->ioutput, $this->ifilter)
57 );
58 }
59
60 public function getFilters() {
61 return array(
62 new TwigFilter(
63 'bb2html',
64 function($text) {
65 $ctx = bbcode_create(
66 array(
67 '' => array('type' => BBCODE_TYPE_ROOT),
68 'code' => array(
69 'type' => BBCODE_TYPE_OPTARG,
70 'open_tag' => '<pre class="{PARAM}">',
71 'close_tag' => '</pre>',
72 'default_arg' => '{CONTENT}'
73 ),
74 'ul' => array(
75 'type' => BBCODE_TYPE_NOARG,
76 'open_tag' => '<ul>',
77 'close_tag' => '</ul>',
78 'childs' => 'li'
79 ),
80 'li' => array(
81 'type' => BBCODE_TYPE_NOARG,
82 'open_tag' => '<li>',
83 'close_tag' => '</li>',
84 'parent' => 'ul',
85 'childs' => 'url'
86 ),
87 'url' => array(
88 'type' => BBCODE_TYPE_OPTARG,
89 'open_tag' => '<a href="{PARAM}">',
90 'close_tag' => '</a>',
91 'default_arg' => '{CONTENT}',
92 'parent' => 'p,li'
93 )
94 )
95 );
96 $text = nl2br(bbcode_parse($ctx, htmlspecialchars($text)));
97 if (preg_match_all('#\<pre[^>]*\>(.*?)\</pre\>#s', $text, $matches) && !empty($matches[1])) {
98 foreach($matches[1] as $string) {
99 $text = str_replace($string, str_replace('<br />', '', $string), $text);
100 }
101 }
102 if (preg_match_all('#\<ul[^>]*\>(.*?)\</ul\>#s', $text, $matches) && !empty($matches[1])) {
103 foreach($matches[1] as $string) {
104 $text = str_replace($string, str_replace('<br />', '', $string), $text);
105 }
106 }
107 $text = preg_replace(
108 array('#(<br />(\r?\n?))*<pre#s', '#</pre>(<br />(\r?\n?))*#', '#(<br />(\r?\n?))*<ul#s', '#</ul>(<br />(\r?\n?))*#', '#(<br />(\r?\n?)){2,}#'),
109 array('</p>\2<pre', '</pre>\2<p>', '</p>\2<ul', '</ul>\2<p>', '</p>\2<p>'),
110 $text
111 );
112 return $text;
113 },
114 array('is_safe' => array('html'))
115 )
116 );
117 }
118 }