PageRenderTime 25ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/Zend/Scheduler/Task.php

https://github.com/querl/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 371 lines | 169 code | 37 blank | 165 comment | 12 complexity | 43e899fc5ce5b6926877186d882cadd4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Scheduler
  17. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Automatic scheduler task.
  22. *
  23. * @category Zend
  24. * @package Zend_Scheduler
  25. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Scheduler_Task implements Zend_Scheduler_Task_Interface
  29. {
  30. protected $_frontController = null;
  31. /**
  32. * @var Zend_Date Request time
  33. */
  34. protected $_time = null;
  35. /** @var string First time the task is allowed to run */
  36. protected $_firstRun = '';
  37. /** @var string Final time the task is allowed to run */
  38. protected $_finalRun = '';
  39. /** @var array Array of Zend_Scheduler_Task_Rule_Abstract objects */
  40. protected $_rules = array();
  41. /** @var array Array of Zend_Controller_Request_Abstract objects */
  42. protected $_requests = array();
  43. /** @var bool Has the task completed execution? */
  44. protected $_completed = false;
  45. /** @var array Array of serialized rules */
  46. protected $_serialized = array();
  47. /**
  48. * Constructor.
  49. *
  50. * @param array $options Task options.
  51. */
  52. public function __construct(array $options = array())
  53. {
  54. $this->setOptions($options);
  55. }
  56. /**
  57. * static task getter
  58. *
  59. * @param array $options
  60. * @return Zend_Scheduler_Task
  61. */
  62. public static function getTask(array $options = array())
  63. {
  64. return new Zend_Scheduler_Task($options);
  65. }
  66. /**
  67. * Sets the Task options.
  68. *
  69. * @param array $options Options
  70. */
  71. public function setOptions(array $options)
  72. {
  73. foreach ($options as $option => $value) {
  74. $method = 'set' . ucfirst($option);
  75. if (method_exists($this, $method)) {
  76. $this->{$method}($value);
  77. }
  78. }
  79. }
  80. /**
  81. * Set the time (by default, the request time). For testing purposes a
  82. * different time can be passed in.
  83. *
  84. * @param Zend_Date $time
  85. * @return Zend_Scheduler_Task This instance
  86. */
  87. public function setTime($time)
  88. {
  89. if (!$time instanceof Zend_Date) {
  90. $time = new Zend_Date($time);
  91. }
  92. $this->_time = $time;
  93. return $this;
  94. }
  95. /**
  96. * Sets the request.
  97. *
  98. * For the first parameter, users can pass in either the name of the
  99. * controller or a request object.
  100. *
  101. * @param string|Zend_Controller_Request_Abstract $controller Controller name or request object
  102. * @param string $action Action name
  103. * @param array $parameters Request parameters
  104. * @return Zend_Scheduler_Task This instance
  105. */
  106. public function setRequest($controller, $action = 'index', array $parameters = array())
  107. {
  108. $this->_requests = array();
  109. return $this->addRequest($controller, $action, $parameters);
  110. }
  111. /**
  112. * Adds a request.
  113. *
  114. * For the first parameter, users can pass in either the name of the
  115. * controller or a request object.
  116. *
  117. * @param string|Zend_Controller_Request_Abstract $controller Controller name or request object
  118. * @param string $action Action name
  119. * @param array $parameters Request parameters
  120. * @return Zend_Scheduler_Task This instance
  121. */
  122. public function addRequest($controller, $action = 'index', array $parameters = array())
  123. {
  124. if ($controller instanceof Zend_Controller_Request_Abstract) {
  125. $request = $controller;
  126. } else {
  127. $request = new Zend_Controller_Request_Http();
  128. $request->setControllerName($controller)
  129. ->setActionName($action)
  130. ->setParams($parameters);
  131. }
  132. $this->_requests[] = $request;
  133. return $this;
  134. }
  135. /**
  136. * Sets the first time the task is allowed to run.
  137. *
  138. * @param string $time
  139. * @return Zend_Scheduler_Task This instance
  140. */
  141. public function setEarliestRun($time)
  142. {
  143. $this->_firstRun = $time;
  144. }
  145. /**
  146. * Sets the final time the task is allowed to run.
  147. *
  148. * @param string $time
  149. * @return Zend_Scheduler_Task This instance
  150. */
  151. public function setLatestRun($time)
  152. {
  153. $this->_finalRun = $time;
  154. }
  155. /**
  156. * Sets the months in which the task is scheduled to run.
  157. *
  158. * @param string $months Comma-delimited list of months (e.g., January)
  159. * @return Zend_Scheduler_Task This instance
  160. */
  161. public function setMonths($months)
  162. {
  163. return $this->_addRule('months', $months);
  164. }
  165. /**
  166. * Sets the days of the month in which the task is scheduled to run.
  167. *
  168. * @param string $days Comma-delimited list of days (1-31)
  169. * @return Zend_Scheduler_Task This instance
  170. */
  171. public function setDays($days)
  172. {
  173. return $this->_addRule('days', $days);
  174. }
  175. /**
  176. * Sets the days of the week in which the task is scheduled to run.
  177. *
  178. * @param string $days Comma-delimited list of days of the week (e.g., Monday)
  179. * @return Zend_Scheduler_Task This instance
  180. */
  181. public function setWeekdays($weekdays)
  182. {
  183. return $this->_addRule('weekdays', $weekdays);
  184. }
  185. /**
  186. * Sets the hours in which the task is scheduled to run.
  187. *
  188. * @param string $days Comma-delimited list of hours (0-23)
  189. * @return Zend_Scheduler_Task This instance
  190. */
  191. public function setHours($hours)
  192. {
  193. return $this->_addRule('hours', $hours);
  194. }
  195. /**
  196. * Sets the minutes in which the task is scheduled to run.
  197. *
  198. * @param string $days Comma-delimited list of minutes (0-59)
  199. * @return Zend_Scheduler_Task This instance
  200. */
  201. public function setMinutes($minutes)
  202. {
  203. return $this->_addRule('minutes', $minutes);
  204. }
  205. /**
  206. * Determines whether a task should be ran or not.
  207. *
  208. * @return bool
  209. */
  210. public function isScheduled()
  211. {
  212. if ($this->_firstRun) {
  213. $firstRun = new Zend_Date($this->_firstRun, Zend_Date::ISO_8601);
  214. $tooEarly = $firstRun->getTimestamp() > $this->_time->getTimestamp();
  215. if ($tooEarly) {
  216. return false;
  217. }
  218. }
  219. if ($this->_finalRun) {
  220. $finalRun = new Zend_Date($this->_finalRun, Zend_Date::ISO_8601);
  221. $tooLate = $this->_time->getTimestamp() > $finalRun->getTimestamp();
  222. if ($tooLate) {
  223. return false;
  224. }
  225. }
  226. foreach ($this->_rules as $rule) {
  227. if (!$rule->matches($this->_time)) {
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. /**
  234. * Dispatches all requests.
  235. *
  236. * @return array|null Array of Response objects, or null if no tasks
  237. */
  238. public function run()
  239. {
  240. $controller = $this->getFrontController();
  241. //$router = $controller->getRouter();
  242. $returnResponse = $controller->returnResponse(true);
  243. $responses = array();
  244. foreach ($this->_requests as $request) {
  245. /*
  246. if ($request->getControllerName()) { // Use default router
  247. $controller->setRouter(new Zend_Controller_Router_Rewrite());
  248. }
  249. */
  250. $request->setPathInfo($request->getModuleName() . '/' . $request->getControllerName() . '/' . $request->getActionName());
  251. if ($returnResponse) {
  252. $responses[] = $controller->dispatch($request);
  253. }
  254. //$controller->setRouter($router);
  255. }
  256. $this->_completed = true;
  257. return $responses;
  258. }
  259. /**
  260. * Returns whether or not the task has completed execution.
  261. *
  262. * @return bool True, if task is completed
  263. */
  264. public function isCompleted()
  265. {
  266. return $this->_completed;
  267. }
  268. /**
  269. * Executes prior to serialization.
  270. */
  271. public function __sleep()
  272. {
  273. foreach ($this->_rules as $rule) {
  274. $this->_serialized['rules'][] = serialize($rule);
  275. }
  276. foreach ($this->_requests as $request) {
  277. $this->_serialized['requests'][] = serialize($request);
  278. }
  279. return array('_serialized');
  280. }
  281. /**
  282. * Executes following unserialization.
  283. */
  284. public function __wakeup()
  285. {
  286. if (isset($this->_serialized['rules'])) {
  287. foreach ($this->_serialized['rules'] as $rule) {
  288. $this->_rules[] = unserialize($rule);
  289. }
  290. }
  291. if (isset($this->_serialized['requests'])) {
  292. foreach ($this->_serialized['requests'] as $request) {
  293. $this->_requests[] = unserialize($request);
  294. }
  295. }
  296. $this->_serialized = array();
  297. }
  298. /**
  299. * Adds a rule. Called by {@link setMonths()}, {@link setDays()}, etc.
  300. *
  301. * @param string $type Rule type
  302. * @param string $rule Rule string
  303. * @return Zend_Scheduler_Task This instance
  304. */
  305. protected function _addRule($type, $rule)
  306. {
  307. $this->_rules[$type] = new Zend_Scheduler_Task_Rule($type, $rule);
  308. return $this;
  309. }
  310. /**
  311. *
  312. * @return Array of Zend_Scheduler_Task_Rule objects
  313. */
  314. public function getRule($type)
  315. {
  316. return $this->_rules[$type];
  317. }
  318. /**
  319. *
  320. * @return Array of Zend_Controller_Request_Http objects
  321. */
  322. public function getRequests()
  323. {
  324. return $this->_requests;
  325. }
  326. public function setFrontController($frontController)
  327. {
  328. $this->_frontController = $frontController;
  329. }
  330. public function getFrontController()
  331. {
  332. return $this->_frontController;
  333. }
  334. }