1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2014 OpenSky Project Inc
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Assetic\Filter;
13
14use Assetic\Asset\AssetInterface;
15
16/**
17 * JSqueeze filter.
18 *
19 * @link https://github.com/nicolas-grekas/JSqueeze
20 * @author Nicolas Grekas <p@tchwork.com>
21 */
22class JSqueezeFilter implements FilterInterface
23{
24    private $singleLine = true;
25    private $keepImportantComments = true;
26    private $className;
27    private $specialVarRx = false;
28    private $defaultRx;
29
30    public function __construct()
31    {
32        // JSqueeze is namespaced since 2.x, this works with both 1.x and 2.x
33        if (class_exists('\\Patchwork\\JSqueeze')) {
34            $this->className = '\\Patchwork\\JSqueeze';
35            $this->defaultRx = \Patchwork\JSqueeze::SPECIAL_VAR_PACKER;
36        } else {
37            $this->className = '\\JSqueeze';
38            $this->defaultRx = \JSqueeze::SPECIAL_VAR_RX;
39        }
40    }
41
42    public function setSingleLine($bool)
43    {
44        $this->singleLine = (bool) $bool;
45    }
46
47    // call setSpecialVarRx(true) to enable global var/method/property
48    // renaming with the default regex (for 1.x or 2.x)
49    public function setSpecialVarRx($specialVarRx)
50    {
51        if (true === $specialVarRx) {
52            $this->specialVarRx = $this->defaultRx;
53        } else {
54            $this->specialVarRx = $specialVarRx;
55        }
56    }
57
58    public function keepImportantComments($bool)
59    {
60        $this->keepImportantComments = (bool) $bool;
61    }
62
63    public function filterLoad(AssetInterface $asset)
64    {
65    }
66
67    public function filterDump(AssetInterface $asset)
68    {
69        $parser = new $this->className();
70        $asset->setContent($parser->squeeze(
71            $asset->getContent(),
72            $this->singleLine,
73            $this->keepImportantComments,
74            $this->specialVarRx
75        ));
76    }
77}
78