-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersionCSSURLs.php
75 lines (65 loc) · 2.42 KB
/
VersionCSSURLs.php
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
<?php
require_once 'phing/filters/BaseFilterReader.php';
include_once 'phing/filters/ChainableReader.php';
include_once 'phing/types/RegularExpression.php';
/**
* Finds urls in CSS files and appends them with a query var representing the last modification time.
*
* @author Dan Bissonnet
*/
class VersionCSSURLs extends BaseFilterReader implements ChainableReader {
private $filepath;
function read($len = null) {
$buffer = $this->in->read($len);
$this->originaldir = dirname($this->in->getResource());
if($buffer === -1) {
return -1;
}
try {
$this->log('Versioning urls in ' . $this->in->getResource());
$token = 'ver=';
$buffer = preg_replace_callback('|url\(([^\)]+\?' . preg_quote($token) . '[^\)]+)\)|', array($this, 'add_version'), $buffer);
} catch (Exception $e) {
// perhaps mismatch in params (e.g. no replace or pattern specified)
$this->log("Error performing regexp replace: " . $e->getMessage(), Project::MSG_WARN);
}
return $buffer;
}
/**
* Callback for preg_replace_callback(). Appends the version string.
*
* Retrieves the filename and checks whether it exists and then appends
* the ctime as a query var.
*
* @param Array $match
*/
private function add_version($match) {
// Strip off any hash or query vars.
// TODO: reappend the hash and query vars.
$originalPath = preg_replace('|([\?#].*)$|', '', trim($match[1],"' "));
$path = $this->originaldir . '/' . $originalPath;
if(file_exists($path)){
$attribute = preg_replace("|url\(([\'\"]*)([^\)\'\"]+)([\'\"]*)\)|", 'url($1' . $originalPath . '?ver=' . filectime($path) . '$3)' , $match[0]);
$this->log($attribute);
return $attribute;
} else {
throw new BuildException("Resource not found: " . $path, 1);
}
return $match[0];
}
/**
* Creates a new VersionCSSURLs filter using the passed in
* Reader for instantiation.
*
* @param Reader $reader A Reader object providing the underlying stream.
* Must not be <code>null</code>.
*
* @return VersionCSSURLs A new filter based on this configuration, but filtering
* the specified reader
*/
function chain(Reader $reader) {
$newFilter = new VersionCSSURLs($reader);
$newFilter->setProject($this->getProject());
return $newFilter;
}
}