Completed
Push — master ( 26f90e...c3c122 )
by Lars
03:13 queued 11s
created

MinifyHtmlExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\twig;
6
7
use Twig_Environment;
8
use voku\cache\Cache;
9
use voku\helper\HtmlMin;
10
11
/**
12
 * Class MinifyHtmlExtension
13
 *
14
 * @copyright Copyright (c) 2015 Marcel Voigt <[email protected]>
15
 * @copyright Copyright (c) 2017 Lars Moelleken <[email protected]>
16
 */
17
class MinifyHtmlExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
18
{
19
  /**
20
   * @var array
21
   */
22
  private $options = [
23
      'is_safe'           => ['html'],
24
      'needs_environment' => true,
25
  ];
26
27
  /**
28
   * @var callable
29
   */
30
  private $callable;
31
32
  /**
33
   * @var HtmlMin
34
   */
35
  private $minifier;
36
37
  /**
38
   * @var bool
39
   */
40
  private $forceCompression = false;
41
42
  /**
43
   * MinifyHtmlExtension constructor.
44
   *
45
   * @param HtmlMin $htmlMin
46
   * @param bool    $forceCompression Default: false. Forces compression regardless of Twig's debug setting.
47
   */
48 9
  public function __construct(HtmlMin $htmlMin, bool $forceCompression = false)
49
  {
50 9
    $this->forceCompression = $forceCompression;
51 9
    $this->minifier = $htmlMin;
52 9
    $this->callable = [$this, 'compress'];
53 9
  }
54
55
  /**
56
   * @param Twig_Environment $twig
57
   * @param string           $html
58
   *
59
   * @return string
60
   */
61 9
  public function compress(Twig_Environment $twig, $html)
62
  {
63 9
    if ($this->isCompressionActive($twig)) {
64
65 6
      static $cache = null;
66 6
      if ($cache === null) {
67 1
        $cache = new Cache(null, null, false);
68
      }
69 6
      $cacheKey = 'HtmlMin::hash' . md5($html);
70
71
      if (
72 6
        $cache->getCacheIsReady() === true
73
        &&
74 6
        $cache->existsItem($cacheKey) === true
75
      ) {
76 5
        return $cache->getItem($cacheKey);
77
      }
78
79 1
      $html = $this->minifier->minify($html);
80
81 1
      if ($cache->getCacheIsReady() === true) {
82 1
        $cache->setItem($cacheKey, $html, 3600);
83
      }
84
85 1
      return $html;
86
    }
87
88 3
    return $html;
89
  }
90
91
  /** @noinspection PhpMissingParentCallCommonInspection */
92
  /**
93
   * @return array
94
   */
95 6
  public function getFilters(): array
96
  {
97
    return [
98 6
        new \Twig_SimpleFilter('htmlcompress', $this->callable, $this->options),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated with message: since Twig 2.7, use "Twig\TwigFilter" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
99
    ];
100
  }
101
102
  /** @noinspection PhpMissingParentCallCommonInspection */
103 6
  public function getFunctions(): array
104
  {
105
    return [
106 6
        new \Twig_SimpleFunction('htmlcompress', $this->callable, $this->options),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
107
    ];
108
  }
109
110
  /** @noinspection PhpMissingParentCallCommonInspection */
111 6
  public function getTokenParsers(): array
112
  {
113
    return [
114 6
        new MinifyHtmlTokenParser(),
115
    ];
116
  }
117
118
  /**
119
   * @param \Twig_Environment $twig
120
   *
121
   * @return bool
122
   */
123 9
  public function isCompressionActive(Twig_Environment $twig): bool
124
  {
125 9
    return $this->forceCompression
126
      ||
127 9
      !$twig->isDebug();
128
  }
129
}
130