1<?php
2/**
3 * Base class for all installation roles.
4 *
5 * PHP versions 4 and 5
6 *
7 * @category   pear
8 * @package    PEAR
9 * @author     Greg Beaver <cellog@php.net>
10 * @copyright  1997-2006 The PHP Group
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
14 */
15/**
16 * Base class for all installation roles.
17 *
18 * This class allows extensibility of file roles.  Packages with complex
19 * customization can now provide custom file roles along with the possibility of
20 * adding configuration values to match.
21 * @category   pear
22 * @package    PEAR
23 * @author     Greg Beaver <cellog@php.net>
24 * @copyright  1997-2006 The PHP Group
25 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
26 * @version    Release: @package_version@
27 * @link       http://pear.php.net/package/PEAR
28 * @since      Class available since Release 1.4.0a1
29 */
30class PEAR_Installer_Role_Common
31{
32    /**
33     * @var PEAR_Config
34     * @access protected
35     */
36    var $config;
37
38    /**
39     * @param PEAR_Config
40     */
41    function __construct(&$config)
42    {
43        $this->config = $config;
44    }
45
46    /**
47     * Retrieve configuration information about a file role from its XML info
48     *
49     * @param string $role Role Classname, as in "PEAR_Installer_Role_Data"
50     * @return array
51     */
52    function getInfo($role)
53    {
54        if (empty($GLOBALS['_PEAR_INSTALLER_ROLES'][$role])) {
55            return PEAR::raiseError('Unknown Role class: "' . $role . '"');
56        }
57        return $GLOBALS['_PEAR_INSTALLER_ROLES'][$role];
58    }
59
60    /**
61     * This is called for each file to set up the directories and files
62     * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
63     * @param array attributes from the <file> tag
64     * @param string file name
65     * @return array an array consisting of:
66     *
67     *    1 the original, pre-baseinstalldir installation directory
68     *    2 the final installation directory
69     *    3 the full path to the final location of the file
70     *    4 the location of the pre-installation file
71     */
72    function processInstallation($pkg, $atts, $file, $tmp_path, $layer = null)
73    {
74        $roleInfo = PEAR_Installer_Role_Common::getInfo('PEAR_Installer_Role_' .
75            ucfirst(str_replace('pear_installer_role_', '', strtolower(get_class($this)))));
76        if (PEAR::isError($roleInfo)) {
77            return $roleInfo;
78        }
79        if (!$roleInfo['locationconfig']) {
80            return false;
81        }
82        if ($roleInfo['honorsbaseinstall']) {
83            $dest_dir = $save_destdir = $this->config->get($roleInfo['locationconfig'], $layer,
84                $pkg->getChannel());
85            if (!empty($atts['baseinstalldir'])) {
86                $dest_dir .= DIRECTORY_SEPARATOR . $atts['baseinstalldir'];
87            }
88        } elseif ($roleInfo['unusualbaseinstall']) {
89            $dest_dir = $save_destdir = $this->config->get($roleInfo['locationconfig'],
90                    $layer, $pkg->getChannel()) . DIRECTORY_SEPARATOR . $pkg->getPackage();
91            if (!empty($atts['baseinstalldir'])) {
92                $dest_dir .= DIRECTORY_SEPARATOR . $atts['baseinstalldir'];
93            }
94        } else {
95            $dest_dir = $save_destdir = $this->config->get($roleInfo['locationconfig'],
96                    $layer, $pkg->getChannel()) . DIRECTORY_SEPARATOR . $pkg->getPackage();
97        }
98        if (dirname($file) != '.' && empty($atts['install-as'])) {
99            $dest_dir .= DIRECTORY_SEPARATOR . dirname($file);
100        }
101        if (empty($atts['install-as'])) {
102            $dest_file = $dest_dir . DIRECTORY_SEPARATOR . basename($file);
103        } else {
104            $dest_file = $dest_dir . DIRECTORY_SEPARATOR . $atts['install-as'];
105        }
106        $orig_file = $tmp_path . DIRECTORY_SEPARATOR . $file;
107
108        // Clean up the DIRECTORY_SEPARATOR mess
109        $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
110
111        list($dest_dir, $dest_file, $orig_file) = preg_replace(array('!\\\\+!', '!/!', "!$ds2+!"),
112                                                    array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR,
113                                                          DIRECTORY_SEPARATOR),
114                                                    array($dest_dir, $dest_file, $orig_file));
115        return array($save_destdir, $dest_dir, $dest_file, $orig_file);
116    }
117
118    /**
119     * Get the name of the configuration variable that specifies the location of this file
120     * @return string|false
121     */
122    function getLocationConfig()
123    {
124        $roleInfo = PEAR_Installer_Role_Common::getInfo('PEAR_Installer_Role_' .
125            ucfirst(str_replace('pear_installer_role_', '', strtolower(get_class($this)))));
126        if (PEAR::isError($roleInfo)) {
127            return $roleInfo;
128        }
129        return $roleInfo['locationconfig'];
130    }
131
132    /**
133     * Do any unusual setup here
134     * @param PEAR_Installer
135     * @param PEAR_PackageFile_v2
136     * @param array file attributes
137     * @param string file name
138     */
139    function setup(&$installer, $pkg, $atts, $file)
140    {
141    }
142
143    function isExecutable()
144    {
145        $roleInfo = PEAR_Installer_Role_Common::getInfo('PEAR_Installer_Role_' .
146            ucfirst(str_replace('pear_installer_role_', '', strtolower(get_class($this)))));
147        if (PEAR::isError($roleInfo)) {
148            return $roleInfo;
149        }
150        return $roleInfo['executable'];
151    }
152
153    function isInstallable()
154    {
155        $roleInfo = PEAR_Installer_Role_Common::getInfo('PEAR_Installer_Role_' .
156            ucfirst(str_replace('pear_installer_role_', '', strtolower(get_class($this)))));
157        if (PEAR::isError($roleInfo)) {
158            return $roleInfo;
159        }
160        return $roleInfo['installable'];
161    }
162
163    function isExtension()
164    {
165        $roleInfo = PEAR_Installer_Role_Common::getInfo('PEAR_Installer_Role_' .
166            ucfirst(str_replace('pear_installer_role_', '', strtolower(get_class($this)))));
167        if (PEAR::isError($roleInfo)) {
168            return $roleInfo;
169        }
170        return $roleInfo['phpextension'];
171    }
172}
173?>
174