PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Service/WindowsAzure/CommandLine/Package.php

https://bitbucket.org/hieronim1981/tunethemusic
PHP | 198 lines | 105 code | 15 blank | 78 comment | 22 complexity | b6a46cd9c4549f52ca6fc07f2c1ae8d2 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_Console
  17. * @subpackage Exception
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * Package commands
  24. *
  25. * @category Zend
  26. * @package Zend_Service_WindowsAzure_CommandLine
  27. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. *
  30. * @command-handler package
  31. * @command-handler-description Windows Azure Package commands
  32. * @command-handler-header Windows Azure SDK for PHP
  33. * @command-handler-header Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  34. * @command-handler-footer
  35. * @command-handler-footer All commands support the --ConfigurationFile or -F parameter.
  36. * @command-handler-footer The parameter file is a simple INI file carrying one parameter
  37. * @command-handler-footer value per line. It accepts the same parameters as one can
  38. * @command-handler-footer use from the command line command.
  39. */
  40. class Zend_Service_WindowsAzure_CommandLine_Package
  41. extends Zend_Service_Console_Command
  42. {
  43. /**
  44. * Scaffolds a Windows Azure project structure which can be customized before packaging.
  45. *
  46. * @command-name Scaffold
  47. * @command-description Scaffolds a Windows Azure project structure which can be customized before packaging.
  48. *
  49. * @command-parameter-for $path Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --Path|-p Required. The path to create the Windows Azure project structure.
  50. * @command-parameter-for $scaffolder Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile|Zend_Service_Console_Command_ParameterSource_Env --Scaffolder|-s Optional. The path to the scaffolder to use. Defaults to Scaffolders/DefaultScaffolder.phar
  51. */
  52. public function scaffoldCommand($path, $scaffolder, $argv)
  53. {
  54. // Default parameter value
  55. if ($scaffolder == '') {
  56. $scaffolder = dirname(__FILE__) . '/Scaffolders/DefaultScaffolder.phar';
  57. }
  58. $scaffolder = realpath($scaffolder);
  59. // Verify scaffolder
  60. if (!is_file($scaffolder)) {
  61. require_once 'Zend/Service/Console/Exception.php';
  62. throw new Zend_Service_Console_Exception('Could not locate the given scaffolder: ' . $scaffolder);
  63. }
  64. // Include scaffolder
  65. $archive = new Phar($scaffolder);
  66. include $scaffolder;
  67. if (!class_exists('Scaffolder')) {
  68. require_once 'Zend/Service/Console/Exception.php';
  69. throw new Zend_Service_Console_Exception('Could not locate a class named Scaffolder in the given scaffolder: ' . $scaffolder . '. Make sure the scaffolder package contains a file named index.php and contains a class named Scaffolder.');
  70. }
  71. // Cleanup $argv
  72. $options = array();
  73. foreach ($argv as $arg) {
  74. list($key, $value) = explode(':', $arg, 2);
  75. while (substr($key, 0, 1) == '-') {
  76. $key = substr($key, 1);
  77. }
  78. $options[$key] = $value;
  79. }
  80. // Run scaffolder
  81. $scaffolderInstance = new Scaffolder();
  82. $scaffolderInstance->invoke($archive, $path, $options);
  83. }
  84. /**
  85. * Packages a Windows Azure project structure.
  86. *
  87. * @command-name Create
  88. * @command-description Packages a Windows Azure project structure.
  89. *
  90. * @command-parameter-for $path Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --Path|-p Required. The path to package.
  91. * @command-parameter-for $runDevFabric Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --RunDevFabric|-dev Required. Switch. Run and deploy to the Windows Azure development fabric.
  92. * @command-parameter-for $outputPath Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --OutputPath|-out Optional. The output path for the resulting package.
  93. */
  94. public function createPackageCommand($path, $runDevFabric, $outputPath)
  95. {
  96. // Create output paths
  97. if ($outputPath == '') {
  98. $outputPath = realpath($path . '/../');
  99. }
  100. $packageOut = $outputPath . '/' . basename($path) . '.cspkg';
  101. // Find Windows Azure SDK bin folder
  102. $windowsAzureSdkFolderCandidates = array_merge(
  103. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  104. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  105. isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array()
  106. );
  107. if (count($windowsAzureSdkFolderCandidates) == 0) {
  108. throw new Zend_Service_Console_Exception('Could not locate Windows Azure SDK for PHP.');
  109. }
  110. $cspack = '"' . $windowsAzureSdkFolderCandidates[0] . '\cspack.exe' . '"';
  111. $csrun = '"' . $windowsAzureSdkFolderCandidates[0] . '\csrun.exe' . '"';
  112. // Open the ServiceDefinition.csdef file and check for role paths
  113. $serviceDefinitionFile = $path . '/ServiceDefinition.csdef';
  114. if (!file_exists($serviceDefinitionFile)) {
  115. require_once 'Zend/Service/Console/Exception.php';
  116. throw new Zend_Service_Console_Exception('Could not locate ServiceDefinition.csdef at ' . $serviceDefinitionFile . '.');
  117. }
  118. $serviceDefinition = simplexml_load_file($serviceDefinitionFile);
  119. $xmlRoles = array();
  120. if ($serviceDefinition->WebRole) {
  121. if (count($serviceDefinition->WebRole) > 1) {
  122. $xmlRoles = array_merge($xmlRoles, $serviceDefinition->WebRole);
  123. } else {
  124. $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WebRole));
  125. }
  126. }
  127. if ($serviceDefinition->WorkerRole) {
  128. if (count($serviceDefinition->WorkerRole) > 1) {
  129. $xmlRoles = array_merge($xmlRoles, $serviceDefinition->WorkerRole);
  130. } else {
  131. $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WorkerRole));
  132. }
  133. }
  134. // Build '/role:' command parameter
  135. $roleArgs = array();
  136. foreach ($xmlRoles as $xmlRole) {
  137. if ($xmlRole["name"]) {
  138. $roleArgs[] = '/role:' . $xmlRole["name"] . ';' . realpath($path . '/' . $xmlRole["name"]);
  139. }
  140. }
  141. // Build command
  142. $command = $cspack;
  143. $args = array(
  144. $path . '\ServiceDefinition.csdef',
  145. implode(' ', $roleArgs),
  146. '/out:' . $packageOut
  147. );
  148. if ($runDevFabric) {
  149. $args[] = '/copyOnly';
  150. }
  151. passthru($command . ' ' . implode(' ', $args));
  152. // Can we copy a configuration file?
  153. $serviceConfigurationFile = $path . '/ServiceConfiguration.cscfg';
  154. $serviceConfigurationFileOut = $outputPath . '/ServiceConfiguration.cscfg';
  155. if (file_exists($serviceConfigurationFile) && !file_exists($serviceConfigurationFileOut)) {
  156. copy($serviceConfigurationFile, $serviceConfigurationFileOut);
  157. }
  158. // Do we have to start the development fabric?
  159. if ($runDevFabric) {
  160. passthru($csrun . ' /devstore:start /devfabric:start');
  161. passthru($csrun . ' /removeAll');
  162. passthru($csrun . ' /run:"' . $packageOut . ';' . $serviceConfigurationFileOut . '" /launchBrowser');
  163. }
  164. }
  165. /**
  166. * Creates a scaffolder from a given path.
  167. *
  168. * @command-name CreateScaffolder
  169. * @command-description Creates a scaffolder from a given path.
  170. *
  171. * @command-parameter-for $rootPath Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --Path|-p Required. The path to package into a scaffolder.
  172. * @command-parameter-for $scaffolderFile Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --OutFile|-out Required. The filename of the scaffolder.
  173. */
  174. public function createScaffolderCommand($rootPath, $scaffolderFile)
  175. {
  176. $archive = new Phar($scaffolderFile);
  177. $archive->buildFromIterator(
  178. new RecursiveIteratorIterator(
  179. new RecursiveDirectoryIterator(realpath($rootPath))),
  180. realpath($rootPath));
  181. }
  182. }
  183. Zend_Service_Console_Command::bootstrap($_SERVER['argv']);