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 $this->prefix
= '@RapsysPackBundle/Resources/public/';
23 $this->coutput
= 'css/*.pack.css';
25 $this->joutput
= 'js/*.pack.js';
27 $this->ioutput
= 'img/*.pack.jpg';
30 $this->cfilter
= array('CPackFilter');
32 $this->jfilter
= array('JPackFilter');
34 $this->ifilter
= array('IPackFilter');
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)) {
48 $this->prefix
= $this->fileLocator
->locate($this->prefix
);
51 public function getTokenParsers() {
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
)
59 public function getFilters() {
66 '' => array('type' => BBCODE_TYPE_ROOT
),
68 'type' => BBCODE_TYPE_OPTARG
,
69 'open_tag' => '<pre class="{PARAM}">',
70 'close_tag' => '</pre>',
71 'default_arg' => '{CONTENT}'
74 'type' => BBCODE_TYPE_NOARG
,
76 'close_tag' => '</ul>',
80 'type' => BBCODE_TYPE_NOARG
,
82 'close_tag' => '</li>',
87 'type' => BBCODE_TYPE_OPTARG
,
88 'open_tag' => '<a href="{PARAM}">',
89 'close_tag' => '</a>',
90 'default_arg' => '{CONTENT}',
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);
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);
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>'),
113 array('is_safe' => array('html'))