home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / PHP / Beautifier / Batch.php next >
Encoding:
PHP Script  |  2008-07-02  |  10.0 KB  |  313 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Definition of  PHP_Beautifier_Batch
  5. *
  6. * PHP version 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. * @category   PHP
  14. * @package PHP_Beautifier
  15. * @subpackage Batch
  16. * @author Claudio Bustos <cdx@users.sourceforge.com>
  17. * @copyright  2004-2006 Claudio Bustos
  18. * @link     http://pear.php.net/package/PHP_Beautifier
  19. * @link     http://beautifyphp.sourceforge.net
  20. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  21. * @version    CVS: $Id:$
  22. */
  23. /**
  24. * Require PHP_Beautifier_Decorator
  25. */
  26. require_once 'Decorator.php';
  27. /**
  28. * Require PHP_Beautifier_Batch_Output
  29. */
  30. require_once 'Batch/Output.php';
  31. // ArrayNested->off();
  32. // ArrayNested->on();
  33.  
  34. /**
  35. * Adds functionality to handle multiple files.
  36. * - STDIN  : As normal
  37. * - STDOUT : Send all the scripts, prepended with the name of the original route
  38. * - One in, one out: as normal
  39. * - Multiple In, one out: determine the type of out.
  40. *   - Without '/' at the end, same as STDOUT.
  41. *   - With '/' at the end, copy the base structure and copy all the scripts
  42. *
  43. * You must define an input file. By default, the output is "./", so the saving
  44. * of files will be done on the directory of your command prompt.
  45. *
  46. * If the file out end in .tgz, the output will be a tar archive. The same action
  47. * will be obtained with {@link setCompress()} to true
  48. * Use:
  49. * <code>
  50. * require "PHP/Beautifier.php";
  51. * require "PHP/Beautifier/Batch.php";
  52. * $oBeaut= new PHP_Beautifier();
  53. * $oBatch= new PHP_Beautifier_Batch($oBeaut); // Decorator
  54. * $oBatch->setInputFile(__FILE__);
  55. * $oBatch->setOutputFile(dirname(__FILE__)."/beautified/");
  56. * $oBatch->process();
  57. * $oBatch->save();
  58. * </code>
  59. *
  60. * @category     PHP
  61. * @package      PHP_Beautifier
  62. * @subpackage   Batch
  63. * @author       Claudio Bustos <cdx@users.sourceforge.com>
  64. * @copyright    2004-2006 Claudio Bustos
  65. * @link         http://pear.php.net/package/PHP_Beautifier
  66. * @link         http://beautifyphp.sourceforge.net
  67. * @license      http://www.php.net/license/3_0.txt  PHP License 3.0
  68. * @version      Release: 0.1.14
  69. */
  70. class PHP_Beautifier_Batch extends PHP_Beautifier_Decorator {
  71.     /**
  72.     * Compression method (for now, false, 'gz' and 'bz2')
  73.     * @var string
  74.     */
  75.     private $sCompress = false;
  76.     /**
  77.     * Array or STDIN of paths to parse
  78.     * @var array
  79.     */
  80.     private $mPreInputFiles = array();
  81.     /**
  82.     * Path to the output
  83.     */
  84.     private $sPreOutputFile = './';
  85.     /**
  86.     * @var PHP_Beautifier_Batch_Output
  87.     */
  88.     private $oBatchOutput;
  89.     /**
  90.     * @var array    PHP_Beautifier_Batch_Input
  91.     */
  92.     private $aBatchInputs;
  93.     public $mInputFiles;
  94.     /**
  95.     * Output mode.  Could be {@link PHP_Beautifier_Batch::FILES} or
  96.     *               {@link PHP_Beautifier_Batch::DIRECTORY}
  97.     */
  98.     private $sOutputMode;
  99.     const FILES = 'Files';
  100.     const DIRECTORY = 'Directory';
  101.     /**
  102.     * Recursive search on dirs
  103.     * @var bool
  104.     */
  105.     public $bRecursive = false;
  106.     // public methods, overloaded from PHP_Beautifier
  107.     
  108.     /**
  109.     * Set recursive search for files in dirs on
  110.     * @param bool
  111.     */
  112.     public function setRecursive($bRecursive = true) 
  113.     {
  114.         $this->bRecursive = $bRecursive;
  115.     }
  116.     /**
  117.     * Set compression on/off
  118.     * @param mixed bool(false, true for gzip) or string ('gz' or 'gz2')
  119.     */
  120.     public function setCompress($mCompress = true) 
  121.     {
  122.         if ($mCompress === true) {
  123.             $mCompress = 'gz';
  124.         } elseif (!$mCompress) {
  125.             $mCompress = false;
  126.         } elseif (!is_string($mCompress)) {
  127.             throw (new Exception('You have to define a mode for compress'));
  128.         }
  129.         $this->sCompress = $mCompress;
  130.     }
  131.     /**
  132.     * Set the input(s) files
  133.     * Could be STDIN or a name, with special chars (?,*)
  134.     * @param mixed STDIN or string(path)
  135.     * @return bool
  136.     */
  137.     public function setInputFile($mFiles) 
  138.     {
  139.         $bCli = (php_sapi_name() == 'cli');
  140.         if ($bCli and $this->mPreInputFiles == STDIN and $mFiles != STDIN) {
  141.             throw (new Exception("Hey, you already defined STDIN,dude"));
  142.         } elseif ($bCli and $mFiles == STDIN) {
  143.             $this->mPreInputFiles = STDIN;
  144.         } else {
  145.             // ArrayNested->off()
  146.             if (is_string($mFiles)) {
  147.                 $mFiles = array($mFiles);
  148.             }
  149.             // ArrayNested->on()
  150.             $this->mPreInputFiles = array_merge($this->mPreInputFiles, $mFiles);
  151.         }
  152.         return true;
  153.     }
  154.     /**
  155.     * Set the output file
  156.     * Could be STDOUT or a path to a file or dir (with '/' at the end)
  157.     * @param mixed STDOUT or string (path)
  158.     * @return true
  159.     */
  160.     public function setOutputFile($sFile) 
  161.     {
  162.         if (!is_string($sFile) and !(php_sapi_name() == 'cli' and $sFile == STDOUT)) {
  163.             throw (new Exception("Accept only string or STDOUT"));
  164.         }
  165.         $this->sPreOutputFile = $sFile;
  166.         return true;
  167.     }
  168.     private function setInputFilePost() 
  169.     {
  170.         $bCli = php_sapi_name() == 'cli';
  171.         // ArrayNested->off()
  172.         if ($bCli and $this->mPreInputFiles == STDIN) {
  173.             $mInputFiles = array(STDIN);
  174.         } else {
  175.             $mInputFiles = array();
  176.             foreach($this->mPreInputFiles as $sPath) {
  177.                 $mInputFiles = array_merge($mInputFiles, PHP_Beautifier_Common::getFilesByGlob($sPath, $this->bRecursive));
  178.             }
  179.         }
  180.         // now, we create stream references for compressed files....
  181.         foreach($mInputFiles as $sFile) {
  182.             // First, tar files
  183.             if (!($bCli and $sFile == STDIN) and preg_match("/(.tgz|\.tar\.gz|\.tar\.bz2|\.tar)$/", $sFile, $aMatch)) {
  184.                 if (strpos($aMatch[1], 'gz') !== FALSE) {
  185.                     $sCompress = 'gz';
  186.                 } elseif (strpos($aMatch[1], 'bz2') !== FALSE) {
  187.                     $sCompress = 'bz2';
  188.                 } elseif (strpos($aMatch[1], 'tar') !== FALSE) {
  189.                     $sCompress = false;
  190.                 }
  191.                 $oTar = new Archive_Tar($sFile, $sCompress);
  192.                 foreach($oTar->listContent() as $aInput) {
  193.                     if (empty($aInput['typeflag'])) {
  194.                         $this->mInputFiles[] = 'tarz://'.$sFile.'#'.$aInput['filename'];
  195.                     }
  196.                 }
  197.             } else {
  198.                 $this->mInputFiles[] = $sFile;
  199.             }
  200.         }
  201.         if (!$this->mInputFiles) {
  202.             throw (new Exception("Can't match any file"));
  203.         }
  204.         return true;
  205.         // ArrayNested->on()
  206.         
  207.     }
  208.     private function setOutputFilePost() 
  209.     {
  210.         if (php_sapi_name() == 'cli' and $this->sPreOutputFile == STDOUT) {
  211.             $this->sOutputMode = PHP_Beautifier_Batch::FILES;
  212.         } else {
  213.             $sPath = str_replace(DIRECTORY_SEPARATOR, '/', $this->sPreOutputFile);
  214.             if (!$sPath) {
  215.                 $sPath = "./";
  216.             }
  217.             // determine file or dir
  218.             if (substr($sPath, -1) != '/' and !is_dir($sPath)) {
  219.                 $this->sOutputMode = PHP_Beautifier_Batch::FILES;
  220.                 // Define compression mode
  221.                 if (preg_match("/\.(gz|bz2|tar)$/", $sPath, $aMatch)) {
  222.                     $this->sCompress = $aMatch[1];
  223.                 }
  224.             } else {
  225.                 $this->sOutputMode = PHP_Beautifier_Batch::DIRECTORY;
  226.             }
  227.         }
  228.         return true;
  229.     }
  230.     /**
  231.     * Create the real references to files
  232.     * @return bool
  233.     * @throws Exception
  234.     */
  235.     public function process() 
  236.     {
  237.         if (!$this->mPreInputFiles) {
  238.             throw (new Exception('Input file not defined'));
  239.         } else {
  240.             $this->setInputFilePost();
  241.             $this->setOutputFilePost();
  242.         }
  243.         if (!$this->mInputFiles) {
  244.             throw (new Exception(implode(',', $this->mPreInputFiles) ." doesn't match any files"));
  245.         } else {
  246.             return true;
  247.         }
  248.     }
  249.     private function getBatchEngine() 
  250.     {
  251.         $sCompress = ($this->sCompress) ? ucfirst($this->sCompress) : '';
  252.         $sClass = $this->sOutputMode.$sCompress;
  253.         $sClassEngine = 'PHP_Beautifier_Batch_Output_'.$sClass;
  254.         $sClassFile = PHP_Beautifier_Common::normalizeDir(dirname(__FILE__)) .'Batch/Output/'.$sClass.'.php';
  255.         if (!file_exists($sClassFile)) {
  256.             throw (new Exception("Doesn't exists file definition for $sClass ($sClassFile)"));
  257.         } else {
  258.             include_once ($sClassFile);
  259.             if (!class_exists($sClassEngine)) {
  260.                 throw (new Exception("$sClassFile exists, but $sClassEngine isn't defined"));
  261.             } else {
  262.                 return new $sClassEngine($this);
  263.             }
  264.         }
  265.     }
  266.     /**
  267.     * Save the beautified sources to file(s)
  268.     * @return bool
  269.     * @throws Exception
  270.     */
  271.     public function save($sFile = null) 
  272.     {
  273.         $oBatchEngine = $this->getBatchEngine();
  274.         return $oBatchEngine->save();
  275.     }
  276.     /**
  277.     * Return a string with the content of the file(s)
  278.     * @return string
  279.     */
  280.     public function get() 
  281.     {
  282.         $oBatchEngine = $this->getBatchEngine();
  283.         return $oBatchEngine->get();
  284.     }
  285.     public function show() 
  286.     {
  287.         echo $this->get();
  288.     }
  289.     /**
  290.     * Allows subclass of {@link PHP_Beautifier_Batch_Engine} call methods of {@link $oBeaut}
  291.     * @param    PHP_Beautifier_Batch_Engine
  292.     * @param    string method to call
  293.     * @param    array  array of args
  294.     * @return   mixed
  295.     */
  296.     public function callBeautifier(PHP_Beautifier_Batch_Output $oEngine, $sMethod, $aArgs = array()) 
  297.     {
  298.         return @call_user_func_array(array(
  299.             $this->oBeaut,
  300.             $sMethod
  301.         ) , $aArgs);
  302.     }
  303.     public function getInputFiles() 
  304.     {
  305.         return $this->mInputFiles;
  306.     }
  307.     public function getOutputPath() 
  308.     {
  309.         return $this->sPreOutputFile;
  310.     }
  311. }
  312. ?>
  313.