1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Nikita Melnikov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package ext.mainscript
|
8
|
|
|
*/
|
9
|
|
|
|
10
|
|
|
/**
|
11
|
|
|
* Class ScriptHashHelper
|
12
|
|
|
*
|
13
|
|
|
* @property string $version
|
14
|
|
|
* @property string $hash
|
15
|
|
|
* @property string $isUpdated
|
16
|
|
|
*/
|
17
|
|
|
class ScriptHashHelper extends CComponent
|
18
|
|
|
{
|
19
|
|
|
/**
|
20
|
|
|
* Относительный путь к файлу с версией файлов
|
21
|
|
|
*
|
22
|
|
|
* @var string
|
23
|
|
|
*/
|
24
|
|
|
public $versionFile = '/js/.version';
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* Текущий хэш файлов
|
28
|
|
|
*
|
29
|
|
|
* @var string
|
30
|
|
|
*/
|
31
|
|
|
protected $hash;
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* Версия файлов
|
35
|
|
|
*
|
36
|
|
|
* @var string
|
37
|
|
|
*/
|
38
|
|
|
protected $version;
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* Массив с количеством файлов и их датой изменения
|
42
|
|
|
*
|
43
|
|
|
* @var array
|
44
|
|
|
*/
|
45
|
|
|
protected $data = array();
|
46
|
|
|
|
47
|
|
|
/**
|
48
|
|
|
* Флаг изменения контрольной суммы
|
49
|
|
|
*
|
50
|
|
|
* @var boolean
|
51
|
|
|
*/
|
52
|
|
|
protected $isUpdated;
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* @var ScriptHashHelper
|
56
|
|
|
*/
|
57
|
|
|
private static $instance = null;
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* @return ScriptHashHelper
|
61
|
|
|
*/
|
62
|
11 |
|
public static function getInstance()
|
63
|
|
|
{
|
64
|
11 |
|
if( empty(self::$instance) )
|
65
|
11 |
|
self::$instance = new ScriptHashHelper();
|
66
|
|
|
|
67
|
11 |
|
return self::$instance;
|
68
|
|
|
}
|
69
|
|
|
|
70
|
2 |
|
private function __construct()
|
71
|
|
|
{
|
72
|
2 |
|
$this->init();
|
73
|
2 |
|
}
|
74
|
|
|
|
75
|
|
|
private function __clone()
|
76
|
|
|
{
|
77
|
|
|
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* Инициализация объекта ScriptHashHelper
|
82
|
|
|
*/
|
83
|
2 |
|
public function init()
|
84
|
|
|
{
|
85
|
2 |
|
$this->setHash();
|
86
|
2 |
|
$this->setVersion();
|
87
|
2 |
|
$this->setUpdateFlag();
|
88
|
|
|
|
89
|
2 |
|
if( $this->isUpdated )
|
90
|
2 |
|
$this->saveVersion();
|
91
|
2 |
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* Создание контролльной суммы,
|
95
|
|
|
* основываясь на сериализации $this->data
|
96
|
|
|
* получение от неё контрольной суммы по md5
|
97
|
|
|
*
|
98
|
|
|
* @return string
|
99
|
|
|
*/
|
100
|
2 |
|
protected function setHash()
|
101
|
|
|
{
|
102
|
2 |
|
$this->data[] = count(ScriptsFileFinder::getInstance()->getFiles());
|
103
|
|
|
|
104
|
2 |
|
foreach( ScriptsFileFinder::getInstance()->getFiles() as $file )
|
105
|
|
|
{
|
106
|
1 |
|
$this->data[] = filemtime($file);
|
107
|
2 |
|
}
|
108
|
|
|
|
109
|
2 |
|
$serialized = serialize($this->data);
|
110
|
2 |
|
return $this->hash = md5($serialized);
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* Инициализация флага обновления контрольной суммы
|
115
|
|
|
*/
|
116
|
2 |
|
protected function setUpdateFlag()
|
117
|
|
|
{
|
118
|
2 |
|
$this->isUpdated = $this->hash !== $this->version;
|
119
|
2 |
|
}
|
120
|
|
|
|
121
|
|
|
/**
|
122
|
|
|
* Получение версии (предыдущей контрольной суммы)
|
123
|
|
|
*
|
124
|
|
|
* @return string
|
125
|
|
|
*/
|
126
|
2 |
|
protected function setVersion()
|
127
|
|
|
{
|
128
|
2 |
|
$path = ScriptsFileFinder::getInstance()->getRoot().$this->versionFile;
|
129
|
|
|
|
130
|
2 |
|
if( file_exists($path) )
|
131
|
2 |
|
$this->version = file_get_contents($path);
|
132
|
|
|
else
|
133
|
1 |
|
$this->version = 'null';
|
134
|
|
|
|
135
|
2 |
|
return $this->version;
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
/**
|
139
|
|
|
* Сохранение текущей контрольной суммы
|
140
|
|
|
* (обновление версии)
|
141
|
|
|
*
|
142
|
|
|
* @throws Exception
|
143
|
|
|
*/
|
144
|
1 |
|
protected function saveVersion()
|
145
|
|
|
{
|
146
|
1 |
|
if( empty($this->hash) )
|
147
|
1 |
|
throw new Exception('hash is empty');
|
148
|
|
|
|
149
|
1 |
|
$path = ScriptsFileFinder::getInstance()->getRoot() . $this->versionFile;
|
150
|
|
|
|
151
|
1 |
|
if( file_exists($path) )
|
152
|
1 |
|
file_put_contents($path, $this->hash);
|
153
|
|
|
else
|
154
|
|
|
{
|
155
|
1 |
|
$versionFile = fopen($path, 'w');
|
156
|
1 |
|
fwrite($versionFile, $this->hash);
|
157
|
1 |
|
fclose($versionFile);
|
158
|
1 |
|
chmod($path, 0664);
|
159
|
|
|
}
|
160
|
1 |
|
}
|
161
|
|
|
|
162
|
4 |
|
protected function getHash()
|
163
|
|
|
{
|
164
|
4 |
|
return $this->hash;
|
165
|
|
|
}
|
166
|
|
|
|
167
|
1 |
|
protected function getVersion()
|
168
|
|
|
{
|
169
|
1 |
|
return $this->version;
|
170
|
|
|
}
|
171
|
|
|
|
172
|
8 |
|
protected function getIsUpdated()
|
173
|
|
|
{
|
174
|
8 |
|
return $this->isUpdated;
|
175
|
|
|
}
|
176
|
|
|
} |