TwigAssetRevExtension::assetRev()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 5
nop 2
crap 4
1
<?php
2
3
/**
4
 * This file is part of the m1\twig-asset-rev-extension library
5
 *
6
 * (c) m1 <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package     m1/twig-asset-rev-extension
12
 * @version     0.1.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/TwigAssetRevExtension/blob/master/LICENSE
16
 * @link        http://github.com/m1/TwigAssetRevExtension/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\TwigAssetRevExtension;
20
21
/**
22
 * The TwigAssetRevExtension class
23
 *
24
 * @since 0.1.0
25
 */
26
class TwigAssetRevExtension extends \Twig_Extension
27
{
28
    /**
29
     * The file extensions to check if there is a minified version
30
     *
31
     * @var array
32
     */
33
    private static $minify_exts = array('css', 'js');
34
35
    /**
36
     * The array of assets rev, raw_asset => rev_asset
37
     *
38
     * @var mixed
39
     */
40
    private $assets;
41
42
    /**
43
     * Whether to search for minified rev'd versions of the assets
44
     *
45
     * @var bool
46
     */
47
    private $minified;
48
49
    /**
50
     * The TwigAssetRevExtension constructor
51
     *
52
     * @param mixed $assets   The array of assets and rev'd assets
53
     * @param bool  $minified Whether to search for minified rev'd assets
54
     */
55 27
    public function __construct($assets, $minified = true)
56
    {
57 27
        if (!is_array($assets) && is_file($assets)) {
58 3
            $assets = json_decode(file_get_contents($assets), true);
59 3
        }
60
61 27
        $this->assets   = (is_array($assets)) ? $assets : array($assets);
62 27
        $this->minified = $minified;
63 27
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 12
    public function getFilters()
69
    {
70
        return array(
71 12
            new \Twig_SimpleFilter('asset_rev', array($this, 'assetRev'), array('needs_environment' => true)),
72 12
        );
73
    }
74
75
    /**
76
     * Gets the rev'd asset,
77
     *
78
     * @param \Twig_Environment $env   The twig environment
79
     * @param string            $asset The asset string to rev
80
     *
81
     * @return string The rev'd asset if available, else the original asset
82
     */
83 27
    public function assetRev(\Twig_Environment $env, $asset)
84
    {
85 27
        $pathinfo = pathinfo($asset);
86
87 27
        if (!isset($pathinfo['extension'])) {
88 6
            return $asset;
89
        }
90
91 21
        return ($this->minify($env, $pathinfo)) ?: ((isset($this->assets[$asset])) ? $this->assets[$asset] : $asset);
92
    }
93
94
    /**
95
     * Gets the minified asset
96
     *
97
     * @param \Twig_Environment $env      The twig environment
98
     * @param array             $pathinfo The pathinfo for the asset
99
     *
100
     * @return bool|string The minified rev'd asset if available, else false
101
     */
102 21
    public function minify($env, $pathinfo)
103
    {
104 21
        $min = sprintf(
105 21
            "%s/%s.min.%s",
106 21
            $pathinfo['dirname'],
107 21
            $pathinfo['filename'],
108 21
            $pathinfo['extension']
109 21
        );
110
111 21
        return (in_array($pathinfo['extension'], self::$minify_exts) &&
112 21
            isset($this->assets[$min]) &&
113 21
            $this->minified &&
114 12
            !$env->isDebug()
115 12
        )
116 21
            ? $this->assets[$min] : false;
117
    }
118
119
    /**
120
     * Returns the name of the extension.
121
     *
122
     * @return string The extension name
123
     */
124 27
    public function getName()
125
    {
126 27
        return 'asset_rev';
127
    }
128
}
129