File indexing completed on 2024-06-23 05:55:44

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_WindowsAzure
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @category   Zend
0024  * @package    Zend_Service_WindowsAzure_CommandLine
0025  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0026  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0027  */ 
0028 abstract class Zend_Service_WindowsAzure_CommandLine_PackageScaffolder_PackageScaffolderAbstract
0029 {
0030   /**
0031    * Invokes the scaffolder.
0032    *
0033    * @param Phar $phar Phar archive containing the current scaffolder.
0034    * @param string $root Path Root path.
0035    * @param array $options Options array (key/value).
0036    */
0037   abstract public function invoke(Phar $phar, $rootPath, $options = array());
0038   
0039   /**
0040    * Writes output to STDERR, followed by a newline (optional)
0041    * 
0042    * @param string $message
0043    * @param string $newLine
0044    */
0045   protected function log($message, $newLine = true)
0046   {
0047     if (error_reporting() === 0) {
0048       return;
0049     }
0050     file_put_contents('php://stderr', $message . ($newLine ? "\r\n" : ''));
0051   }
0052   
0053   /**
0054    * Extract resources to a file system path
0055    * 
0056    * @param Phar $phar Phar archive.
0057    * @param string $path Output path root.
0058    */
0059   protected function extractResources(Phar $phar, $path)
0060   {
0061     $this->deleteDirectory($path);
0062     $phar->extractTo($path);
0063     @unlink($path . '/index.php');
0064     @unlink($path . '/build.bat');
0065     $this->copyDirectory($path . '/resources', $path, false);
0066     $this->deleteDirectory($path . '/resources');
0067   }
0068   
0069   /**
0070    * Apply file transforms.
0071    * 
0072    * @param string $rootPath Root path.
0073    * @param array $values Key/value array.
0074    */
0075   protected function applyTransforms($rootPath, $values)
0076   {
0077         if (is_null($rootPath) || !is_string($rootPath) || empty($rootPath)) {
0078             throw new InvalidArgumentException("Undefined \"rootPath\"");
0079         }
0080                         
0081         if (is_dir($rootPath)) {
0082             $d = dir($rootPath);
0083             while ( false !== ( $entry = $d->read() ) ) {
0084                 if ( $entry == '.' || $entry == '..' ) {
0085                     continue;
0086                 }
0087                 $entry = $rootPath . '/' . $entry; 
0088                 
0089                 $this->applyTransforms($entry, $values);
0090             }
0091             $d->close();
0092         } else {
0093           $contents = file_get_contents($rootPath);
0094           foreach ($values as $key => $value) {
0095             $contents = str_replace('$' . $key . '$', $value, $contents);
0096           }
0097             file_put_contents($rootPath, $contents);
0098         }
0099         
0100         return true;
0101   }
0102   
0103   /**
0104      * Create directory
0105      * 
0106      * @param string  $path           Path of directory to create.
0107      * @param boolean $abortIfExists  Abort if directory exists.
0108      * @param boolean $recursive      Create parent directories if not exist.
0109      * 
0110      * @return boolean
0111      */
0112     protected function createDirectory($path, $abortIfExists = true, $recursive = true) {
0113         if (is_null($path) || !is_string($path) || empty($path)) {
0114             throw new InvalidArgumentException ("Undefined \"path\"" );        
0115         }
0116                 
0117         if (is_dir($path) && $abortIfExists) {
0118             return false;       
0119         }
0120         
0121         if (is_dir($path) ) {
0122             @chmod($path, '0775');
0123             if (!self::deleteDirectory($path) ) {
0124                 throw new RuntimeException("Failed to delete \"{$path}\".");
0125             }
0126         }
0127             
0128         if (!mkdir($path, '0775', $recursive) || !is_dir($path)) {
0129             throw new RuntimeException( "Failed to create directory \"{$path}\"." );
0130         }
0131 
0132         return true;
0133     }
0134     
0135     /**
0136      * Fully copy a source directory to a target directory.
0137      * 
0138      * @param string  $sourcePath   Source directory
0139      * @param string  $destinationPath   Target directory
0140      * @param boolean $abortIfExists Query re-creating target directory if exists
0141      * @param octal   $mode           Changes access mode
0142      * 
0143      * @return boolean
0144      */
0145     protected function copyDirectory($sourcePath, $destinationPath, $abortIfExists = true, $mode = '0775') {
0146         $mode = $mode & ~0002;
0147 
0148         if (is_null($sourcePath) || !is_string($sourcePath) || empty($sourcePath)) {
0149             throw new InvalidArgumentException("Undefined \"sourcePath\"");
0150         }
0151         
0152         if (is_null($destinationPath) || !is_string($destinationPath) || empty($destinationPath)) {
0153           throw new InvalidArgumentException("Undefined \"destinationPath\"");
0154         }
0155                     
0156         if (is_dir($destinationPath) && $abortIfExists) {
0157             return false;
0158         }
0159                         
0160         if (is_dir($sourcePath)) {
0161             if (!is_dir($destinationPath) && !mkdir($destinationPath, $mode)) {
0162                 throw new RuntimeException("Failed to create target directory \"{$destinationPath}\"" );
0163             }
0164             $d = dir($sourcePath);
0165             while ( false !== ( $entry = $d->read() ) ) {
0166                 if ( $entry == '.' || $entry == '..' ) {
0167                     continue;
0168                 }
0169                 $strSourceEntry = $sourcePath . '/' . $entry; 
0170                 $strTargetEntry = $destinationPath . '/' . $entry;
0171                 if (is_dir($strSourceEntry) ) {
0172                     $this->copyDirectory(
0173                       $strSourceEntry, 
0174                       $strTargetEntry, 
0175                       false, 
0176                       $mode
0177                     );
0178                     continue;
0179                 }
0180                 if (!copy($strSourceEntry, $strTargetEntry) ) {
0181                     throw new RuntimeException (
0182                         "Failed to copy"
0183                         . " file \"{$strSourceEntry}\""
0184                         . " to \"{$strTargetEntry}\"" 
0185                     );
0186                 }
0187             }
0188             $d->close();
0189         } else {
0190             if (!copy($sourcePath, $destinationPath)) {
0191                 throw new RuntimeException (
0192                     "Failed to copy"
0193                     . " file \"{$sourcePath}\""
0194                     . " to \"{$destinationPath}\"" 
0195                     
0196                 );
0197             }
0198         }
0199         
0200         return true;
0201     }
0202     
0203     /**
0204      * Delete directory and all of its contents;
0205      * 
0206      * @param string $path Directory path
0207      * @return boolean
0208      */
0209     protected function deleteDirectory($path) 
0210     {
0211         if (is_null($path) || !is_string($path) || empty($path)) {
0212             throw new InvalidArgumentException( "Undefined \"path\"" );        
0213         }
0214         
0215         $handleDir = false;
0216         if (is_dir($path) ) {    
0217             $handleDir = @opendir($path);
0218         }
0219         if (!$handleDir) {
0220             return false;
0221         }
0222         @chmod($path, 0775);
0223         while ($file = readdir($handleDir)) {
0224             if ($file == '.' || $file == '..') {
0225                 continue;
0226             }
0227             
0228             $fsEntity = $path . "/" . $file;
0229             
0230             if (is_dir($fsEntity)) {
0231                 $this->deleteDirectory($fsEntity);
0232                 continue;
0233             }
0234             
0235             if (is_file($fsEntity)) {
0236                 @unlink($fsEntity);
0237                 continue;
0238             }
0239             
0240             throw new LogicException (
0241                 "Unexpected file type: \"{$fsEntity}\"" 
0242             );
0243         }
0244         
0245         @chmod($path, 0775);        
0246         closedir($handleDir);
0247         @rmdir($path);
0248                      
0249         return true;
0250     }
0251 }