Microsoft_WindowsAzure_CommandLine
[ class tree: Microsoft_WindowsAzure_CommandLine ] [ index: Microsoft_WindowsAzure_CommandLine ] [ all elements ]

Source for file PackageScaffolderAbstract.php

Documentation is available at PackageScaffolderAbstract.php

  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. /**
  36.  * @see Microsoft_AutoLoader
  37.  */
  38. require_once dirname(__FILE__'/../../../AutoLoader.php';
  39.  
  40. /**
  41.  * @category   Microsoft
  42.  * @package    Microsoft_WindowsAzure_CommandLine
  43.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  44.  * @license    http://phpazure.codeplex.com/license
  45.  */ 
  46.     extends Microsoft_Console_Command
  47. {
  48.     /**
  49.      * Displays the help information.
  50.      * 
  51.      * @command-name <default>
  52.      * @command-name -h
  53.      * @command-name -help
  54.      * @command-description Displays the current help information.
  55.      */
  56.     public function helpCommand({
  57.         $handler $this->getHandler();
  58.         $newline "\r\n";
  59.         
  60.         if (count($handler->headers0{
  61.             foreach ($handler->headers as $header{
  62.                 printf('%s%s'$header$newline);        
  63.             }
  64.             printf($newline);
  65.         }
  66.         printf('%s%s'$handler->description$newline);
  67.         printf($newline);
  68.         printf('Available commands:%s'$newline);
  69.         foreach ($handler->commands as $command{
  70.             if ($command->aliases[0!= '<default>'{
  71.                 $description str_split($command->description50);
  72.                 printf('  %-25s %s%s'implode(', '$command->aliases)$description[0]$newline);
  73.                 for ($di 1$di count($description)$di++{
  74.                     printf('  %-25s %s%s'''$description[$di]$newline);
  75.                 }
  76.                 printf($newline);            
  77.                 
  78.                 if (count($command->parameters0{
  79.                     foreach ($command->parameters as $parameter{
  80.                         if ($parameter->aliases[0!= '--Phar' && $parameter->aliases[0!= '--Path'{
  81.                             $description str_split($parameter->description50);
  82.                             printf('    %-23s %s'implode(', '$parameter->aliases)$newline);
  83.                             for ($di 0$di count($description)$di++{
  84.                                 printf('    %-23s %s%s'''$description[$di]$newline);
  85.                             }
  86.                             printf($newline);
  87.                         }
  88.                     }
  89.                 }
  90.                 printf($newline);
  91.                 
  92.                 if (count($command->examples0{
  93.                     printf('    Example usage:%s'$newline);
  94.                     foreach ($command->examples as $example{
  95.                         printf('      %s%s'$example$newline);
  96.                     }
  97.                     printf($newline);
  98.                 }
  99.             }
  100.         }
  101.         
  102.         if (count($handler->footers0{
  103.             printf($newline);
  104.             foreach ($handler->footers as $footer{
  105.                 printf('%s%s'$footer$newline);        
  106.             }
  107.             printf($newline);
  108.         }
  109.     }
  110.     
  111.     /**
  112.      * Writes output to STDERR, followed by a newline (optional)
  113.      * 
  114.      * @param string $message 
  115.      * @param string $newLine 
  116.      */
  117.     protected function log($message$newLine true)
  118.     {
  119.         if (error_reporting(=== 0{
  120.             return;
  121.         }
  122.         file_put_contents('php://stderr'$message ($newLine "\r\n" ''));
  123.     }
  124.     
  125.     /**
  126.      * Extract resources to a file system path
  127.      * 
  128.      * @param Phar $phar Phar archive.
  129.      * @param string $path Output path root.
  130.      */
  131.     protected function extractResources(Phar $phar$path)
  132.     {
  133.         $this->deleteDirectory($path);
  134.         $phar->extractTo($path);
  135.         @unlink($path '/index.php');
  136.         @unlink($path '/build.bat');
  137.         $this->copyDirectory($path '/resources'$path '/tmp'false);
  138.         $this->deleteDirectory($path '/resources');
  139.         $this->copyDirectory($path '/tmp'$pathfalse);
  140.         $this->deleteDirectory($path '/tmp');
  141.     }
  142.     
  143.     /**
  144.      * Apply file transforms.
  145.      * 
  146.      * @param string $rootPath Root path.
  147.      * @param array $values Key/value array.
  148.      */
  149.     protected function applyTransforms($rootPath$values)
  150.     {
  151.         if (is_null($rootPath|| !is_string($rootPath|| empty($rootPath)) {
  152.             throw new InvalidArgumentException("Undefined \"rootPath\"");
  153.         }
  154.                         
  155.         if (is_dir($rootPath)) {
  156.             $d dir($rootPath);
  157.             while false !== $entry $d->read() ) ) {
  158.                 if $entry == '.' || $entry == '..' {
  159.                     continue;
  160.                 }
  161.                 $entry $rootPath '/' $entry
  162.                 
  163.                 $this->applyTransforms($entry$values);
  164.             }
  165.             $d->close();
  166.         else {
  167.             $contents file_get_contents($rootPath);
  168.             foreach ($values as $key => $value{
  169.                 $contents str_replace('$' $key '$'$value$contents);
  170.             }
  171.             file_put_contents($rootPath$contents);
  172.         }
  173.         
  174.         return true;
  175.     }
  176.     
  177.     /**
  178.      * Create directory
  179.      * 
  180.      * @param string  $path           Path of directory to create.
  181.      * @param boolean $abortIfExists  Abort if directory exists.
  182.      * @param boolean $recursive      Create parent directories if not exist.
  183.      * 
  184.      * @return boolean 
  185.      */
  186.     protected function createDirectory($path$abortIfExists true$recursive true{
  187.         if (is_null($path|| !is_string($path|| empty($path)) {
  188.             throw new InvalidArgumentException ("Undefined \"path\"" );        
  189.         }
  190.                 
  191.         if (is_dir($path&& $abortIfExists{
  192.             return false;       
  193.         }
  194.         
  195.         if (is_dir($path) ) {
  196.             @chmod($path'0777');
  197.             if (!self::deleteDirectory($path) ) {
  198.                 throw new RuntimeException("Failed to delete \"{$path}\".");
  199.             }
  200.         }
  201.             
  202.         if (!mkdir($path'0777'$recursive|| !is_dir($path)) {
  203.             throw new RuntimeException"Failed to create directory \"{$path}\".);
  204.         }
  205.  
  206.         return true;
  207.     }
  208.     
  209.     /**
  210.      * Fully copy a source directory to a target directory.
  211.      * 
  212.      * @param string  $sourcePath   Source directory
  213.      * @param string  $destinationPath   Target directory
  214.      * @param boolean $abortIfExists Query re-creating target directory if exists
  215.      * @param octal   $mode           Changes access mode
  216.      * 
  217.      * @return boolean 
  218.      */
  219.     protected function copyDirectory($sourcePath$destinationPath$abortIfExists true$mode '0777'{
  220.         if (is_null($sourcePath|| !is_string($sourcePath|| empty($sourcePath)) {
  221.             throw new InvalidArgumentException("Undefined \"sourcePath\"");
  222.         }
  223.         
  224.         if (is_null($destinationPath|| !is_string($destinationPath|| empty($destinationPath)) {
  225.             throw new InvalidArgumentException("Undefined \"destinationPath\"");
  226.         }
  227.                     
  228.         if (is_dir($destinationPath&& $abortIfExists{
  229.             return false;
  230.         }
  231.                         
  232.         if (is_dir($sourcePath)) {
  233.             if (!is_dir($destinationPath&& !mkdir($destinationPath$mode)) {
  234.                 throw new RuntimeException("Failed to create target directory \"{$destinationPath}\");
  235.             }
  236.             $d dir($sourcePath);
  237.             while false !== $entry $d->read() ) ) {
  238.                 if $entry == '.' || $entry == '..' {
  239.                     continue;
  240.                 }
  241.                 $strSourceEntry $sourcePath '/' $entry
  242.                 $strTargetEntry $destinationPath '/' $entry;
  243.                 if (is_dir($strSourceEntry) ) {
  244.                     $this->copyDirectory(
  245.                         $strSourceEntry
  246.                         $strTargetEntry
  247.                         false
  248.                         $mode
  249.                     );
  250.                     continue;
  251.                 }
  252.                 if (!copy($strSourceEntry$strTargetEntry) ) {
  253.                     throw new RuntimeException (
  254.                         "Failed to copy"
  255.                         . " file \"{$strSourceEntry}\""
  256.                         . " to \"{$strTargetEntry}\"
  257.                     );
  258.                 }
  259.             }
  260.             $d->close();
  261.         else {
  262.             if (!copy($sourcePath$destinationPath)) {
  263.                 throw new RuntimeException (
  264.                     "Failed to copy"
  265.                     . " file \"{$sourcePath}\""
  266.                     . " to \"{$destinationPath}\"
  267.                     
  268.                 );
  269.             }
  270.         }
  271.         
  272.         return true;
  273.     }
  274.     
  275.     /**
  276.      * Delete directory and all of its contents;
  277.      * 
  278.      * @param string $path Directory path
  279.      * @return boolean 
  280.      */
  281.     protected function deleteDirectory($path
  282.     {
  283.         if (is_null($path|| !is_string($path|| empty($path)) {
  284.             throw new InvalidArgumentException"Undefined \"path\"" );        
  285.         }
  286.         
  287.         $handleDir false;
  288.         if (is_dir($path) ) {    
  289.             $handleDir @opendir($path);
  290.         }
  291.         if (!$handleDir{
  292.             return false;
  293.         }
  294.         @chmod($path0777);
  295.         while ($file readdir($handleDir)) {
  296.             if ($file == '.' || $file == '..'{
  297.                 continue;
  298.             }
  299.             
  300.             $fsEntity $path "/" $file;
  301.             
  302.             if (is_dir($fsEntity)) {
  303.                 $this->deleteDirectory($fsEntity);
  304.                 continue;
  305.             }
  306.             
  307.             if (is_file($fsEntity)) {
  308.                 @unlink($fsEntity);
  309.                 continue;
  310.             }
  311.             
  312.             throw new LogicException (
  313.                 "Unexpected file type: \"{$fsEntity}\"
  314.             );
  315.         }
  316.         
  317.         @chmod($path0777);        
  318.         closedir($handleDir);
  319.         @rmdir($path);
  320.                      
  321.         return true;
  322.     }
  323. }

Documentation generated on Sat, 03 Dec 2011 13:59:31 +0100 by phpDocumentor 1.4.3