1
|
|
|
<?php |
2
|
|
|
namespace Germania\AssetTimestamper; |
3
|
|
|
|
4
|
|
|
class AssetTimestamper |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
public $base_path; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Timestamp format to use. |
15
|
|
|
* |
16
|
|
|
* @see http://php.net/manual/de/function.date.php |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $format = 'YmdHis'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Separator sign that preceeds the timestamp |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $separator = '.'; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $base_path Optional: Base path, default: Current work dir. |
|
|
|
|
30
|
|
|
* @uses $base_path |
31
|
|
|
*/ |
32
|
55 |
|
public function __construct( $base_path = null ) |
33
|
|
|
{ |
34
|
55 |
|
$this->base_path = $base_path ?: getcwd(); |
35
|
55 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
* @uses $base_path |
40
|
|
|
*/ |
41
|
15 |
|
public function getBasePath() |
42
|
|
|
{ |
43
|
15 |
|
return $this->base_path; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $asset Asset URL |
48
|
|
|
* @return string Timestamped Asset URL |
49
|
|
|
* @uses $base_path |
50
|
|
|
*/ |
51
|
40 |
|
public function __invoke( $asset ) |
52
|
|
|
{ |
53
|
|
|
// Parse asset URL |
54
|
40 |
|
$asset_parts = parse_url( $asset ); |
55
|
|
|
|
56
|
|
|
// Exclude if asset seems to come from different location, |
57
|
|
|
// i.e. if it has defined hostname |
58
|
40 |
|
if (is_array($asset_parts) |
59
|
40 |
|
&& !empty($asset_parts['host'])) { |
60
|
10 |
|
return $asset; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Prepend DIRECTORY_SEPARATOR if missing |
64
|
30 |
|
$work_asset = (substr($asset, 0, strlen(\DIRECTORY_SEPARATOR)) === \DIRECTORY_SEPARATOR) |
65
|
18 |
|
? $asset |
66
|
30 |
|
: \DIRECTORY_SEPARATOR . $asset; |
67
|
|
|
|
68
|
|
|
// Glue base path and asset; throw if file not existant |
69
|
30 |
|
if (!$real_file = realpath($this->base_path . $work_asset)) { |
70
|
10 |
|
return $asset; |
71
|
|
|
} |
72
|
|
|
|
73
|
20 |
|
$result = $this->buildTimestampedPath($asset, $real_file); |
74
|
20 |
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $asset Asset path |
80
|
|
|
* @param string $real_file Real file path |
81
|
|
|
* @return string The timestamped asset file name |
82
|
|
|
*/ |
83
|
20 |
|
public function buildTimestampedPath($asset, $real_file) |
84
|
|
|
{ |
85
|
|
|
// Build result |
86
|
20 |
|
$timestamp = date( $this->format, filemtime( $real_file )); |
87
|
|
|
|
88
|
20 |
|
$path_info = pathinfo( $asset ); |
89
|
|
|
|
90
|
20 |
|
$result = str_replace( |
91
|
20 |
|
$path_info['basename'], |
92
|
20 |
|
join( "", [ |
93
|
20 |
|
$path_info['filename'], |
94
|
20 |
|
$this->separator, |
95
|
20 |
|
$timestamp, |
96
|
20 |
|
".", |
97
|
20 |
|
$path_info['extension'] |
98
|
4 |
|
]), |
99
|
12 |
|
$asset |
100
|
4 |
|
); |
101
|
|
|
|
102
|
20 |
|
return $result; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.