Source for file StartupCheck.class.php

Documentation is available at StartupCheck.class.php

  1. <?php
  2. /**
  3. *
  4. * @package polyphony.startupcheck
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: StartupCheck.class.php,v 1.12 2007/09/19 14:04:49 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * include our StartupRequirement interface
  14. *
  15. */
  16. require_once POLYPHONY."/main/library/StartupCheck/StartupRequirement.interface.php";
  17.  
  18. /**
  19. * then include our common requirements
  20. *
  21. */
  22. require_once POLYPHONY."/main/library/StartupCheck/CommonRequirements/include.php";
  23.  
  24. /**
  25. * Says requirement is met.
  26. * @name STARTUP_STATUS_OK
  27. */
  28. define("STARTUP_STATUS_OK", 2);
  29.  
  30. /**
  31. * Says the requirement is not met and that there is an internal error that can't be fixed.
  32. * @name STARTUP_STATUS_ERROR
  33. */
  34. define("STARTUP_STATUS_ERROR", 3);
  35.  
  36. /**
  37. * Says the requirement will be met after doing some internal updating.
  38. * @name STARTUP_STATUS_NEEDS_UPDATE
  39. */
  40. define("STARTUP_STATUS_NEEDS_UPDATE", 1);
  41.  
  42. /**
  43. * Says the requirement will need user input before it can be updated.
  44. * @name STARTUP_STATUS_NEEDS_USER_INPUT
  45. */
  46. define("STARTUP_STATUS_NEEDS_USER_INPUT", 5);
  47.  
  48. /**
  49. * Says the requirement has not yet been checked.
  50. * @name STARTUP_STATUS_NOT_CHECKED
  51. */
  52. define("STARTUP_STATUS_NOT_CHECKED", 4);
  53.  
  54. /**
  55. * Says the requirement will be met after installing/configuring some system components.
  56. * @name STARTUP_STATUS_NEEDS_INSTALL
  57. */
  58. define("STARTUP_STATUS_NEEDS_INSTALL", 0);
  59.  
  60. /**
  61. *
  62. * @package polyphony.startupcheck
  63. *
  64. * @copyright Copyright &copy; 2005, Middlebury College
  65. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  66. *
  67. * @version $Id: StartupCheck.class.php,v 1.12 2007/09/19 14:04:49 adamfranco Exp $
  68. */
  69. class StartupCheck {
  70.  
  71. /**
  72. * @var array $_status Keeps a record of what parts of the program need updating.
  73. * @access private
  74. ***/
  75. var $_status;
  76.  
  77. /**
  78. * @var array $_requirements An array of {@link StartupRequirements}.
  79. * @access private
  80. ***/
  81. var $_requirements;
  82.  
  83. /**
  84. * @var string $_currentRequirement The name of the requirement we are in the middle of configuring (with user input).
  85. * @access private
  86. ***/
  87. var $_currentRequirement;
  88.  
  89. /**
  90. * @var object $_currentWizard The {@link Wizard} object associated with the $_currentRequirement.
  91. * @access private
  92. ***/
  93. var $_currentWizard;
  94.  
  95. function StartupCheck() {
  96. $this->_requirements = array();
  97. $this->_status = array();
  98. }
  99.  
  100. /**
  101. * Adds a {@link StartupRequirement} to the requirement list.
  102. * @param string $name A short name describing this component, such as "db tables".
  103. * @param ref object $requirement A {@link StartupRequirement} to add.
  104. * @access public
  105. * @return void
  106. */
  107. function addRequirement($name, $requirement)
  108. {
  109. $this->_requirements[$name] =$requirement;
  110. $this->_status[$name] = STARTUP_STATUS_NOT_CHECKED;
  111. }
  112.  
  113. /**
  114. * Runs through all the requirements and asks their status.
  115. * @access public
  116. * @return boolean FALSE if an update is needed, TRUE if everything is a-OK.
  117. */
  118. function checkAllRequirements()
  119. {
  120. // cycle through requirements and get their respective statii
  121. $aOK = true;
  122. foreach(array_keys($this->_requirements) as $key) {
  123. $status = $this->_requirements[$key]->getStatus();
  124. $this->_status[$key] = $status;
  125. if ($status != STARTUP_STATUS_OK) $aOK = false;
  126.  
  127. // check if the status is an error, if so, print a fatty error.
  128. // if ($status == STARTUP_STATUS_ERROR) {
  129. // throwError( new Error(
  130. // "StartupCheck::checkAllRequirements() - While processing requirement '".$this->_requirements[$key]->getDisplayName()."' a FATAL ERROR occured and the program could not continue. See other errors for more details.","StartupCheck",true)
  131. // );
  132. // }
  133. }
  134. return $aOK;
  135. }
  136.  
  137. /**
  138. * Returns the current status of the named requirement.
  139. * @param string $name The component name to check.
  140. * @access public
  141. * @return integer One of the STARTUP_STATUS_* values.
  142. */
  143. function getStatus($name)
  144. {
  145. return $this->_status[$name];
  146. }
  147.  
  148. /**
  149. * Tells each requirement class to update those components necessary to be met, if it can be done so without user interaction.
  150. * @access public
  151. * @return boolean FALSE if an error occurs.
  152. */
  153. function updateAllAutonomousRequirements()
  154. {
  155. $aOK = true;
  156. foreach (array_keys($this->_requirements) as $key) {
  157. if ($this->getStatus($key) == STARTUP_STATUS_NEEDS_UPDATE
  158. || $this->getStatus($key) == STARTUP_STATUS_NEEDS_INSTALL) {
  159. // do the update
  160. if (($this->_status[$key] = $this->_requirements[$key]->doUpdate()) != STARTUP_STATUS_OK) $aOK = false;
  161. }
  162. }
  163.  
  164. return $aOK;
  165. }
  166.  
  167. /**
  168. * Returns an array of the names of requirements that currently have the status requested.
  169. * @access public
  170. * @return array
  171. */
  172. function getRequirementsOfStatus($status)
  173. {
  174. $array = array();
  175. foreach ($this->_status as $key=>$value) {
  176. if ($value == $status) $array[] = $key;
  177. }
  178. return $array;
  179. }
  180.  
  181. /**
  182. * Tells a specific named requirement to update necessary components without user input.
  183. * @param string $name The short name of the requirement.
  184. * @access public
  185. * @return boolean FALSE if error occurs.
  186. */
  187. function updateRequirement($name)
  188. {
  189. if ($this->getStatus($name) == STARTUP_STATUS_NEEDS_UPDATE || $this->getStatus($name) == STARTUP_STATUS_NEEDS_INSTALL) {
  190. if (($this->_status[$name] = $this->_requirements[$name]->doUpdate()) == STARTUP_STATUS_OK) {
  191. return true;
  192. } else {
  193. return false;
  194. }
  195. }
  196. return false;
  197. }
  198.  
  199. /**
  200. * Tells a specific named requirement to update necessary components using the {@link WizardProperty} objects supplied by the given {@link Wizard}.
  201. * @access public
  202. * @param string $name The short-name of the requirement to update.
  203. * @param ref object $wizard A {@link Wizard} object.
  204. * @return boolean FALSE if error occurs.
  205. */
  206. function updateRequirementWithWizard($name, $wizard)
  207. {
  208. // we can't use the data until the end user has pressed the "Save" button on the wizard
  209. $listener =$wizard->getChild("_savecancel");
  210. if ($listener->isSaveRequested()) {
  211. if (($this->_status[$name] = $this->_requirements[$name]->doUpdate($wizard->getAllValues())) == STARTUP_STATUS_OK) {
  212. return true;
  213. } else { // don't change its status.
  214. return false;
  215. }
  216. }
  217. return false;
  218. }
  219.  
  220. /**
  221. * Returns the number of requirements we have to meet.
  222. * @access public
  223. * @return integer
  224. */
  225. function getRequirementCount()
  226. {
  227. return count($this->_requirements);
  228. }
  229.  
  230. /**
  231. * Handles the update process with user input, if necessary. Uses the {@link Harmoni} object to output HTML to the end user.
  232. * In order for this to work, we must be session_registered and called every page-load.
  233. * @access public
  234. * @return boolean TRUE if program execution can continue as normal, FALSE if updating is still required.
  235. */
  236. function handleAllUpdates()
  237. {
  238. // this method does the following:
  239. // - checks all requirements, updates those that are autonomous automatically.
  240. // - if any require user input, it will step through them and handle the user input side
  241. // - if we are currently in the middle of one of these input sessions, we will
  242. // skip everything else and just output the wizard
  243. // - once the person hits the "Save" button, we will give the wizard to the requirement for handling,
  244. // and then get the next requirement that needs user input.
  245.  
  246. $harmoni = Harmoni::instance();
  247.  
  248. // if we have nothing to do, we're done.
  249. if ($this->areAllOK()) return true;
  250. // if we are actively working with a wizard...
  251. if (!$this->_useWizard($harmoni)) return false;
  252. // ok, let's get all the updates. if everything's ok, we can just return.
  253. if ($this->checkAllRequirements()) return true;
  254.  
  255. // update all autonomous requirements
  256. $this->updateAllAutonomousRequirements();
  257.  
  258. // if the above command took care of everything, let's get out.
  259. if ($this->areAllOK()) return true;
  260. // otherwise...
  261. if (!$this->_currentRequirement) {
  262. $this->_setupNextRequirementForInput();
  263. } else {
  264. $this->_currentWizard->update();
  265. }
  266.  
  267. // if we have something to work with user-input-wise, let's handle it.
  268. return $this->_useWizard($harmoni);
  269. return false;
  270. }
  271. /**
  272. * Internally handles input & output from a wizard.
  273. * @param ref object $harmoni A {@link Harmoni} object.
  274. * @access private
  275. * @return void
  276. */
  277. function _useWizard($harmoni)
  278. {
  279. if ($req = $this->_currentRequirement) {
  280. $this->_currentWizard->go();
  281. $listener =$this->_currentWizard->getChild("_savecancel");
  282. if ($listener->isSaveRequested()) {
  283. $this->updateRequirementWithWizard($req, $this->_currentWizard);
  284. $this->_currentRequirement = $this->_currentWizard = null;
  285. if ($this->areAllOK()) return true;
  286. else $this->_setupNextRequirementForInput();
  287. }
  288. if ($this->_currentWizard) {
  289. // output some HTML bizness
  290. $layout =$this->_currentWizard->getLayout();
  291. $output =$harmoni->getOutputHandler();
  292. $output->output($layout,'');
  293. return false;
  294. }
  295. }
  296. return true;
  297. }
  298.  
  299. /**
  300. * Sets up the next requirement wizard for user input.
  301. * @access private
  302. * @return void
  303. */
  304. function _setupNextRequirementForInput()
  305. {
  306. // if we have one, let's set up a new wizard and get going on that.
  307. $name = $this->getNextRequiringInput();
  308. if ($name) {
  309. $this->_currentRequirement = $name;
  310. $this->_currentWizard =$this->_requirements[$name]->createWizard();
  311. // add a listener so we know when the save buttons are pressed.
  312. $this->_currentWizard->addComponent("_savecancel", new WSaveCancelListener());
  313. }
  314. }
  315.  
  316. /**
  317. * Returns true if all requirements have been met (and checked, of course).
  318. * @access public
  319. * @return boolean
  320. */
  321. function areAllOK()
  322. {
  323. return ($this->getRequirementCount() == $this->getRequirementsOfStatus(STARTUP_STATUS_OK));
  324. }
  325.  
  326. /**
  327. * Gets the next requirement that requires user input and returns the name.
  328. * @access public
  329. * @return string or NULL if none are left.
  330. */
  331. function getNextRequiringInput()
  332. {
  333. $array = $this->getRequirementsOfStatus(STARTUP_STATUS_NEEDS_USER_INPUT);
  334. if (!count($array)) return null;
  335. return $array[0];
  336. }
  337. /**
  338. * This function prints out an error message to tell the user that something went wrong in the startup check process.
  339. * @access private
  340. * @return void
  341. * @static
  342. */
  343. function error($string)
  344. {
  345. $f = new FieldSet;
  346. $f->set("pageTitle",_("Startup Error"));
  347. $f->set("intro", _("The startup process has encountered a problem"));
  348. $f->set("errorString",$string);
  349. $tpl = new Template("error.tpl.php",POLYPHONY."/main/library/StartupCheck/");
  350. $tpl->output($f);
  351. exit(1); // exit with error
  352. }
  353. }

Documentation generated on Wed, 19 Sep 2007 10:26:41 -0400 by phpDocumentor 1.3.0RC3