This repository was archived by the owner on Nov 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAssetsVersionManager.php
178 lines (153 loc) · 5.99 KB
/
AssetsVersionManager.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
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
<?php
namespace Kachkaev\AssetsVersionBundle;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
/**
* Works with parameters.yml at a low level, extracts and writes back value of assets_version
* Because parsing is done using regular expressions insead of YamlParser, formatting is preserved when writing value to file.
*
* @author Alexander Kachkaev <alexander@kachkaev.ru>
*
*/
class AssetsVersionManager
{
static protected $versionParameterMask = '[a-zA-Z_][a-zA-Z0-9_-]*';
static protected $versionValueMask = '[a-zA-Z0-9_\.-]*';
protected $filePath;
protected $parameterName;
protected $fileContents;
protected $versionValue;
protected $versionStartPos;
public function __construct($filePath, $parameterName)
{
$this->filePath = $filePath;
if (!preg_match('/^' . static::$versionParameterMask . '$/', $parameterName)) {
throw new \InvalidArgumentException(sprintf(
'Wrong value for parameter name %s - it should consist only of characters, numbers and dash or underscore',
var_export($parameterName, true)
));
}
$this->parameterName = $parameterName;
$this->readFile();
}
/**
* Gets the value of assets version found in parameters file
*
* @param boolean $rereadFile - if true, re-reads the file first
*/
public function getVersion($rereadFile = false)
{
if ($rereadFile) {
$this->readFile();
}
return $this->versionValue;
}
/**
* Sets a new value for the assets version parameter
*
* Assets version must consist only of letters, numbers and the following characters: .-_
*/
public function setVersion($value)
{
if (!is_string($value) && !is_numeric($value)) {
throw new \InvalidArgumentException(sprintf(
'Wrong value for assets version: %s - it must be string or numeric',
var_export($value, true)
));
}
if (!preg_match('/^' . static::$versionValueMask . '$/', $value)) {
throw new \InvalidArgumentException(sprintf(
'Wrong value for assets version: %s - it must be empty or consist only of letters, numbers and the following characters: .-_',
var_export($value, true)
));
}
$this->fileContents = substr_replace(
$this->fileContents,
$value,
$this->versionStartPos,
strlen($this->versionValue)
);
$this->versionValue = $value;
try {
file_put_contents($this->filePath, $this->fileContents);
} catch (\Exception $e) {
throw new FileException(sprintf(
'Could not write to write "%s"; make sure it exists and you have enough permissions',
$this->filePath
));
}
}
/**
* Increments value for assets version found in parameters file
* Only works when current value is integer or has integer ending, e.g. v42
* If delta is given, incrementing is done by that value.
*
* @param int $delta number to increment (default is 1)
* @param boolean $rereadFile if true, re-reads the file first
*/
public function incrementVersion($delta = 1, $rereadFile = false)
{
if ($rereadFile) {
$this->readFile();
}
if (!is_numeric($delta) || round($delta) != $delta) {
throw new \InvalidArgumentException(sprintf(
'Delta must be integer, %s given',
var_export($delta, true)
));
}
preg_match('/^(.*)(\d+)$/U', $this->versionValue, $matches);
if (!array_key_exists(2, $matches)) {
throw new \UnexpectedValueException(sprintf(
'Could not increment assets version %s - it should be integer or at least have integer ending',
var_export($this->versionValue, true)
));
}
$newValue = max(0, $matches[2] + $delta) . '';
// Preserve leading zeros
if ($matches[2][0] == '0') {
$newValue = str_pad($newValue, strlen($matches[2]), '0', STR_PAD_LEFT);
}
$this->setVersion($matches[1] . $newValue);
}
/**
* Reads and parses file with parameters
*
* @throws InvalidConfigurationException
* @throws FileException
* @throws \Exception
*/
protected function readFile()
{
$fileExtension = pathinfo($this->filePath, PATHINFO_EXTENSION);
if ($fileExtension != 'yml' && $fileExtension != 'yaml')
throw new InvalidConfigurationException(sprintf(
'Could not use "%s" - only yml files are supported by AssetsVersionManager',
var_export($this->versionValue, true)
));
try {
$this->fileContents = file_get_contents($this->filePath);
} catch (\Exception $e) {
throw new FileException(sprintf(
'Could not read file "%s"; make sure it exists and you have enough permissions',
$this->filePath
));
}
// Find a row with the parameter
preg_match(
'/(\s+' . $this->parameterName . '\:[^\S\n]*)(' . static::$versionValueMask . ')\s*(\n|#)/',
$this->fileContents . "\n",
$matches
);
if (array_key_exists(2, $matches)) {
$this->versionValue = $matches[2];
$this->versionStartPos = strpos($this->fileContents."\n", $matches[0]) + strlen($matches[1]);
return;
}
throw new \Exception(sprintf(
'Could not find definition of parameter "%s"; make sure it exists in "%s"',
$this->parameterName,
$this->filePath
));
}
}