3 * PEAR_Frontend, the singleton-based frontend for user input/output
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: Frontend.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
18 * Include error handling
20 //require_once 'PEAR.php';
23 * Which user interface class is being used.
24 * @var string class name
26 $GLOBALS['_PEAR_FRONTEND_CLASS'] = 'PEAR_Frontend_CLI';
29 * Instance of $_PEAR_Command_uiclass.
32 $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = null;
35 * Singleton-based frontend for PEAR user input/output
39 * @author Greg Beaver <cellog@php.net>
40 * @copyright 1997-2009 The Authors
41 * @license http://opensource.org/licenses/bsd-license.php New BSD License
42 * @version Release: 1.9.4
43 * @link http://pear.php.net/package/PEAR
44 * @since Class available since Release 1.4.0a1
46 class PEAR_Frontend extends PEAR
49 * Retrieve the frontend object
50 * @return PEAR_Frontend_CLI|PEAR_Frontend_Web|PEAR_Frontend_Gtk
53 function &singleton($type = null)
56 if (!isset($GLOBALS['_PEAR_FRONTEND_SINGLETON'])) {
60 return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
63 $a = PEAR_Frontend::setFrontendClass($type);
68 * Set the frontend class that will be used by calls to {@link singleton()}
70 * Frontends are expected to conform to the PEAR naming standard of
71 * _ => DIRECTORY_SEPARATOR (PEAR_Frontend_CLI is in PEAR/Frontend/CLI.php)
72 * @param string $uiclass full class name
73 * @return PEAR_Frontend
76 function &setFrontendClass($uiclass)
78 if (is_object($GLOBALS['_PEAR_FRONTEND_SINGLETON']) &&
79 is_a($GLOBALS['_PEAR_FRONTEND_SINGLETON'], $uiclass)) {
80 return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
83 if (!class_exists($uiclass)) {
84 $file = str_replace('_', '/', $uiclass) . '.php';
85 if (PEAR_Frontend::isIncludeable($file)) {
90 if (class_exists($uiclass)) {
92 // quick test to see if this class implements a few of the most
93 // important frontend methods
94 if (is_a($obj, 'PEAR_Frontend')) {
95 $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = &$obj;
96 $GLOBALS['_PEAR_FRONTEND_CLASS'] = $uiclass;
100 $err = PEAR::raiseError("not a frontend class: $uiclass");
104 $err = PEAR::raiseError("no such class: $uiclass");
109 * Set the frontend class that will be used by calls to {@link singleton()}
111 * Frontends are expected to be a descendant of PEAR_Frontend
112 * @param PEAR_Frontend
113 * @return PEAR_Frontend
116 function &setFrontendObject($uiobject)
118 if (is_object($GLOBALS['_PEAR_FRONTEND_SINGLETON']) &&
119 is_a($GLOBALS['_PEAR_FRONTEND_SINGLETON'], get_class($uiobject))) {
120 return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
123 if (!is_a($uiobject, 'PEAR_Frontend')) {
124 $err = PEAR::raiseError('not a valid frontend class: (' .
125 get_class($uiobject) . ')');
129 $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = &$uiobject;
130 $GLOBALS['_PEAR_FRONTEND_CLASS'] = get_class($uiobject);
135 * @param string $path relative or absolute include path
139 function isIncludeable($path)
141 if (file_exists($path) && is_readable($path)) {
145 $fp = @fopen($path, 'r', true);
157 function setConfig(&$config)
162 * This can be overridden to allow session-based temporary file management
164 * By default, all files are deleted at the end of a session. The web installer
165 * needs to be able to sustain a list over many sessions in order to support
166 * user interaction with install scripts
168 function addTempFile($file)
170 $GLOBALS['_PEAR_Common_tempfiles'][] = $file;
176 * @param string $msg the message to log
177 * @param boolean $append_crlf
178 * @return boolean true
181 function log($msg, $append_crlf = true)
186 * Run a post-installation script
188 * @param array $scripts array of post-install scripts
191 function runPostinstallScripts(&$scripts)
196 * Display human-friendly output formatted depending on the
197 * $command parameter.
199 * This should be able to handle basic output data with no command
200 * @param mixed $data data structure containing the information to display
201 * @param string $command command from which this method was called
204 function outputData($data, $command = '_default')
209 * Display a modal form dialog and return the given input
211 * A frontend that requires multiple requests to retrieve and process
212 * data must take these needs into account, and implement the request
214 * @param string $command command from which this method was called
215 * @param array $prompts associative array. keys are the input field names
216 * and values are the description
217 * @param array $types array of input field types (text, password,
218 * etc.) keys have to be the same like in $prompts
219 * @param array $defaults array of default values. again keys have
220 * to be the same like in $prompts. Do not depend
221 * on a default value being set.
222 * @return array input sent by the user
225 function userDialog($command, $prompts, $types = array(), $defaults = array())