/
Abstract.php
299 lines (266 loc) · 7.07 KB
/
Abstract.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Zend Framework Extension file rotator
*
* @category ZendEx
* @package ZendEx_File_Rotator
* @copyright Copyright (c) 2011 hssh.
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Abstract.php 112 2011-08-21 12:07:20Z hssh $
*/
require_once "Zend/Config.php";
require_once "Zend/Filter.php";
require_once "ZendEx/File/Rotator/Interface.php";
require_once "ZendEx/Text/Formatter.php";
/**
* @category ZendEx
* @package ZendEx_File_Rotator
*/
abstract class ZendEx_File_Rotator_Abstract implements ZendEx_File_Rotator_Interface {
/**
* Url of file
*
* @var string
*/
protected $_url = null;
/**
* Filename format for rotation
*
* @see ZendEx_Text_Fromatter
* @var string
*/
protected $_format = "%filename%%.{extension}%%.{suffix}%";
/**
* Cycle length of rotations. Zero means unlimited cycle length
*
* @var string
*/
protected $_cycle = 0;
/**
* Sets rotator options
*
* Accepts the following option keys:
* 'format' => string, filename format for rotation
*
* @param string $url
* @param array|Zend_Config $options
* @return void
*/
public function __construct($url, $config) {
if (empty($url)) {
require_once "Zend/Exception.php";
throw new Zend_Exception("Url is empty.");
}
$config = array_merge(array("format" => $this->_format, "cycle" => $this->_cycle),
self::_parseConfig($config));
$this->setUrl($url)
->setFormat($config["format"])
->setCycle($config["cycle"]);
}
/**
* Sets file url
*
* @param string $url
* @return ZendEx_File_Rotator_Abstract
*/
public function setUrl($url) {
$this->_url = $url;
return $this;
}
/**
* Returns file url
*
* @return string
*/
public function getUrl() {
return $this->_url;
}
/**
* Sets filename format for rotation
*
* @param string $format
* @return ZendEx_File_Rotator_Abstract
*/
public function setFormat($format) {
$this->_format = $format;
return $this;
}
/**
* Returns filename format for rotation
*
* @return string
*/
public function getFormat() {
return $this->_format;
}
/**
* Sets cycle of rotation
*
* @param string $format
* @return ZendEx_File_Rotator_Abstract
*/
public function setCycle($cycle) {
$this->_cycle = $cycle;
return $this;
}
/**
* Returns cycle of rotation
*
* @return string
*/
public function getCycle() {
return $this->_cycle;
}
/**
* Returns filename for rotation
*
* @return string
*/
public function getRotatingFilename() {
$formatter = $this->_getFormatter(array("suffix" => $this->_getSuffix()));
return $this->_getDirname() . DIRECTORY_SEPARATOR . $formatter;
}
/**
* Rotates file
*
* @return ZendEx_File_Rotator_Abstract
*/
public function rotate() {
if ($this->_isNeedRotation()) {
$this->_cycle();
rename($this->getUrl(), $this->getRotatingFilename());
$this->_unlinkCycleExceededFiles();
}
return $this;
}
/**
* Returns dirname of url
*
* @return string
*/
protected function _getDirname() {
return Zend_Filter::filterStatic($this->getUrl(), "Dir");
}
/**
* Returns basename of url
*
* @return string
*/
protected function _getBasename() {
return Zend_Filter::filterStatic($this->getUrl(), "BaseName");
}
/**
* Returns filename of url
*
* @return string
*/
protected function _getFilename() {
return pathinfo($this->_getBasename(), PATHINFO_FILENAME);
}
/**
* Returns extension of url
*
* @return string
*/
protected function _getExtension() {
return pathinfo($this->_getBasename(), PATHINFO_EXTENSION);
}
/**
* Returns initialized formatter
*
* @param array $replacements Replacements for formatter tags
* @return string
*/
protected function _getFormatter($replacements = array()) {
$replacements =
array_merge(array("basename" => $this->_getBasename(),
"filename" => $this->_getFilename(),
"extension" => $this->_getExtension()),
$replacements);
return new ZendEx_Text_Formatter($this->getFormat(), $replacements);
}
/**
* Returns suffix
*
* @return string
*/
abstract protected function _getSuffix();
/**
* Returns suffix pattern
*
* @return string
*/
abstract protected function _getSuffixPattern();
/**
* Checks necessity of rotation
*
* @return bool
*/
abstract protected function _isNeedRotation();
/**
* Cycle rotated files
*
* @return Zend_File_Rotator
*/
protected function _cycle() {
return $this;
}
/**
* Unlink files which exceed rotation cycle
*
* @return Zend_File_Rotator
*/
protected function _unlinkCycleExceededFiles() {
// Unlimited rotation if $_cycle is zero
if ($this->getCycle() == 0) {
return $this;
}
// Unlink files which exceed rotation cycle
$files = $this->_getRotatedFiles();
for ($i = $this->getCycle() - 1; $i < count($files); $i++) {
unlink($files[$i]);
}
return $this;
}
/**
* Get rotated files
*
* @return array
*/
protected function _getRotatedFiles() {
// Get initialized formatter
$pattern = $this->_getFormatter();
// Glob files which match with any suffix pattern.
$pattern->suffix = "*";
$files = glob($this->_getDirname() . DIRECTORY_SEPARATOR . $pattern);
// Exclude files which don't match with the rotator's suffix pattern
$pattern->suffix = $this->_getSuffixPattern();
foreach ($files as $key => $filename) {
if (!preg_match("/$pattern/", basename($filename), $matches)) {
unset($files[$key]);
}
}
// Natural sort & Reindexing
natsort($files);
return array_values($files);
}
/**
* Validate and optionally convert the config to array
*
* @param array|Zend_Config $config Zend_Config or Array
* @return array
* @throws Zend_Log_Exception
*/
static protected function _parseConfig($config) {
if ($config instanceof Zend_Config) {
$config = $config->toArray();
}
if (!is_array($config)) {
require_once 'Zend/Log/Exception.php';
throw new Zend_Log_Exception(
'Configuration must be an array or instance of Zend_Config'
);
}
return $config;
}
}