Frontend.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * PEAR_Frontend, the singleton-based frontend for user input/output
  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. * @link http://pear.php.net/package/PEAR
  13. * @since File available since Release 1.4.0a1
  14. */
  15. /**
  16. * Include error handling
  17. */
  18. //require_once 'PEAR.php';
  19. /**
  20. * Which user interface class is being used.
  21. * @var string class name
  22. */
  23. $GLOBALS['_PEAR_FRONTEND_CLASS'] = 'PEAR_Frontend_CLI';
  24. /**
  25. * Instance of $_PEAR_Command_uiclass.
  26. * @var object
  27. */
  28. $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = null;
  29. /**
  30. * Singleton-based frontend for PEAR user input/output
  31. *
  32. * @category pear
  33. * @package PEAR
  34. * @author Greg Beaver <cellog@php.net>
  35. * @copyright 1997-2009 The Authors
  36. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  37. * @version Release: 1.10.10
  38. * @link http://pear.php.net/package/PEAR
  39. * @since Class available since Release 1.4.0a1
  40. */
  41. class PEAR_Frontend extends PEAR
  42. {
  43. /**
  44. * Retrieve the frontend object
  45. * @return PEAR_Frontend_CLI|PEAR_Frontend_Web|PEAR_Frontend_Gtk
  46. */
  47. public static function &singleton($type = null)
  48. {
  49. if ($type === null) {
  50. if (!isset($GLOBALS['_PEAR_FRONTEND_SINGLETON'])) {
  51. $a = false;
  52. return $a;
  53. }
  54. return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
  55. }
  56. $a = PEAR_Frontend::setFrontendClass($type);
  57. return $a;
  58. }
  59. /**
  60. * Set the frontend class that will be used by calls to {@link singleton()}
  61. *
  62. * Frontends are expected to conform to the PEAR naming standard of
  63. * _ => DIRECTORY_SEPARATOR (PEAR_Frontend_CLI is in PEAR/Frontend/CLI.php)
  64. * @param string $uiclass full class name
  65. * @return PEAR_Frontend
  66. */
  67. public static function &setFrontendClass($uiclass)
  68. {
  69. if (is_object($GLOBALS['_PEAR_FRONTEND_SINGLETON']) &&
  70. is_a($GLOBALS['_PEAR_FRONTEND_SINGLETON'], $uiclass)) {
  71. return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
  72. }
  73. if (!class_exists($uiclass)) {
  74. $file = str_replace('_', '/', $uiclass) . '.php';
  75. if (PEAR_Frontend::isIncludeable($file)) {
  76. include_once $file;
  77. }
  78. }
  79. if (class_exists($uiclass)) {
  80. $obj = new $uiclass;
  81. // quick test to see if this class implements a few of the most
  82. // important frontend methods
  83. if (is_a($obj, 'PEAR_Frontend')) {
  84. $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = &$obj;
  85. $GLOBALS['_PEAR_FRONTEND_CLASS'] = $uiclass;
  86. return $obj;
  87. }
  88. $err = PEAR::raiseError("not a frontend class: $uiclass");
  89. return $err;
  90. }
  91. $err = PEAR::raiseError("no such class: $uiclass");
  92. return $err;
  93. }
  94. /**
  95. * Set the frontend class that will be used by calls to {@link singleton()}
  96. *
  97. * Frontends are expected to be a descendant of PEAR_Frontend
  98. * @param PEAR_Frontend
  99. * @return PEAR_Frontend
  100. */
  101. public static function &setFrontendObject($uiobject)
  102. {
  103. if (is_object($GLOBALS['_PEAR_FRONTEND_SINGLETON']) &&
  104. is_a($GLOBALS['_PEAR_FRONTEND_SINGLETON'], get_class($uiobject))) {
  105. return $GLOBALS['_PEAR_FRONTEND_SINGLETON'];
  106. }
  107. if (!is_a($uiobject, 'PEAR_Frontend')) {
  108. $err = PEAR::raiseError('not a valid frontend class: (' .
  109. get_class($uiobject) . ')');
  110. return $err;
  111. }
  112. $GLOBALS['_PEAR_FRONTEND_SINGLETON'] = &$uiobject;
  113. $GLOBALS['_PEAR_FRONTEND_CLASS'] = get_class($uiobject);
  114. return $uiobject;
  115. }
  116. /**
  117. * @param string $path relative or absolute include path
  118. * @return boolean
  119. */
  120. public static function isIncludeable($path)
  121. {
  122. if (file_exists($path) && is_readable($path)) {
  123. return true;
  124. }
  125. $fp = @fopen($path, 'r', true);
  126. if ($fp) {
  127. fclose($fp);
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * @param PEAR_Config
  134. */
  135. function setConfig(&$config)
  136. {
  137. }
  138. /**
  139. * This can be overridden to allow session-based temporary file management
  140. *
  141. * By default, all files are deleted at the end of a session. The web installer
  142. * needs to be able to sustain a list over many sessions in order to support
  143. * user interaction with install scripts
  144. */
  145. static function addTempFile($file)
  146. {
  147. $GLOBALS['_PEAR_Common_tempfiles'][] = $file;
  148. }
  149. /**
  150. * Log an action
  151. *
  152. * @param string $msg the message to log
  153. * @param boolean $append_crlf
  154. * @return boolean true
  155. * @abstract
  156. */
  157. function log($msg, $append_crlf = true)
  158. {
  159. }
  160. /**
  161. * Run a post-installation script
  162. *
  163. * @param array $scripts array of post-install scripts
  164. * @abstract
  165. */
  166. function runPostinstallScripts(&$scripts)
  167. {
  168. }
  169. /**
  170. * Display human-friendly output formatted depending on the
  171. * $command parameter.
  172. *
  173. * This should be able to handle basic output data with no command
  174. * @param mixed $data data structure containing the information to display
  175. * @param string $command command from which this method was called
  176. * @abstract
  177. */
  178. function outputData($data, $command = '_default')
  179. {
  180. }
  181. /**
  182. * Display a modal form dialog and return the given input
  183. *
  184. * A frontend that requires multiple requests to retrieve and process
  185. * data must take these needs into account, and implement the request
  186. * handling code.
  187. * @param string $command command from which this method was called
  188. * @param array $prompts associative array. keys are the input field names
  189. * and values are the description
  190. * @param array $types array of input field types (text, password,
  191. * etc.) keys have to be the same like in $prompts
  192. * @param array $defaults array of default values. again keys have
  193. * to be the same like in $prompts. Do not depend
  194. * on a default value being set.
  195. * @return array input sent by the user
  196. * @abstract
  197. */
  198. function userDialog($command, $prompts, $types = array(), $defaults = array())
  199. {
  200. }
  201. }