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 / phing / tasks / system / ExecTask.php < prev    next >
Encoding:
PHP Script  |  2007-08-14  |  6.5 KB  |  249 lines

  1. <?php
  2.  
  3. /*
  4.  *  $Id: ExecTask.php 215 2007-08-14 14:13:17Z mrook $
  5.  *
  6.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  10.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  12.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  13.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17.  *
  18.  * This software consists of voluntary contributions made by many individuals
  19.  * and is licensed under the LGPL. For more information please see
  20.  * <http://phing.info>.
  21.  */
  22.  
  23. require_once 'phing/Task.php';
  24.  
  25. /**
  26.  * Executes a command on the shell.
  27.  *
  28.  * @author   Andreas Aderhold <andi@binarycloud.com>
  29.  * @author   Hans Lellelid <hans@xmpl.org>
  30.  * @version  $Revision: 1.17 $
  31.  * @package  phing.tasks.system
  32.  */
  33. class ExecTask extends Task {
  34.  
  35.     /**
  36.      * Command to execute.
  37.      * @var string
  38.      */
  39.     protected $command;
  40.     
  41.     /**
  42.      * Working directory.
  43.      * @var File
  44.      */
  45.     protected $dir;
  46.     
  47.     /**
  48.      * Operating system.
  49.      * @var string
  50.      */
  51.     protected $os;
  52.     
  53.     /**
  54.      * Whether to escape shell command using escapeshellcmd().
  55.      * @var boolean
  56.      */
  57.     protected $escape = false;
  58.     
  59.     /**
  60.      * Where to direct output.
  61.      * @var File
  62.      */
  63.     protected $output;
  64.     
  65.     /**
  66.      * Whether to passthru the output
  67.      * @var boolean
  68.      */
  69.     protected $passthru = false;
  70.     
  71.     /**
  72.      * Where to direct error output.
  73.      * @var File
  74.      */
  75.     protected $error;
  76.     
  77.     /**
  78.      * If spawn is set then [unix] programs will redirect stdout and add '&'.
  79.      * @var boolean
  80.      */
  81.     protected $spawn = false;
  82.     
  83.     /**
  84.      * Whether to check the return code.
  85.      * @var boolean
  86.      */
  87.     protected $checkreturn = false;
  88.     
  89.     /**
  90.      * Main method: wraps execute() command.
  91.      * @return void
  92.      */
  93.     public function main() {
  94.         $this->execute();
  95.     }
  96.     
  97.     /**
  98.      * Executes a program and returns the return code.
  99.      * Output from command is logged at INFO level.
  100.      * @return int Return code from execution.
  101.      */
  102.     public function execute() {
  103.     
  104.          // test if os match
  105.         $myos = Phing::getProperty("os.name");
  106.         $this->log("Myos = " . $myos, Project::MSG_VERBOSE);
  107.         if (($this->os !== null) && (strpos($this->os, $myos) === false)) {
  108.             // this command will be executed only on the specified OS
  109.             $this->log("Not found in " . $this->os, Project::MSG_VERBOSE);
  110.             return 0;
  111.         }
  112.         
  113.          if ($this->dir !== null) {
  114.             if ($this->dir->isDirectory()) {
  115.                 $currdir = getcwd();
  116.                 @chdir($this->dir->getPath());
  117.             } else {
  118.                 throw new BuildException("Can't chdir to:" . $this->dir->__toString());
  119.             }
  120.         }
  121.  
  122.  
  123.         if ($this->escape == true) {
  124.             // FIXME - figure out whether this is correct behavior
  125.             $this->command = escapeshellcmd($this->command);
  126.         }
  127.         
  128.         if ($this->error !== null) {
  129.             $this->command .= ' 2> ' . $this->error->getPath();
  130.             $this->log("Writing error output to: " . $this->error->getPath());
  131.         }
  132.         
  133.         if ($this->output !== null) {
  134.             $this->command .= ' 1> ' . $this->output->getPath();
  135.             $this->log("Writing standard output to: " . $this->output->getPath());
  136.         } elseif ($this->spawn) {
  137.             $this->command .= ' 1>/dev/null';
  138.             $this->log("Sending ouptut to /dev/null");
  139.         }
  140.         
  141.         // If neither output nor error are being written to file
  142.         // then we'll redirect error to stdout so that we can dump
  143.         // it to screen below.
  144.  
  145.         if ($this->output === null && $this->error === null) {
  146.             $this->command .= ' 2>&1';
  147.         }
  148.                 
  149.         // we ignore the spawn boolean for windows
  150.         if ($this->spawn) {
  151.             $this->command .= ' &';
  152.         }
  153.  
  154.         $this->log("Executing command: " . $this->command);
  155.                 
  156.         $output = array();
  157.         $return = null;
  158.         exec($this->command, $output, $return);
  159.  
  160.         if ($this->dir !== null) {
  161.             @chdir($currdir);
  162.         }
  163.  
  164.         foreach($output as $line) {
  165.             $this->log($line,  ($this->passthru ? Project::MSG_INFO : Project::MSG_VERBOSE));
  166.         }
  167.         
  168.         if($return != 0 && $this->checkreturn)
  169.         {
  170.           throw new BuildException("Task exited with code $return");
  171.         }
  172.         
  173.         return $return;
  174.     }
  175.  
  176.     /**
  177.      * The command to use.
  178.      * @param mixed $command String or string-compatible (e.g. w/ __toString()).
  179.      */
  180.     function setCommand($command) {
  181.         $this->command = "" . $command;
  182.     }
  183.     
  184.     /**
  185.      * Whether to use escapeshellcmd() to escape command.
  186.      * @param boolean $escape
  187.      */
  188.     function setEscape($escape) {
  189.         $this->escape = (bool) $escape;
  190.     }
  191.     
  192.     /**
  193.      * Specify the working directory for executing this command.
  194.      * @param PhingFile $dir
  195.      */
  196.     function setDir(PhingFile $dir) {
  197.         $this->dir = $dir;
  198.     }
  199.     
  200.     /**
  201.      * Specify OS (or muliple OS) that must match in order to execute this command.
  202.      * @param string $os
  203.      */
  204.     function setOs($os) {
  205.         $this->os = (string) $os;
  206.     }
  207.     
  208.     /**
  209.      * File to which output should be written.
  210.      * @param PhingFile $output
  211.      */
  212.     function setOutput(PhingFile $f) {
  213.         $this->output = $f;
  214.     }
  215.     
  216.     /**
  217.      * File to which error output should be written.
  218.      * @param PhingFile $output
  219.      */
  220.     function setError(PhingFile $f) {
  221.         $this->error = $f;
  222.     }
  223.     
  224.     /**
  225.      * Whether to use passthru the output.
  226.      * @param boolean $passthru
  227.      */
  228.     function setPassthru($passthru) {
  229.         $this->passthru = (bool) $passthru;
  230.     }
  231.     
  232.     /**
  233.      * Whether to suppress all output and run in the background.
  234.      * @param boolean $spawn
  235.      */
  236.     function setSpawn($spawn) {
  237.         $this->spawn  = (bool) $spawn;
  238.     }
  239.  
  240.     /**
  241.      * Whether to check the return code.
  242.      * @param boolean $checkreturn
  243.      */
  244.     function setCheckreturn($checkreturn) {
  245.       $this->checkreturn = (bool) $checkreturn;
  246.     }
  247. }
  248.  
  249.