summaryrefslogtreecommitdiffstats
path: root/admin/survey/minify/lib/Minify/Source.php
blob: d94bf58042eccc2d1f26bf07de34997461b22621 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
 * Class Minify_Source
 * @package Minify
 */

/**
 * A content source to be minified by Minify.
 *
 * This allows per-source minification options and the mixing of files with
 * content from other sources.
 *
 * @package Minify
 * @author Stephen Clay <steve@mrclay.org>
 */
class Minify_Source implements Minify_SourceInterface
{

    /**
     * @var int time of last modification
     */
    protected $lastModified;

    /**
     * @var callback minifier function specifically for this source.
     */
    protected $minifier;

    /**
     * @var array minification options specific to this source.
     */
    protected $minifyOptions = array();

    /**
     * @var string full path of file
     */
    protected $filepath;

    /**
     * @var string HTTP Content Type (Minify requires one of the constants Minify::TYPE_*)
     */
    protected $contentType;

    /**
     * @var string
     */
    protected $content;

    /**
     * @var callable
     */
    protected $getContentFunc;

    /**
     * @var string
     */
    protected $id;

    /**
     * Create a Minify_Source
     *
     * In the $spec array(), you can either provide a 'filepath' to an existing
     * file (existence will not be checked!) or give 'id' (unique string for
     * the content), 'content' (the string content) and 'lastModified'
     * (unixtime of last update).
     *
     * @param array $spec options
     */
    public function __construct($spec)
    {
        if (isset($spec['filepath'])) {
            $ext = pathinfo($spec['filepath'], PATHINFO_EXTENSION);
            switch ($ext) {
                case 'js': $this->contentType = Minify::TYPE_JS;
                    break;
                case 'less': // fallthrough
                case 'scss': // fallthrough
                case 'css': $this->contentType = Minify::TYPE_CSS;
                    break;
                case 'htm': // fallthrough
                case 'html': $this->contentType = Minify::TYPE_HTML;
                    break;
            }
            $this->filepath = $spec['filepath'];
            $this->id = $spec['filepath'];

            // TODO ideally not touch disk in constructor
            $this->lastModified = filemtime($spec['filepath']);

            if (!empty($spec['uploaderHoursBehind'])) {
                // offset for Windows uploaders with out of sync clocks
                $this->lastModified += round($spec['uploaderHoursBehind'] * 3600);
            }
        } elseif (isset($spec['id'])) {
            $this->id = 'id::' . $spec['id'];
            if (isset($spec['content'])) {
                $this->content = $spec['content'];
            } else {
                $this->getContentFunc = $spec['getContentFunc'];
            }
            $this->lastModified = isset($spec['lastModified']) ? $spec['lastModified'] : time();
        }
        if (isset($spec['contentType'])) {
            $this->contentType = $spec['contentType'];
        }
        if (isset($spec['minifier'])) {
            $this->setMinifier($spec['minifier']);
        }
        if (isset($spec['minifyOptions'])) {
            $this->minifyOptions = $spec['minifyOptions'];
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getLastModified()
    {
        return $this->lastModified;
    }

    /**
     * {@inheritdoc}
     */
    public function getMinifier()
    {
        return $this->minifier;
    }

    /**
     * {@inheritdoc}
     */
    public function setMinifier($minifier = null)
    {
        if ($minifier === '') {
            error_log(__METHOD__ . " cannot accept empty string. Use 'Minify::nullMinifier' or 'trim'.");
            $minifier = 'Minify::nullMinifier';
        }
        if ($minifier !== null && !is_callable($minifier, true)) {
            throw new InvalidArgumentException('minifier must be null or a valid callable');
        }
        $this->minifier = $minifier;
    }

    /**
     * {@inheritdoc}
     */
    public function getMinifierOptions()
    {
        return $this->minifyOptions;
    }

    /**
     * {@inheritdoc}
     */
    public function setMinifierOptions(array $options)
    {
        $this->minifyOptions = $options;
    }

    /**
     * {@inheritdoc}
     */
    public function getContentType()
    {
        return $this->contentType;
    }

    /**
     * {@inheritdoc}
     */
    public function getContent()
    {
        if (null === $this->filepath) {
            if (null === $this->content) {
                $content = call_user_func($this->getContentFunc, $this->id);
            } else {
                $content = $this->content;
            }
        } else {
            $content = file_get_contents($this->filepath);
        }

        // remove UTF-8 BOM if present
        if (strpos($content, "\xEF\xBB\xBF") === 0) {
            return substr($content, 3);
        }

        return $content;
    }

    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * {@inheritdoc}
     */
    public function getFilePath()
    {
        return $this->filepath;
    }

    /**
     * {@inheritdoc}
     */
    public function setupUriRewrites()
    {
        if ($this->filepath
            && !isset($this->minifyOptions['currentDir'])
            && !isset($this->minifyOptions['prependRelativePath'])) {
            $this->minifyOptions['currentDir'] = dirname($this->filepath);
        }
    }
}