1: <?php
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: class Horde_Release_Sentinel
33: {
34:
35: const CHANGES = '/docs/CHANGES';
36:
37:
38: const APPLICATION = '/lib/Application.php';
39:
40:
41: const BUNDLE = '/lib/Bundle.php';
42:
43: 44: 45: 46: 47:
48: private $_component;
49:
50: 51: 52: 53: 54:
55: public function __construct($component)
56: {
57: $this->_component = $component;
58: }
59:
60: 61: 62: 63: 64: 65: 66:
67: public function updateChanges($version)
68: {
69: if ($changes = $this->changesFileExists()) {
70: $tmp = Horde_Util::getTempFile();
71:
72: $oldfp = fopen($changes, 'r');
73: $newfp = fopen($tmp, 'w');
74: $version = 'v' . $version;
75: fwrite(
76: $newfp,
77: str_repeat('-', strlen($version)) . "\n$version\n" .
78: str_repeat('-', strlen($version)) . "\n\n\n\n"
79: );
80: while ($line = fgets($oldfp)) {
81: fwrite($newfp, $line);
82: }
83: fclose($oldfp);
84: fclose($newfp);
85:
86: system("mv -f $tmp $changes");
87: }
88:
89: }
90:
91: 92: 93: 94: 95: 96: 97:
98: public function replaceChanges($version)
99: {
100: if ($changes = $this->changesFileExists()) {
101: $tmp = Horde_Util::getTempFile();
102:
103: $oldfp = fopen($changes, 'r');
104: $newfp = fopen($tmp, 'w');
105: $version = 'v' . $version;
106: $counter = 0;
107: while ($line = fgets($oldfp)) {
108: if ($counter < 2) {
109: $counter++;
110: } else if ($counter == 2) {
111: fwrite(
112: $newfp,
113: str_repeat('-', strlen($version)) . "\n$version\n" .
114: str_repeat('-', strlen($version)) . "\n"
115: );
116: $counter++;
117: } else {
118: fwrite($newfp, $line);
119: }
120: }
121: fclose($oldfp);
122: fclose($newfp);
123:
124: system("mv -f $tmp $changes");
125: }
126:
127: }
128:
129: 130: 131: 132: 133: 134: 135:
136: public function updateApplication($version)
137: {
138: if ($application = $this->applicationFileExists()) {
139: $tmp = Horde_Util::getTempFile();
140:
141: $oldmode = fileperms($application);
142: $oldfp = fopen($application, 'r');
143: $newfp = fopen($tmp, 'w');
144: while ($line = fgets($oldfp)) {
145: $line = preg_replace(
146: '/public \$version = \'[^\']*\';/',
147: 'public \$version = \'' . $version . '\';',
148: $line
149: );
150: fwrite($newfp, $line);
151: }
152: fclose($oldfp);
153: fclose($newfp);
154: chmod($tmp, $oldmode);
155:
156: system("mv -f $tmp $application");
157: } elseif ($bundle = $this->bundleFileExists()) {
158: $tmp = Horde_Util::getTempFile();
159:
160: $oldmode = fileperms($bundle);
161: $oldfp = fopen($bundle, 'r');
162: $newfp = fopen($tmp, 'w');
163: while ($line = fgets($oldfp)) {
164: $line = preg_replace(
165: '/const VERSION = \'[^\']*\';/',
166: 'const VERSION = \'' . $version . '\';',
167: $line
168: );
169: fwrite($newfp, $line);
170: }
171: fclose($oldfp);
172: fclose($newfp);
173: chmod($tmp, $oldmode);
174:
175: system("mv -f $tmp $bundle");
176: }
177: }
178:
179: 180: 181: 182: 183: 184:
185: public function changesFileExists()
186: {
187: $changes = $this->_component . self::CHANGES;
188: if (file_exists($changes)) {
189: return $changes;
190: }
191: return false;
192: }
193:
194: 195: 196: 197: 198: 199:
200: public function applicationFileExists()
201: {
202: $application = $this->_component . self::APPLICATION;
203: if (file_exists($application)) {
204: return $application;
205: }
206: return false;
207: }
208:
209: 210: 211: 212: 213: 214:
215: public function bundleFileExists()
216: {
217: $bundle = $this->_component . self::BUNDLE;
218: if (file_exists($bundle)) {
219: return $bundle;
220: }
221: return false;
222: }
223:
224: 225: 226: 227: 228:
229: public function getVersion()
230: {
231: if ($application = $this->applicationFileExists()) {
232: $oldfp = fopen($application, 'r');
233: while ($line = fgets($oldfp)) {
234: if (preg_match('/public \$version = \'([^\']*)\';/', $line, $match)) {
235: return $match[1];
236: }
237: }
238: fclose($oldfp);
239: }
240: if ($bundle = $this->bundleFileExists()) {
241: $oldfp = fopen($bundle, 'r');
242: while ($line = fgets($oldfp)) {
243: if (preg_match('/const VERSION = \'([^\']*)\';/', $line, $match)) {
244: return $match[1];
245: }
246: }
247: fclose($oldfp);
248: }
249: }
250: }
251: