File indexing completed on 2024-12-29 05:28:03

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_Console
0017  * @subpackage Exception
0018  * @version    $Id$
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** @see Zend_Xml_Security */
0024 // require_once 'Zend/Xml/Security.php';
0025 
0026 /**
0027 * @see Zend_Service_Console_Command
0028 */
0029 // require_once 'Zend/Service/Console/Command.php';
0030 
0031 /**
0032  * Package commands
0033  *
0034  * @category   Zend
0035  * @package    Zend_Service_WindowsAzure_CommandLine
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  *
0039  * @command-handler package
0040  * @command-handler-description Windows Azure Package commands
0041  * @command-handler-header Windows Azure SDK for PHP
0042  * @command-handler-header Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
0043  * @command-handler-footer
0044  * @command-handler-footer All commands support the --ConfigurationFile or -F parameter.
0045  * @command-handler-footer The parameter file is a simple INI file carrying one parameter
0046  * @command-handler-footer value per line. It accepts the same parameters as one can
0047  * @command-handler-footer use from the command line command.
0048  */
0049 class Zend_Service_WindowsAzure_CommandLine_Package
0050   extends Zend_Service_Console_Command
0051 {
0052   /**
0053    * Scaffolds a Windows Azure project structure which can be customized before packaging.
0054    *
0055    * @command-name Scaffold
0056    * @command-description Scaffolds a Windows Azure project structure which can be customized before packaging.
0057    *
0058    * @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.
0059    * @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
0060    */
0061   public function scaffoldCommand($path, $scaffolder, $argv)
0062   {
0063     // Default parameter value
0064     if ($scaffolder == '') {
0065       $scaffolder = dirname(__FILE__) . '/Scaffolders/DefaultScaffolder.phar';
0066     }
0067     $scaffolder = realpath($scaffolder);
0068 
0069     // Verify scaffolder
0070     if (!is_file($scaffolder)) {
0071       // require_once 'Zend/Service/Console/Exception.php';
0072       throw new Zend_Service_Console_Exception('Could not locate the given scaffolder: ' . $scaffolder);
0073     }
0074 
0075     // Include scaffolder
0076     $archive = new Phar($scaffolder);
0077     include $scaffolder;
0078     if (!class_exists('Scaffolder')) {
0079       // require_once 'Zend/Service/Console/Exception.php';
0080       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.');
0081     }
0082 
0083     // Cleanup $argv
0084     $options = array();
0085     foreach ($argv as $arg) {
0086       list($key, $value) = explode(':', $arg, 2);
0087       while (substr($key, 0, 1) == '-') {
0088         $key = substr($key, 1);
0089       }
0090       $options[$key] = $value;
0091     }
0092 
0093     // Run scaffolder
0094     $scaffolderInstance = new Scaffolder();
0095     $scaffolderInstance->invoke($archive, $path, $options);
0096   }
0097 
0098 
0099   /**
0100    * Packages a Windows Azure project structure.
0101    *
0102    * @command-name Create
0103    * @command-description Packages a Windows Azure project structure.
0104    *
0105    * @command-parameter-for $path Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --Path|-p Required. The path to package.
0106    * @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.
0107    * @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.
0108    */
0109   public function createPackageCommand($path, $runDevFabric, $outputPath)
0110   {
0111     // Create output paths
0112     if ($outputPath == '') {
0113       $outputPath = realpath($path . '/../');
0114     }
0115     $packageOut = $outputPath . '/' . basename($path) . '.cspkg';
0116 
0117     // Find Windows Azure SDK bin folder
0118     $windowsAzureSdkFolderCandidates = array_merge(
0119       isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
0120       isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
0121       isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array()
0122     );
0123     if (count($windowsAzureSdkFolderCandidates) == 0) {
0124       throw new Zend_Service_Console_Exception('Could not locate Windows Azure SDK for PHP.');
0125     }
0126     $cspack = '"' . $windowsAzureSdkFolderCandidates[0] . '\cspack.exe' . '"';
0127     $csrun = '"' . $windowsAzureSdkFolderCandidates[0] . '\csrun.exe' . '"';
0128 
0129     // Open the ServiceDefinition.csdef file and check for role paths
0130     $serviceDefinitionFile = $path . '/ServiceDefinition.csdef';
0131     if (!file_exists($serviceDefinitionFile)) {
0132       // require_once 'Zend/Service/Console/Exception.php';
0133       throw new Zend_Service_Console_Exception('Could not locate ServiceDefinition.csdef at ' . $serviceDefinitionFile . '.');
0134     }
0135     $serviceDefinition = Zend_Xml_Security::scanFile($serviceDefinitionFile);
0136     $xmlRoles = array();
0137     if ($serviceDefinition->WebRole) {
0138       if (count($serviceDefinition->WebRole) > 1) {
0139           $xmlRoles = array_merge($xmlRoles, $serviceDefinition->WebRole);
0140       } else {
0141           $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WebRole));
0142         }
0143     }
0144     if ($serviceDefinition->WorkerRole) {
0145       if (count($serviceDefinition->WorkerRole) > 1) {
0146           $xmlRoles = array_merge($xmlRoles, $serviceDefinition->WorkerRole);
0147       } else {
0148           $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WorkerRole));
0149         }
0150     }
0151 
0152     // Build '/role:' command parameter
0153     $roleArgs = array();
0154     foreach ($xmlRoles as $xmlRole) {
0155       if ($xmlRole["name"]) {
0156         $roleArgs[] = '/role:' . $xmlRole["name"] . ';' . realpath($path . '/' . $xmlRole["name"]);
0157       }
0158     }
0159 
0160     // Build command
0161     $command = $cspack;
0162     $args = array(
0163       $path . '\ServiceDefinition.csdef',
0164       implode(' ', $roleArgs),
0165       '/out:' . $packageOut
0166     );
0167     if ($runDevFabric) {
0168       $args[] = '/copyOnly';
0169     }
0170     passthru($command . ' ' . implode(' ', $args));
0171 
0172     // Can we copy a configuration file?
0173     $serviceConfigurationFile = $path . '/ServiceConfiguration.cscfg';
0174     $serviceConfigurationFileOut = $outputPath . '/ServiceConfiguration.cscfg';
0175     if (file_exists($serviceConfigurationFile) && !file_exists($serviceConfigurationFileOut)) {
0176       copy($serviceConfigurationFile, $serviceConfigurationFileOut);
0177     }
0178 
0179     // Do we have to start the development fabric?
0180     if ($runDevFabric) {
0181       passthru($csrun . ' /devstore:start /devfabric:start');
0182       passthru($csrun . ' /removeAll');
0183       passthru($csrun . ' /run:"' . $packageOut . ';' . $serviceConfigurationFileOut . '" /launchBrowser');
0184     }
0185   }
0186 
0187   /**
0188    * Creates a scaffolder from a given path.
0189    *
0190    * @command-name CreateScaffolder
0191    * @command-description Creates a scaffolder from a given path.
0192    *
0193    * @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.
0194    * @command-parameter-for $scaffolderFile Zend_Service_Console_Command_ParameterSource_Argv|Zend_Service_Console_Command_ParameterSource_ConfigFile --OutFile|-out Required. The filename of the scaffolder.
0195    */
0196   public function createScaffolderCommand($rootPath, $scaffolderFile)
0197   {
0198     $archive = new Phar($scaffolderFile);
0199     $archive->buildFromIterator(
0200       new RecursiveIteratorIterator(
0201         new RecursiveDirectoryIterator(realpath($rootPath))),
0202     realpath($rootPath));
0203   }
0204 }
0205 Zend_Service_Console_Command::bootstrap($_SERVER['argv']);