|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Service_WindowsAzure |
|
17 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id$ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @category Zend |
|
24 * @package Zend_Service_WindowsAzure_CommandLine |
|
25 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
26 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
27 */ |
|
28 abstract class Zend_Service_WindowsAzure_CommandLine_PackageScaffolder_PackageScaffolderAbstract |
|
29 { |
|
30 /** |
|
31 * Invokes the scaffolder. |
|
32 * |
|
33 * @param Phar $phar Phar archive containing the current scaffolder. |
|
34 * @param string $root Path Root path. |
|
35 * @param array $options Options array (key/value). |
|
36 */ |
|
37 abstract public function invoke(Phar $phar, $rootPath, $options = array()); |
|
38 |
|
39 /** |
|
40 * Writes output to STDERR, followed by a newline (optional) |
|
41 * |
|
42 * @param string $message |
|
43 * @param string $newLine |
|
44 */ |
|
45 protected function log($message, $newLine = true) |
|
46 { |
|
47 if (error_reporting() === 0) { |
|
48 return; |
|
49 } |
|
50 file_put_contents('php://stderr', $message . ($newLine ? "\r\n" : '')); |
|
51 } |
|
52 |
|
53 /** |
|
54 * Extract resources to a file system path |
|
55 * |
|
56 * @param Phar $phar Phar archive. |
|
57 * @param string $path Output path root. |
|
58 */ |
|
59 protected function extractResources(Phar $phar, $path) |
|
60 { |
|
61 $this->deleteDirectory($path); |
|
62 $phar->extractTo($path); |
|
63 @unlink($path . '/index.php'); |
|
64 @unlink($path . '/build.bat'); |
|
65 $this->copyDirectory($path . '/resources', $path, false); |
|
66 $this->deleteDirectory($path . '/resources'); |
|
67 } |
|
68 |
|
69 /** |
|
70 * Apply file transforms. |
|
71 * |
|
72 * @param string $rootPath Root path. |
|
73 * @param array $values Key/value array. |
|
74 */ |
|
75 protected function applyTransforms($rootPath, $values) |
|
76 { |
|
77 if (is_null($rootPath) || !is_string($rootPath) || empty($rootPath)) { |
|
78 throw new InvalidArgumentException("Undefined \"rootPath\""); |
|
79 } |
|
80 |
|
81 if (is_dir($rootPath)) { |
|
82 $d = dir($rootPath); |
|
83 while ( false !== ( $entry = $d->read() ) ) { |
|
84 if ( $entry == '.' || $entry == '..' ) { |
|
85 continue; |
|
86 } |
|
87 $entry = $rootPath . '/' . $entry; |
|
88 |
|
89 $this->applyTransforms($entry, $values); |
|
90 } |
|
91 $d->close(); |
|
92 } else { |
|
93 $contents = file_get_contents($rootPath); |
|
94 foreach ($values as $key => $value) { |
|
95 $contents = str_replace('$' . $key . '$', $value, $contents); |
|
96 } |
|
97 file_put_contents($rootPath, $contents); |
|
98 } |
|
99 |
|
100 return true; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Create directory |
|
105 * |
|
106 * @param string $path Path of directory to create. |
|
107 * @param boolean $abortIfExists Abort if directory exists. |
|
108 * @param boolean $recursive Create parent directories if not exist. |
|
109 * |
|
110 * @return boolean |
|
111 */ |
|
112 protected function createDirectory($path, $abortIfExists = true, $recursive = true) { |
|
113 if (is_null($path) || !is_string($path) || empty($path)) { |
|
114 throw new InvalidArgumentException ("Undefined \"path\"" ); |
|
115 } |
|
116 |
|
117 if (is_dir($path) && $abortIfExists) { |
|
118 return false; |
|
119 } |
|
120 |
|
121 if (is_dir($path) ) { |
|
122 @chmod($path, '0777'); |
|
123 if (!self::deleteDirectory($path) ) { |
|
124 throw new RuntimeException("Failed to delete \"{$path}\"."); |
|
125 } |
|
126 } |
|
127 |
|
128 if (!mkdir($path, '0777', $recursive) || !is_dir($path)) { |
|
129 throw new RuntimeException( "Failed to create directory \"{$path}\"." ); |
|
130 } |
|
131 |
|
132 return true; |
|
133 } |
|
134 |
|
135 /** |
|
136 * Fully copy a source directory to a target directory. |
|
137 * |
|
138 * @param string $sourcePath Source directory |
|
139 * @param string $destinationPath Target directory |
|
140 * @param boolean $abortIfExists Query re-creating target directory if exists |
|
141 * @param octal $mode Changes access mode |
|
142 * |
|
143 * @return boolean |
|
144 */ |
|
145 protected function copyDirectory($sourcePath, $destinationPath, $abortIfExists = true, $mode = '0777') { |
|
146 if (is_null($sourcePath) || !is_string($sourcePath) || empty($sourcePath)) { |
|
147 throw new InvalidArgumentException("Undefined \"sourcePath\""); |
|
148 } |
|
149 |
|
150 if (is_null($destinationPath) || !is_string($destinationPath) || empty($destinationPath)) { |
|
151 throw new InvalidArgumentException("Undefined \"destinationPath\""); |
|
152 } |
|
153 |
|
154 if (is_dir($destinationPath) && $abortIfExists) { |
|
155 return false; |
|
156 } |
|
157 |
|
158 if (is_dir($sourcePath)) { |
|
159 if (!is_dir($destinationPath) && !mkdir($destinationPath, $mode)) { |
|
160 throw new RuntimeException("Failed to create target directory \"{$destinationPath}\"" ); |
|
161 } |
|
162 $d = dir($sourcePath); |
|
163 while ( false !== ( $entry = $d->read() ) ) { |
|
164 if ( $entry == '.' || $entry == '..' ) { |
|
165 continue; |
|
166 } |
|
167 $strSourceEntry = $sourcePath . '/' . $entry; |
|
168 $strTargetEntry = $destinationPath . '/' . $entry; |
|
169 if (is_dir($strSourceEntry) ) { |
|
170 $this->copyDirectory( |
|
171 $strSourceEntry, |
|
172 $strTargetEntry, |
|
173 false, |
|
174 $mode |
|
175 ); |
|
176 continue; |
|
177 } |
|
178 if (!copy($strSourceEntry, $strTargetEntry) ) { |
|
179 throw new RuntimeException ( |
|
180 "Failed to copy" |
|
181 . " file \"{$strSourceEntry}\"" |
|
182 . " to \"{$strTargetEntry}\"" |
|
183 ); |
|
184 } |
|
185 } |
|
186 $d->close(); |
|
187 } else { |
|
188 if (!copy($sourcePath, $destinationPath)) { |
|
189 throw new RuntimeException ( |
|
190 "Failed to copy" |
|
191 . " file \"{$sourcePath}\"" |
|
192 . " to \"{$destinationPath}\"" |
|
193 |
|
194 ); |
|
195 } |
|
196 } |
|
197 |
|
198 return true; |
|
199 } |
|
200 |
|
201 /** |
|
202 * Delete directory and all of its contents; |
|
203 * |
|
204 * @param string $path Directory path |
|
205 * @return boolean |
|
206 */ |
|
207 protected function deleteDirectory($path) |
|
208 { |
|
209 if (is_null($path) || !is_string($path) || empty($path)) { |
|
210 throw new InvalidArgumentException( "Undefined \"path\"" ); |
|
211 } |
|
212 |
|
213 $handleDir = false; |
|
214 if (is_dir($path) ) { |
|
215 $handleDir = @opendir($path); |
|
216 } |
|
217 if (!$handleDir) { |
|
218 return false; |
|
219 } |
|
220 @chmod($path, 0777); |
|
221 while ($file = readdir($handleDir)) { |
|
222 if ($file == '.' || $file == '..') { |
|
223 continue; |
|
224 } |
|
225 |
|
226 $fsEntity = $path . "/" . $file; |
|
227 |
|
228 if (is_dir($fsEntity)) { |
|
229 $this->deleteDirectory($fsEntity); |
|
230 continue; |
|
231 } |
|
232 |
|
233 if (is_file($fsEntity)) { |
|
234 @unlink($fsEntity); |
|
235 continue; |
|
236 } |
|
237 |
|
238 throw new LogicException ( |
|
239 "Unexpected file type: \"{$fsEntity}\"" |
|
240 ); |
|
241 } |
|
242 |
|
243 @chmod($path, 0777); |
|
244 closedir($handleDir); |
|
245 @rmdir($path); |
|
246 |
|
247 return true; |
|
248 } |
|
249 } |