PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Service/WindowsAzure/CommandLine/PackageScaffolder/PackageScaffolderAbstract.php

https://bitbucket.org/simukti/zf1
PHP | 249 lines | 148 code | 26 blank | 75 comment | 52 complexity | 72dcecc7d6c9d20bed26796697708ea6 MD5 | raw file
  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. * @category Zend
  23. * @package Zend_Service_WindowsAzure_CommandLine
  24. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. abstract class Zend_Service_WindowsAzure_CommandLine_PackageScaffolder_PackageScaffolderAbstract
  28. {
  29. /**
  30. * Invokes the scaffolder.
  31. *
  32. * @param Phar $phar Phar archive containing the current scaffolder.
  33. * @param string $root Path Root path.
  34. * @param array $options Options array (key/value).
  35. */
  36. abstract public function invoke(Phar $phar, $rootPath, $options = array());
  37. /**
  38. * Writes output to STDERR, followed by a newline (optional)
  39. *
  40. * @param string $message
  41. * @param string $newLine
  42. */
  43. protected function log($message, $newLine = true)
  44. {
  45. if (error_reporting() === 0) {
  46. return;
  47. }
  48. file_put_contents('php://stderr', $message . ($newLine ? "\r\n" : ''));
  49. }
  50. /**
  51. * Extract resources to a file system path
  52. *
  53. * @param Phar $phar Phar archive.
  54. * @param string $path Output path root.
  55. */
  56. protected function extractResources(Phar $phar, $path)
  57. {
  58. $this->deleteDirectory($path);
  59. $phar->extractTo($path);
  60. @unlink($path . '/index.php');
  61. @unlink($path . '/build.bat');
  62. $this->copyDirectory($path . '/resources', $path, false);
  63. $this->deleteDirectory($path . '/resources');
  64. }
  65. /**
  66. * Apply file transforms.
  67. *
  68. * @param string $rootPath Root path.
  69. * @param array $values Key/value array.
  70. */
  71. protected function applyTransforms($rootPath, $values)
  72. {
  73. if (is_null($rootPath) || !is_string($rootPath) || empty($rootPath)) {
  74. throw new InvalidArgumentException("Undefined \"rootPath\"");
  75. }
  76. if (is_dir($rootPath)) {
  77. $d = dir($rootPath);
  78. while ( false !== ( $entry = $d->read() ) ) {
  79. if ( $entry == '.' || $entry == '..' ) {
  80. continue;
  81. }
  82. $entry = $rootPath . '/' . $entry;
  83. $this->applyTransforms($entry, $values);
  84. }
  85. $d->close();
  86. } else {
  87. $contents = file_get_contents($rootPath);
  88. foreach ($values as $key => $value) {
  89. $contents = str_replace('$' . $key . '$', $value, $contents);
  90. }
  91. file_put_contents($rootPath, $contents);
  92. }
  93. return true;
  94. }
  95. /**
  96. * Create directory
  97. *
  98. * @param string $path Path of directory to create.
  99. * @param boolean $abortIfExists Abort if directory exists.
  100. * @param boolean $recursive Create parent directories if not exist.
  101. *
  102. * @return boolean
  103. */
  104. protected function createDirectory($path, $abortIfExists = true, $recursive = true) {
  105. if (is_null($path) || !is_string($path) || empty($path)) {
  106. throw new InvalidArgumentException ("Undefined \"path\"" );
  107. }
  108. if (is_dir($path) && $abortIfExists) {
  109. return false;
  110. }
  111. if (is_dir($path) ) {
  112. @chmod($path, '0777');
  113. if (!self::deleteDirectory($path) ) {
  114. throw new RuntimeException("Failed to delete \"{$path}\".");
  115. }
  116. }
  117. if (!mkdir($path, '0777', $recursive) || !is_dir($path)) {
  118. throw new RuntimeException( "Failed to create directory \"{$path}\"." );
  119. }
  120. return true;
  121. }
  122. /**
  123. * Fully copy a source directory to a target directory.
  124. *
  125. * @param string $sourcePath Source directory
  126. * @param string $destinationPath Target directory
  127. * @param boolean $abortIfExists Query re-creating target directory if exists
  128. * @param octal $mode Changes access mode
  129. *
  130. * @return boolean
  131. */
  132. protected function copyDirectory($sourcePath, $destinationPath, $abortIfExists = true, $mode = '0777') {
  133. if (is_null($sourcePath) || !is_string($sourcePath) || empty($sourcePath)) {
  134. throw new InvalidArgumentException("Undefined \"sourcePath\"");
  135. }
  136. if (is_null($destinationPath) || !is_string($destinationPath) || empty($destinationPath)) {
  137. throw new InvalidArgumentException("Undefined \"destinationPath\"");
  138. }
  139. if (is_dir($destinationPath) && $abortIfExists) {
  140. return false;
  141. }
  142. if (is_dir($sourcePath)) {
  143. if (!is_dir($destinationPath) && !mkdir($destinationPath, $mode)) {
  144. throw new RuntimeException("Failed to create target directory \"{$destinationPath}\"" );
  145. }
  146. $d = dir($sourcePath);
  147. while ( false !== ( $entry = $d->read() ) ) {
  148. if ( $entry == '.' || $entry == '..' ) {
  149. continue;
  150. }
  151. $strSourceEntry = $sourcePath . '/' . $entry;
  152. $strTargetEntry = $destinationPath . '/' . $entry;
  153. if (is_dir($strSourceEntry) ) {
  154. $this->copyDirectory(
  155. $strSourceEntry,
  156. $strTargetEntry,
  157. false,
  158. $mode
  159. );
  160. continue;
  161. }
  162. if (!copy($strSourceEntry, $strTargetEntry) ) {
  163. throw new RuntimeException (
  164. "Failed to copy"
  165. . " file \"{$strSourceEntry}\""
  166. . " to \"{$strTargetEntry}\""
  167. );
  168. }
  169. }
  170. $d->close();
  171. } else {
  172. if (!copy($sourcePath, $destinationPath)) {
  173. throw new RuntimeException (
  174. "Failed to copy"
  175. . " file \"{$sourcePath}\""
  176. . " to \"{$destinationPath}\""
  177. );
  178. }
  179. }
  180. return true;
  181. }
  182. /**
  183. * Delete directory and all of its contents;
  184. *
  185. * @param string $path Directory path
  186. * @return boolean
  187. */
  188. protected function deleteDirectory($path)
  189. {
  190. if (is_null($path) || !is_string($path) || empty($path)) {
  191. throw new InvalidArgumentException( "Undefined \"path\"" );
  192. }
  193. $handleDir = false;
  194. if (is_dir($path) ) {
  195. $handleDir = @opendir($path);
  196. }
  197. if (!$handleDir) {
  198. return false;
  199. }
  200. @chmod($path, 0777);
  201. while ($file = readdir($handleDir)) {
  202. if ($file == '.' || $file == '..') {
  203. continue;
  204. }
  205. $fsEntity = $path . "/" . $file;
  206. if (is_dir($fsEntity)) {
  207. $this->deleteDirectory($fsEntity);
  208. continue;
  209. }
  210. if (is_file($fsEntity)) {
  211. @unlink($fsEntity);
  212. continue;
  213. }
  214. throw new LogicException (
  215. "Unexpected file type: \"{$fsEntity}\""
  216. );
  217. }
  218. @chmod($path, 0777);
  219. closedir($handleDir);
  220. @rmdir($path);
  221. return true;
  222. }
  223. }