Overview

Packages

  • Release

Classes

  • Horde_Release
  • Horde_Release_Freecode
  • Horde_Release_MailingList
  • Horde_Release_Sentinel
  • Horde_Release_Whups
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Update the sentinel in CHANGES.
  4:  *
  5:  * PHP version 5
  6:  *
  7:  * @category Horde
  8:  * @package  Release
  9:  * @author   Mike Hardy
 10:  * @author   Jan Schneider <jan@horde.org>
 11:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 12:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 13:  * @link     http://www.horde.org/libraries/Horde_Release
 14:  */
 15: 
 16: /**
 17:  * Update the sentinel in CHANGES.
 18:  *
 19:  * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
 20:  *
 21:  * See the enclosed file COPYING for license information (LGPL). If you
 22:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 23:  *
 24:  * @category Horde
 25:  * @package  Release
 26:  * @author   Mike Hardy
 27:  * @author   Jan Schneider <jan@horde.org>
 28:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 29:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 30:  * @link     http://www.horde.org/libraries/Horde_Release
 31:  */
 32: class Horde_Release_Sentinel
 33: {
 34:     /** Path to the CHANGES file. */
 35:     const CHANGES = '/docs/CHANGES';
 36: 
 37:     /** Path to the Application.php file. */
 38:     const APPLICATION = '/lib/Application.php';
 39: 
 40:     /** Path to the Bundle.php file. */
 41:     const BUNDLE = '/lib/Bundle.php';
 42: 
 43:     /**
 44:      * Base component path.
 45:      *
 46:      * @var string
 47:      */
 48:     private $_component;
 49: 
 50:     /**
 51:      * Constructor.
 52:      *
 53:      * @param string $component Base component path.
 54:      */
 55:     public function __construct($component)
 56:     {
 57:         $this->_component = $component;
 58:     }
 59: 
 60:     /**
 61:      * Update the CHANGES file in case it exists.
 62:      *
 63:      * @param string $version Version string that should be added.
 64:      *
 65:      * @return NULL
 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:      * Replace the current sentinel in the CHANGES file in case it exists.
 93:      *
 94:      * @param string $version Version string that should be added.
 95:      *
 96:      * @return NULL
 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:      * Update the Application.php or Bundle.php file in case it exists.
131:      *
132:      * @param string $new_version Version string that should be added.
133:      *
134:      * @return NULL
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:      * Indicates if there is a CHANGES file for this component.
181:      *
182:      * @return string|boolean The path to the CHANGES file if it exists, false
183:      *                        otherwise.
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:      * Indicates if there is a Application.php file for this component.
196:      *
197:      * @return string|boolean The path to the Application.php file if it exists,
198:      *                        false otherwise.
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:      * Indicates if there is a Bundle.php file for this component.
211:      *
212:      * @return string|boolean The path to the Bundle.php file if it exists,
213:      *                        false otherwise.
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:      * Returns the current version from Application.php or Bundle.php.
226:      *
227:      * @return string Version string.
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: 
API documentation generated by ApiGen