]> Raphaƫl G. Git Repositories - packbundle/blob - Twig/PackExtension.php
Update composer.json
[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 $this->prefix = '@RapsysPackBundle/Resources/public/';
21
22 //Set default coutput
23 $this->coutput = 'css/*.pack.css';
24 //Set default joutput
25 $this->joutput = 'js/*.pack.js';
26 //Set default ioutput
27 $this->ioutput = 'img/*.pack.jpg';
28
29 //Set default cfilter
30 $this->cfilter = array('CPackFilter');
31 //Set default jfilter
32 $this->jfilter = array('JPackFilter');
33 //Set default ifilter
34 $this->ifilter = array('IPackFilter');
35
36 //Load configuration
37 if ($containerInterface->hasParameter('rapsys_pack')) {
38 if ($parameters = $containerInterface->getParameter('rapsys_pack')) {
39 foreach($parameters as $k => $v) {
40 if (isset($this->$k) && !empty($v)) {
41 $this->$k = $v;
42 }
43 }
44 }
45 }
46
47 //Fix prefix
48 $this->prefix = $this->fileLocator->locate($this->prefix);
49 }
50
51 public function getTokenParsers() {
52 return array(
53 new PackTokenParser($this->fileLocator, $this->containerInterface, $this->assetsPackages, $this->prefix, 'stylesheet', $this->coutput, $this->cfilter),
54 new PackTokenParser($this->fileLocator, $this->containerInterface, $this->assetsPackages, $this->prefix, 'javascript', $this->joutput, $this->jfilter),
55 new PackTokenParser($this->fileLocator, $this->containerInterface, $this->assetsPackages, $this->prefix, 'image', $this->ioutput, $this->ifilter)
56 );
57 }
58
59 public function getFilters() {
60 return array(
61 new TwigFilter(
62 'bb2html',
63 function($text) {
64 $ctx = bbcode_create(
65 array(
66 '' => array('type' => BBCODE_TYPE_ROOT),
67 'code' => array(
68 'type' => BBCODE_TYPE_OPTARG,
69 'open_tag' => '<pre class="{PARAM}">',
70 'close_tag' => '</pre>',
71 'default_arg' => '{CONTENT}'
72 ),
73 'ul' => array(
74 'type' => BBCODE_TYPE_NOARG,
75 'open_tag' => '<ul>',
76 'close_tag' => '</ul>',
77 'childs' => 'li'
78 ),
79 'li' => array(
80 'type' => BBCODE_TYPE_NOARG,
81 'open_tag' => '<li>',
82 'close_tag' => '</li>',
83 'parent' => 'ul',
84 'childs' => 'url'
85 ),
86 'url' => array(
87 'type' => BBCODE_TYPE_OPTARG,
88 'open_tag' => '<a href="{PARAM}">',
89 'close_tag' => '</a>',
90 'default_arg' => '{CONTENT}',
91 'parent' => 'p,li'
92 )
93 )
94 );
95 $text = nl2br(bbcode_parse($ctx, htmlspecialchars($text)));
96 if (preg_match_all('#\<pre[^>]*\>(.*?)\</pre\>#s', $text, $matches) && !empty($matches[1])) {
97 foreach($matches[1] as $string) {
98 $text = str_replace($string, str_replace('<br />', '', $string), $text);
99 }
100 }
101 if (preg_match_all('#\<ul[^>]*\>(.*?)\</ul\>#s', $text, $matches) && !empty($matches[1])) {
102 foreach($matches[1] as $string) {
103 $text = str_replace($string, str_replace('<br />', '', $string), $text);
104 }
105 }
106 $text = preg_replace(
107 array('#(<br />(\r?\n?))*<pre#s', '#</pre>(<br />(\r?\n?))*#', '#(<br />(\r?\n?))*<ul#s', '#</ul>(<br />(\r?\n?))*#', '#(<br />(\r?\n?)){2,}#'),
108 array('</p>\2<pre', '</pre>\2<p>', '</p>\2<ul', '</ul>\2<p>', '</p>\2<p>'),
109 $text
110 );
111 return $text;
112 },
113 array('is_safe' => array('html'))
114 )
115 );
116 }
117 }