2 // src/Rapsys/PackBundle/Twig/PackExtension.php
3 namespace Rapsys\PackBundle\Twig
;
5 use Symfony\Component\HttpKernel\Config\FileLocator
;
6 use Symfony\Component\DependencyInjection\ContainerInterface
;
7 use Symfony\Component\Asset\Packages
;
10 class PackExtension
extends \Twig_Extension
{
11 public function __construct(FileLocator
$fileLocator, ContainerInterface
$containerInterface, Packages
$assetsPackages) {
13 $this->fileLocator
= $fileLocator;
14 //Set container interface
15 $this->containerInterface
= $containerInterface;
17 $this->assetsPackages
= $assetsPackages;
20 //XXX: require symfony 3.3
21 $this->prefix
= $this->containerInterface
->getParameter('kernel.project_dir').'/web/';
24 $this->coutput
= 'css/*.pack.css';
26 $this->joutput
= 'js/*.pack.js';
28 $this->ioutput
= 'img/*.pack.jpg';
31 $this->cfilter
= array('CPackFilter');
33 $this->jfilter
= array('JPackFilter');
35 $this->ifilter
= array('IPackFilter');
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)) {
49 $this->prefix
= $this->fileLocator
->locate($this->prefix
);
52 public function getTokenParsers() {
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
)
60 public function getFilters() {
67 '' => array('type' => BBCODE_TYPE_ROOT
),
69 'type' => BBCODE_TYPE_OPTARG
,
70 'open_tag' => '<pre class="{PARAM}">',
71 'close_tag' => '</pre>',
72 'default_arg' => '{CONTENT}'
75 'type' => BBCODE_TYPE_NOARG
,
77 'close_tag' => '</ul>',
81 'type' => BBCODE_TYPE_NOARG
,
83 'close_tag' => '</li>',
88 'type' => BBCODE_TYPE_OPTARG
,
89 'open_tag' => '<a href="{PARAM}">',
90 'close_tag' => '</a>',
91 'default_arg' => '{CONTENT}',
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);
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);
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>'),
114 array('is_safe' => array('html'))