com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_events.php

Documentation is available at lib_events.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * Describes a library that handles everything related to timed events. The
  9. * timed events work very much as cron jobs except that they are checked on
  10. * every page load instead of being a running process.
  11. * @author Andreas Launila
  12. * @version $Revision: 1.2 $
  13. * @package com.lokorin.lokorin.lib
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. */
  16. /**
  17. * For access to common constants and functions.
  18. */
  19. require_once('common.php');
  20.  
  21. /**
  22. * Gets all events that are due to be performed.
  23. * @return array An array of instances of TimedEvent. Each instance represents
  24. * one event that should be performed. An empty array is returned if there
  25. * are no due events.
  26. */
  27. function getDueEvents() {
  28. global $db;
  29.  
  30. $rows = $db->querySelect(TABLE_EVENTS, array("id"),
  31. "WHERE `next_time` <= " . $db->escape(time()));
  32. $events = array();
  33. foreach($rows as $row) {
  34. $events[] = new TimedEvent($row["id"]);
  35. }
  36. return $events;
  37. }
  38.  
  39. /**
  40. * Describes an event that is schedudled to be run every so often.
  41. * @author Andreas Launila
  42. * @package com.lokorin.lokorin.lib
  43. */
  44. class TimedEvent extends TableEntry {
  45. /**
  46. * A description of the event.
  47. * @var string
  48. */
  49. private $decsription;
  50. /**
  51. * A timestamp that describes the next point in time when the event should
  52. * be performed.
  53. * @var integer
  54. */
  55. private $nextTime;
  56. /**
  57. * The name of the library that should be included before the event is run
  58. * and before the callback is called. If it's empty then no library is
  59. * included.
  60. * @var string
  61. */
  62. private $libraryName;
  63. /**
  64. * The callback that should be called when the event is to be performed.
  65. * @var string
  66. */
  67. private $callback;
  68. /**
  69. * A timestamp pattern for the timestamp that should be used as the next
  70. * point in time that the event should be triggered on failure.
  71. * @var string
  72. */
  73. private $timestampFailure;
  74. /**
  75. * A timestamp pattern for the timestamp that should be used as the next
  76. * point in time that the event should be triggered on success.
  77. * @var string
  78. */
  79. private $timestampSuccess;
  80. /**
  81. * Constructor for TimedEvent.
  82. * @param integer $id The unique identifier for the timed event.
  83. */
  84. public function __construct($id) {
  85. $fields = array(
  86. "description",
  87. "next_time",
  88. "library_name",
  89. "callback",
  90. "timestamp_failure",
  91. "timestamp_success"
  92. );
  93. parent::__construct(TABLE_EVENTS, $id, $fields);
  94. }
  95. /**
  96. * @see com.lokorin.dfcrafters.TableEntry.readRow($row)
  97. */
  98. public function readRow($row) {
  99. $this->description = $row["description"];
  100. $this->nextTime = $row["next_time"];
  101. $this->libraryName = $row["library_name"];
  102. $this->callback = $row["callback"];
  103. $this->timestampFailure = $row["timestamp_failure"];
  104. $this->timestampSuccess = $row["timestamp_success"];
  105. }
  106. /**
  107. * Gets a brief description of the event.
  108. * @return string A short description or name.
  109. */
  110. public function getDesciption() {
  111. return $this->description;
  112. }
  113. /**
  114. * Performs the event, correctly processing it and marking it as performed.
  115. * That means that it will not be performed again until a set number of
  116. * seconds have elapsed.
  117. */
  118. public function performEvent() {
  119. //Update the time as if a failure had occured before attempting the
  120. //event, this is to avoid double hits.
  121. $this->markAsPerformed($this->timestampFailure);
  122. //Load the libraries.
  123. if(strlen($this->libraryName) != '') {
  124. doInclude($this->libraryName);
  125. }
  126. //Call the callback.
  127. try {
  128. $callback = $this->callback;
  129. $callback();
  130. $this->markAsPerformed($this->timestampSuccess);
  131. } catch(Exception $e) {
  132. logException($e);
  133. }
  134. }
  135. /**
  136. * Marks the event as performed, updating the next time that it should be
  137. * performed.
  138. * @var string $timestampPattern The timestamp patternt that describes the
  139. * next time that the event hsould be performed.
  140. * @throws IllegalArgumentException If the string is not a valid timestamp
  141. * pattern.
  142. */
  143. private function markAsPerformed($timestampPattern) {
  144. //Validate the pattern.
  145. $matches = array();
  146. //floor|
  147. preg_match_all("/(floor|time|[0-9*()+-\/\s])+/", $timestampPattern, $matches);
  148. if(implode($matches[0]) != $timestampPattern) {
  149. throw new IllegalArgumentException("The pattern (".
  150. $timestampPattern.") is not a valid timestamp pattern.");
  151. }
  152. //Get the new timestamp to use.
  153. ob_start();
  154. eval('echo '.$timestampPattern.';');
  155. $timestamp = ob_get_clean();
  156. //Enter it into the database.
  157. global $db;
  158. $vars = array(
  159. "next_time" => $timestamp
  160. );
  161. $idFields = array(
  162. "id" => parent::$this->getId()
  163. );
  164. $db->queryUpdate(TABLE_EVENTS, $vars, $db->buildSqlSelector($idFields));
  165. }
  166. }
  167.  
  168. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:22 +0200 by phpDocumentor 1.3.0RC4