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 * @link http://pear.php.net/package/PEAR
13 * @since File available since Release 1.4.0a1
17 * Include error handling
19 //require_once 'PEAR.php';
22 * Which user interface class is being used.
23 * @var string class name
25 $GLOBALS['_PEAR_FRONTEND_CLASS'] = 'PEAR_Frontend_CLI';
28 * Instance of $_PEAR_Command_uiclass.
31 $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = null;
34 * Singleton-based frontend for PEAR user input/output
38 * @author Greg Beaver <cellog@php.net>
39 * @copyright 1997-2009 The Authors
40 * @license http://opensource.org/licenses/bsd-license.php New BSD License
41 * @version Release: 1.10.1
42 * @link http://pear.php.net/package/PEAR
43 * @since Class available since Release 1.4.0a1
45 class PEAR_Frontend extends PEAR
48 * Retrieve the frontend object
49 * @return PEAR_Frontend_CLI|PEAR_Frontend_Web|PEAR_Frontend_Gtk
51 public static function &singleton($type = null)
54 if (!isset($GLOBALS['_PEAR_FRONTEND_SINGLETON'])) {
58 return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
61 $a = PEAR_Frontend::setFrontendClass($type);
66 * Set the frontend class that will be used by calls to {@link singleton()}
68 * Frontends are expected to conform to the PEAR naming standard of
69 * _ => DIRECTORY_SEPARATOR (PEAR_Frontend_CLI is in PEAR/Frontend/CLI.php)
70 * @param string $uiclass full class name
71 * @return PEAR_Frontend
73 public static function &setFrontendClass($uiclass)
75 if (is_object($GLOBALS['_PEAR_FRONTEND_SINGLETON']) &&
76 is_a($GLOBALS['_PEAR_FRONTEND_SINGLETON'], $uiclass)) {
77 return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
80 if (!class_exists($uiclass)) {
81 $file = str_replace('_', '/', $uiclass) . '.php';
82 if (PEAR_Frontend::isIncludeable($file)) {
87 if (class_exists($uiclass)) {
89 // quick test to see if this class implements a few of the most
90 // important frontend methods
91 if (is_a($obj, 'PEAR_Frontend')) {
92 $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = &$obj;
93 $GLOBALS['_PEAR_FRONTEND_CLASS'] = $uiclass;
97 $err = PEAR::raiseError("not a frontend class: $uiclass");
101 $err = PEAR::raiseError("no such class: $uiclass");
106 * Set the frontend class that will be used by calls to {@link singleton()}
108 * Frontends are expected to be a descendant of PEAR_Frontend
109 * @param PEAR_Frontend
110 * @return PEAR_Frontend
112 public static function &setFrontendObject($uiobject)
114 if (is_object($GLOBALS['_PEAR_FRONTEND_SINGLETON']) &&
115 is_a($GLOBALS['_PEAR_FRONTEND_SINGLETON'], get_class($uiobject))) {
116 return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
119 if (!is_a($uiobject, 'PEAR_Frontend')) {
120 $err = PEAR::raiseError('not a valid frontend class: (' .
121 get_class($uiobject) . ')');
125 $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = &$uiobject;
126 $GLOBALS['_PEAR_FRONTEND_CLASS'] = get_class($uiobject);
131 * @param string $path relative or absolute include path
134 public static function isIncludeable($path)
136 if (file_exists($path) && is_readable($path)) {
140 $fp = @fopen($path, 'r', true);
152 function setConfig(&$config)
157 * This can be overridden to allow session-based temporary file management
159 * By default, all files are deleted at the end of a session. The web installer
160 * needs to be able to sustain a list over many sessions in order to support
161 * user interaction with install scripts
163 function addTempFile($file)
165 $GLOBALS['_PEAR_Common_tempfiles'][] = $file;
171 * @param string $msg the message to log
172 * @param boolean $append_crlf
173 * @return boolean true
176 function log($msg, $append_crlf = true)
181 * Run a post-installation script
183 * @param array $scripts array of post-install scripts
186 function runPostinstallScripts(&$scripts)
191 * Display human-friendly output formatted depending on the
192 * $command parameter.
194 * This should be able to handle basic output data with no command
195 * @param mixed $data data structure containing the information to display
196 * @param string $command command from which this method was called
199 function outputData($data, $command = '_default')
204 * Display a modal form dialog and return the given input
206 * A frontend that requires multiple requests to retrieve and process
207 * data must take these needs into account, and implement the request
209 * @param string $command command from which this method was called
210 * @param array $prompts associative array. keys are the input field names
211 * and values are the description
212 * @param array $types array of input field types (text, password,
213 * etc.) keys have to be the same like in $prompts
214 * @param array $defaults array of default values. again keys have
215 * to be the same like in $prompts. Do not depend
216 * on a default value being set.
217 * @return array input sent by the user
220 function userDialog($command, $prompts, $types = array(), $defaults = array())