PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/CommandLine/PackageScaffolder/PackageScaffolderAbstract.php

https://bitbucket.org/ktos/tinyshare
PHP | 324 lines | 201 code | 31 blank | 92 comment | 64 complexity | a07954b36ff3f3baade7a8c9eba8aecc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2011, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  31. * @license http://phpazure.codeplex.com/license
  32. * @version $Id: SharedKeyCredentials.php 14561 2009-05-07 08:05:12Z unknown $
  33. */
  34. /**
  35. * @see Microsoft_AutoLoader
  36. */
  37. require_once dirname(__FILE__) . '/../../../AutoLoader.php';
  38. /**
  39. * @category Microsoft
  40. * @package Microsoft_WindowsAzure_CommandLine
  41. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  42. * @license http://phpazure.codeplex.com/license
  43. */
  44. abstract class Microsoft_WindowsAzure_CommandLine_PackageScaffolder_PackageScaffolderAbstract
  45. extends Microsoft_Console_Command
  46. {
  47. /**
  48. * Displays the help information.
  49. *
  50. * @command-name <default>
  51. * @command-name -h
  52. * @command-name -help
  53. * @command-description Displays the current help information.
  54. */
  55. public function helpCommand() {
  56. $handler = $this->getHandler();
  57. $newline = "\r\n";
  58. if (count($handler->headers) > 0) {
  59. foreach ($handler->headers as $header) {
  60. printf('%s%s', $header, $newline);
  61. }
  62. printf($newline);
  63. }
  64. printf('%s%s', $handler->description, $newline);
  65. printf($newline);
  66. printf('Available commands:%s', $newline);
  67. foreach ($handler->commands as $command) {
  68. if ($command->aliases[0] != '<default>') {
  69. $description = str_split($command->description, 50);
  70. printf(' %-25s %s%s', implode(', ', $command->aliases), $description[0], $newline);
  71. for ($di = 1; $di < count($description); $di++) {
  72. printf(' %-25s %s%s', '', $description[$di], $newline);
  73. }
  74. printf($newline);
  75. if (count($command->parameters) > 0) {
  76. foreach ($command->parameters as $parameter) {
  77. if ($parameter->aliases[0] != '--Phar' && $parameter->aliases[0] != '--Path') {
  78. $description = str_split($parameter->description, 50);
  79. printf(' %-23s %s', implode(', ', $parameter->aliases), $newline);
  80. for ($di = 0; $di < count($description); $di++) {
  81. printf(' %-23s %s%s', '', $description[$di], $newline);
  82. }
  83. printf($newline);
  84. }
  85. }
  86. }
  87. printf($newline);
  88. if (count($command->examples) > 0) {
  89. printf(' Example usage:%s', $newline);
  90. foreach ($command->examples as $example) {
  91. printf(' %s%s', $example, $newline);
  92. }
  93. printf($newline);
  94. }
  95. }
  96. }
  97. if (count($handler->footers) > 0) {
  98. printf($newline);
  99. foreach ($handler->footers as $footer) {
  100. printf('%s%s', $footer, $newline);
  101. }
  102. printf($newline);
  103. }
  104. }
  105. /**
  106. * Writes output to STDERR, followed by a newline (optional)
  107. *
  108. * @param string $message
  109. * @param string $newLine
  110. */
  111. protected function log($message, $newLine = true)
  112. {
  113. if (error_reporting() === 0) {
  114. return;
  115. }
  116. file_put_contents('php://stderr', $message . ($newLine ? "\r\n" : ''));
  117. }
  118. /**
  119. * Extract resources to a file system path
  120. *
  121. * @param Phar $phar Phar archive.
  122. * @param string $path Output path root.
  123. */
  124. protected function extractResources(Phar $phar, $path)
  125. {
  126. $this->deleteDirectory($path);
  127. $phar->extractTo($path);
  128. @unlink($path . '/index.php');
  129. @unlink($path . '/build.bat');
  130. $this->copyDirectory($path . '/resources', $path . '/tmp', false);
  131. $this->deleteDirectory($path . '/resources');
  132. $this->copyDirectory($path . '/tmp', $path, false);
  133. $this->deleteDirectory($path . '/tmp');
  134. }
  135. /**
  136. * Apply file transforms.
  137. *
  138. * @param string $rootPath Root path.
  139. * @param array $values Key/value array.
  140. */
  141. protected function applyTransforms($rootPath, $values)
  142. {
  143. if (is_null($rootPath) || !is_string($rootPath) || empty($rootPath)) {
  144. throw new InvalidArgumentException("Undefined \"rootPath\"");
  145. }
  146. if (is_dir($rootPath)) {
  147. $d = dir($rootPath);
  148. while ( false !== ( $entry = $d->read() ) ) {
  149. if ( $entry == '.' || $entry == '..' ) {
  150. continue;
  151. }
  152. $entry = $rootPath . '/' . $entry;
  153. $this->applyTransforms($entry, $values);
  154. }
  155. $d->close();
  156. } else {
  157. $contents = file_get_contents($rootPath);
  158. foreach ($values as $key => $value) {
  159. $contents = str_replace('$' . $key . '$', $value, $contents);
  160. }
  161. file_put_contents($rootPath, $contents);
  162. }
  163. return true;
  164. }
  165. /**
  166. * Create directory
  167. *
  168. * @param string $path Path of directory to create.
  169. * @param boolean $abortIfExists Abort if directory exists.
  170. * @param boolean $recursive Create parent directories if not exist.
  171. *
  172. * @return boolean
  173. */
  174. protected function createDirectory($path, $abortIfExists = true, $recursive = true) {
  175. if (is_null($path) || !is_string($path) || empty($path)) {
  176. throw new InvalidArgumentException ("Undefined \"path\"" );
  177. }
  178. if (is_dir($path) && $abortIfExists) {
  179. return false;
  180. }
  181. if (is_dir($path) ) {
  182. @chmod($path, '0777');
  183. if (!self::deleteDirectory($path) ) {
  184. throw new RuntimeException("Failed to delete \"{$path}\".");
  185. }
  186. }
  187. if (!mkdir($path, '0777', $recursive) || !is_dir($path)) {
  188. throw new RuntimeException( "Failed to create directory \"{$path}\"." );
  189. }
  190. return true;
  191. }
  192. /**
  193. * Fully copy a source directory to a target directory.
  194. *
  195. * @param string $sourcePath Source directory
  196. * @param string $destinationPath Target directory
  197. * @param boolean $abortIfExists Query re-creating target directory if exists
  198. * @param octal $mode Changes access mode
  199. *
  200. * @return boolean
  201. */
  202. protected function copyDirectory($sourcePath, $destinationPath, $abortIfExists = true, $mode = '0777') {
  203. if (is_null($sourcePath) || !is_string($sourcePath) || empty($sourcePath)) {
  204. throw new InvalidArgumentException("Undefined \"sourcePath\"");
  205. }
  206. if (is_null($destinationPath) || !is_string($destinationPath) || empty($destinationPath)) {
  207. throw new InvalidArgumentException("Undefined \"destinationPath\"");
  208. }
  209. if (is_dir($destinationPath) && $abortIfExists) {
  210. return false;
  211. }
  212. if (is_dir($sourcePath)) {
  213. if (!is_dir($destinationPath) && !mkdir($destinationPath, $mode)) {
  214. throw new RuntimeException("Failed to create target directory \"{$destinationPath}\"" );
  215. }
  216. $d = dir($sourcePath);
  217. while ( false !== ( $entry = $d->read() ) ) {
  218. if ( $entry == '.' || $entry == '..' ) {
  219. continue;
  220. }
  221. $strSourceEntry = $sourcePath . '/' . $entry;
  222. $strTargetEntry = $destinationPath . '/' . $entry;
  223. if (is_dir($strSourceEntry) ) {
  224. $this->copyDirectory(
  225. $strSourceEntry,
  226. $strTargetEntry,
  227. false,
  228. $mode
  229. );
  230. continue;
  231. }
  232. if (!copy($strSourceEntry, $strTargetEntry) ) {
  233. throw new RuntimeException (
  234. "Failed to copy"
  235. . " file \"{$strSourceEntry}\""
  236. . " to \"{$strTargetEntry}\""
  237. );
  238. }
  239. }
  240. $d->close();
  241. } else {
  242. if (!copy($sourcePath, $destinationPath)) {
  243. throw new RuntimeException (
  244. "Failed to copy"
  245. . " file \"{$sourcePath}\""
  246. . " to \"{$destinationPath}\""
  247. );
  248. }
  249. }
  250. return true;
  251. }
  252. /**
  253. * Delete directory and all of its contents;
  254. *
  255. * @param string $path Directory path
  256. * @return boolean
  257. */
  258. protected function deleteDirectory($path)
  259. {
  260. if (is_null($path) || !is_string($path) || empty($path)) {
  261. throw new InvalidArgumentException( "Undefined \"path\"" );
  262. }
  263. $handleDir = false;
  264. if (is_dir($path) ) {
  265. $handleDir = @opendir($path);
  266. }
  267. if (!$handleDir) {
  268. return false;
  269. }
  270. @chmod($path, 0777);
  271. while ($file = readdir($handleDir)) {
  272. if ($file == '.' || $file == '..') {
  273. continue;
  274. }
  275. $fsEntity = $path . "/" . $file;
  276. if (is_dir($fsEntity)) {
  277. $this->deleteDirectory($fsEntity);
  278. continue;
  279. }
  280. if (is_file($fsEntity)) {
  281. @unlink($fsEntity);
  282. continue;
  283. }
  284. throw new LogicException (
  285. "Unexpected file type: \"{$fsEntity}\""
  286. );
  287. }
  288. @chmod($path, 0777);
  289. closedir($handleDir);
  290. @rmdir($path);
  291. return true;
  292. }
  293. }