PEAR PHP Cross Reference Developer Tools

Source: /PEAR/Task/Common.php - 202 lines - 6110 bytes - Summary - Text - Print

Description: PEAR_Task_Common, base class for installer tasks PHP versions 4 and 5

   1  <?php
   2  /**
   3   * PEAR_Task_Common, base class for installer tasks
   4   *
   5   * PHP versions 4 and 5
   6   *
   7   * @category   pear
   8   * @package    PEAR
   9   * @author     Greg Beaver <cellog@php.net>
  10   * @copyright  1997-2009 The Authors
  11   * @license    http://opensource.org/licenses/bsd-license.php New BSD License
  12   * @version    CVS: $Id: Common.php 313023 2011-07-06 19:17:11Z dufuz $
  13   * @link       http://pear.php.net/package/PEAR
  14   * @since      File available since Release 1.4.0a1
  15   */
  16  /**#@+
  17   * Error codes for task validation routines
  18   */
  19  define('PEAR_TASK_ERROR_NOATTRIBS', 1);
  20  define('PEAR_TASK_ERROR_MISSING_ATTRIB', 2);
  21  define('PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE', 3);
  22  define('PEAR_TASK_ERROR_INVALID', 4);
  23  /**#@-*/
  24  define('PEAR_TASK_PACKAGE', 1);
  25  define('PEAR_TASK_INSTALL', 2);
  26  define('PEAR_TASK_PACKAGEANDINSTALL', 3);
  27  /**
  28   * A task is an operation that manipulates the contents of a file.
  29   *
  30   * Simple tasks operate on 1 file.  Multiple tasks are executed after all files have been
  31   * processed and installed, and are designed to operate on all files containing the task.
  32   * The Post-install script task simply takes advantage of the fact that it will be run
  33   * after installation, replace is a simple task.
  34   *
  35   * Combining tasks is possible, but ordering is significant.
  36   *
  37   * <file name="test.php" role="php">
  38   *  <tasks:replace from="@data-dir@" to="data_dir" type="pear-config"/>
  39   *  <tasks:postinstallscript/>
  40   * </file>
  41   *
  42   * This will first replace any instance of @data-dir@ in the test.php file
  43   * with the path to the current data directory.  Then, it will include the
  44   * test.php file and run the script it contains to configure the package post-installation.
  45   * @category   pear
  46   * @package    PEAR
  47   * @author     Greg Beaver <cellog@php.net>
  48   * @copyright  1997-2009 The Authors
  49   * @license    http://opensource.org/licenses/bsd-license.php New BSD License
  50   * @version    Release: 1.9.4
  51   * @link       http://pear.php.net/package/PEAR
  52   * @since      Class available since Release 1.4.0a1
  53   * @abstract
  54   */
  55  class PEAR_Task_Common
  56  {
  57      /**
  58       * Valid types for this version are 'simple' and 'multiple'
  59       *
  60       * - simple tasks operate on the contents of a file and write out changes to disk
  61       * - multiple tasks operate on the contents of many files and write out the
  62       *   changes directly to disk
  63       *
  64       * Child task classes must override this property.
  65       * @access protected
  66       */
  67      var $type = 'simple';
  68      /**
  69       * Determines which install phase this task is executed under
  70       */
  71      var $phase = PEAR_TASK_INSTALL;
  72      /**
  73       * @access protected
  74       */
  75      var $config;
  76      /**
  77       * @access protected
  78       */
  79      var $registry;
  80      /**
  81       * @access protected
  82       */
  83      var $logger;
  84      /**
  85       * @access protected
  86       */
  87      var $installphase;
  88      /**
  89       * @param PEAR_Config
  90       * @param PEAR_Common
  91       */
  92      function PEAR_Task_Common(&$config, &$logger, $phase)
  93      {
  94          $this->config = &$config;
  95          $this->registry = &$config->getRegistry();
  96          $this->logger = &$logger;
  97          $this->installphase = $phase;
  98          if ($this->type == 'multiple') {
  99              $GLOBALS['_PEAR_TASK_POSTINSTANCES'][get_class($this)][] = &$this;
 100          }
 101      }
 102  
 103      /**
 104       * Validate the basic contents of a task tag.
 105       * @param PEAR_PackageFile_v2
 106       * @param array
 107       * @param PEAR_Config
 108       * @param array the entire parsed <file> tag
 109       * @return true|array On error, return an array in format:
 110       *    array(PEAR_TASK_ERROR_???[, param1][, param2][, ...])
 111       *
 112       *    For PEAR_TASK_ERROR_MISSING_ATTRIB, pass the attribute name in
 113       *    For PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, pass the attribute name and an array
 114       *    of legal values in
 115       * @static
 116       * @abstract
 117       */
 118      function validateXml($pkg, $xml, $config, $fileXml)
 119      {
 120      }
 121  
 122      /**
 123       * Initialize a task instance with the parameters
 124       * @param array raw, parsed xml
 125       * @param array attributes from the <file> tag containing this task
 126       * @param string|null last installed version of this package
 127       * @abstract
 128       */
 129      function init($xml, $fileAttributes, $lastVersion)
 130      {
 131      }
 132  
 133      /**
 134       * Begin a task processing session.  All multiple tasks will be processed after each file
 135       * has been successfully installed, all simple tasks should perform their task here and
 136       * return any errors using the custom throwError() method to allow forward compatibility
 137       *
 138       * This method MUST NOT write out any changes to disk
 139       * @param PEAR_PackageFile_v2
 140       * @param string file contents
 141       * @param string the eventual final file location (informational only)
 142       * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail
 143       *         (use $this->throwError), otherwise return the new contents
 144       * @abstract
 145       */
 146      function startSession($pkg, $contents, $dest)
 147      {
 148      }
 149  
 150      /**
 151       * This method is used to process each of the tasks for a particular multiple class
 152       * type.  Simple tasks need not implement this method.
 153       * @param array an array of tasks
 154       * @access protected
 155       * @static
 156       * @abstract
 157       */
 158      function run($tasks)
 159      {
 160      }
 161  
 162      /**
 163       * @static
 164       * @final
 165       */
 166      function hasPostinstallTasks()
 167      {
 168          return isset($GLOBALS['_PEAR_TASK_POSTINSTANCES']);
 169      }
 170  
 171      /**
 172       * @static
 173       * @final
 174       */
 175       function runPostinstallTasks()
 176       {
 177           foreach ($GLOBALS['_PEAR_TASK_POSTINSTANCES'] as $class => $tasks) {
 178               $err = call_user_func(array($class, 'run'),
 179                    $GLOBALS['_PEAR_TASK_POSTINSTANCES'][$class]);
 180               if ($err) {
 181                   return PEAR_Task_Common::throwError($err);
 182               }
 183           }
 184           unset($GLOBALS['_PEAR_TASK_POSTINSTANCES']);
 185      }
 186  
 187      /**
 188       * Determines whether a role is a script
 189       * @return bool
 190       */
 191      function isScript()
 192      {
 193          return $this->type == 'script';
 194      }
 195  
 196      function throwError($msg, $code = -1)
 197      {
 198          include_once  'PEAR.php';
 199          return PEAR::raiseError($msg, $code);
 200      }
 201  }
 202  ?>

title

Description

title

Description

title

Description

title

title

Body