Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

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