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 / ext / svn / SvnBaseTask.php next >
Encoding:
PHP Script  |  2007-11-02  |  6.5 KB  |  306 lines

  1. <?php
  2. /*
  3.  *  $Id: SvnBaseTask.php 281 2007-11-02 09:17:51Z hans $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://phing.info>.
  20.  */
  21.  
  22. include_once 'phing/Task.php';
  23.  
  24. /**
  25.  * Base class for Subversion tasks
  26.  *
  27.  * @author Michiel Rook <michiel.rook@gmail.com>
  28.  * @author Andrew Eddie <andrew.eddie@jamboworks.com> 
  29.  * @version $Id: SvnBaseTask.php 281 2007-11-02 09:17:51Z hans $
  30.  * @package phing.tasks.ext.svn
  31.  * @see VersionControl_SVN
  32.  * @since 2.2.0
  33.  */
  34. abstract class SvnBaseTask extends Task
  35. {
  36.     private $workingCopy = "";
  37.     
  38.     private $repositoryUrl = "";
  39.     
  40.     private $svnPath = "/usr/bin/svn";
  41.     
  42.     private $svn = NULL;
  43.     
  44.     private $mode = "";
  45.     
  46.     private $svnArgs = array();
  47.     
  48.     private $svnSwitches = array();
  49.  
  50.     private $toDir = "";
  51.  
  52.     /**
  53.      * Initialize Task.
  54.       * This method includes any necessary SVN libraries and triggers
  55.      * appropriate error if they cannot be found.  This is not done in header
  56.      * because we may want this class to be loaded w/o triggering an error.
  57.      */
  58.     function init() {
  59.         include_once 'VersionControl/SVN.php';
  60.         if (!class_exists('VersionControl_SVN')) {
  61.             throw new Exception("SvnLastRevisionTask depends on PEAR VersionControl_SVN package being installed.");
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Sets the path to the workingcopy
  67.      */
  68.     function setWorkingCopy($workingCopy)
  69.     {
  70.         $this->workingCopy = $workingCopy;
  71.     }
  72.  
  73.     /**
  74.      * Returns the path to the workingcopy
  75.      */
  76.     function getWorkingCopy()
  77.     {
  78.         return $this->workingCopy;
  79.     }
  80.  
  81.     /**
  82.      * Sets the path/URI to the repository
  83.      */
  84.     function setRepositoryUrl($repositoryUrl)
  85.     {
  86.         $this->repositoryUrl = $repositoryUrl;
  87.     }
  88.  
  89.     /**
  90.      * Returns the path/URI to the repository
  91.      */
  92.     function getRepositoryUrl()
  93.     {
  94.         return $this->repositoryUrl;
  95.     }
  96.  
  97.     /**
  98.      * Sets the path to the SVN executable
  99.      */
  100.     function setSvnPath($svnPath)
  101.     {
  102.         $this->svnPath = $svnPath;
  103.     }
  104.  
  105.     /**
  106.      * Returns the path to the SVN executable
  107.      */
  108.     function getSvnPath()
  109.     {
  110.         return $this->svnPath;
  111.     }
  112.  
  113.     //
  114.     // Args
  115.     //
  116.  
  117.     /**
  118.      * Sets the path to export/checkout to
  119.      */
  120.     function setToDir($toDir)
  121.     {
  122.         $this->toDir = $toDir;
  123.     }
  124.  
  125.     /**
  126.      * Returns the path to export/checkout to
  127.      */
  128.     function getToDir()
  129.     {
  130.         return $this->toDir;
  131.     }
  132.  
  133.     //
  134.     // Switches
  135.     //
  136.  
  137.     /**
  138.      * Sets the force switch
  139.      */
  140.     function setForce($value)
  141.     {
  142.         $this->svnSwitches['force'] = $value;
  143.     }
  144.  
  145.     /**
  146.      * Returns the forec switch
  147.      */
  148.     function getForce()
  149.     {
  150.         return isset( $this->svnSwitches['force'] ) ? $this->svnSwitches['force'] : '';
  151.     }
  152.  
  153.     /**
  154.      * Sets the username of the user to export
  155.      */
  156.     function setUsername($value)
  157.     {
  158.         $this->svnSwitches['username'] = $value;
  159.     }
  160.  
  161.     /**
  162.      * Returns the username
  163.      */
  164.     function getUsername()
  165.     {
  166.         return isset( $this->svnSwitches['username'] ) ? $this->svnSwitches['username'] : '';
  167.     }
  168.  
  169.     /**
  170.      * Sets the password of the user to export
  171.      */
  172.     function setPassword($value)
  173.     {
  174.         $this->svnSwitches['password'] = $value;
  175.     }
  176.  
  177.     /**
  178.      * Returns the password
  179.      */
  180.     function getPassword()
  181.     {
  182.         return isset( $this->svnSwitches['password'] ) ? $this->svnSwitches['password'] : '';
  183.     }
  184.  
  185.     /**
  186.      * Sets the password of the user to export
  187.      */
  188.     function setNoCache($value)
  189.     {
  190.         $this->svnSwitches['no-auth-cache'] = $value;
  191.     }
  192.  
  193.     /**
  194.      * Returns the password
  195.      */
  196.     function getNoCache()
  197.     {
  198.         return isset( $this->svnSwitches['no-auth-cache'] ) ? $this->svnSwitches['no-auth-cache'] : '';
  199.     }
  200.     
  201.     /**
  202.      * Toggles recursive behavior
  203.      */
  204.     function setRecursive($value)
  205.     {
  206.         $this->svnSwitches['non-recursive'] = is_bool($value) ? !$value : TRUE;
  207.     }
  208.     
  209.     /**
  210.      * Returns status of recursive behavior
  211.      */
  212.     function getRecursive()
  213.     {
  214.         return isset( $this->svnSwitches['non-recursive'] ) ? $this->svnSwitches['non-recursive'] : '';
  215.     }
  216.     
  217.     /**
  218.      * Creates a VersionControl_SVN class based on $mode
  219.      *
  220.      * @param string The SVN mode to use (info, export, checkout, ...)
  221.      * @throws BuildException
  222.      */
  223.     protected function setup($mode)
  224.     {
  225.         $this->mode = $mode;
  226.         
  227.         // Set up runtime options. Will be passed to all
  228.         // subclasses.
  229.         $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_ASSOC, 'svn_path' => $this->getSvnPath());
  230.         
  231.         // Pass array of subcommands we need to factory
  232.         $this->svn = VersionControl_SVN::factory($mode, $options);
  233.  
  234.         if (!empty($this->repositoryUrl))
  235.         {
  236.             $this->svnArgs = array($this->repositoryUrl);
  237.         }
  238.         else
  239.         if (!empty($this->workingCopy))
  240.         {
  241.             if (is_dir($this->workingCopy))
  242.             {
  243.                 if (in_array(".svn", scandir($this->workingCopy)))
  244.                 {
  245.                     $this->svnArgs = array($this->workingCopy);
  246.                 }
  247.                 else
  248.                 {
  249.                     throw new BuildException("'".$this->workingCopy."' doesn't seem to be a working copy");
  250.                 }
  251.             }
  252.             else
  253.             if ($mode=='info' )
  254.             {
  255.                 if (is_file($this->workingCopy))
  256.                 {
  257.                     $this->svnArgs = array($this->workingCopy);
  258.                 }
  259.                 else
  260.                 {
  261.                     throw new BuildException("'".$this->workingCopy."' is not a directory nor a file");
  262.                 }
  263.             }
  264.             else
  265.             {
  266.                 throw new BuildException("'".$this->workingCopy."' is not a directory");
  267.             }
  268.         }
  269.     }
  270.     
  271.     /**
  272.      * Executes the constructed VersionControl_SVN instance
  273.      *
  274.      * @param array Additional arguments to pass to SVN.
  275.      * @param array Switches to pass to SVN.
  276.      * @return string Output generated by SVN.
  277.      */
  278.     protected function run($args = array(), $switches = array())
  279.     {
  280.         $svnstack = PEAR_ErrorStack::singleton('VersionControl_SVN');
  281.         
  282.         $tempArgs = $this->svnArgs;
  283.         
  284.         $tempArgs = array_merge($tempArgs, $args);
  285.  
  286.         $tempSwitches = $this->svnSwitches;
  287.         
  288.         $tempSwitches = array_merge($tempSwitches, $switches);
  289.  
  290.         if ($output = $this->svn->run($tempArgs, $tempSwitches))
  291.         {
  292.             return $output;
  293.         }
  294.         else
  295.         {
  296.             if (count($errs = $svnstack->getErrors()))
  297.             {
  298.                 $err = current($errs);
  299.  
  300.                 throw new BuildException("Failed to run the 'svn " . $this->mode . "' command: " . $err['message']);
  301.             }
  302.         }
  303.     }
  304. }
  305. ?>
  306.