Guest User

Controller

a guest
Nov 14th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * CronController - The central cron controller. This cron controller needs
  5.  * to be called every minute of every day. The cron controller itself will
  6.  * handle various cron tasks within to determine how many times a certain call
  7.  * needs to be executed. This way only one cronjob needs to be set in the
  8.  * administration panel on the server, while the rest will be handled by the
  9.  * application, and new cron jobs can be added to the application easily.
  10.  *
  11.  * THIS CONTROLLER NEEDS TO BE CALLED EVERY MINUTE OF EVERY DAY TO ENSURE THAT
  12.  *         IT WILL LOOP THROUGH ALL THE OTHER CRON CLASSES PROPERLY
  13.  *
  14.  * @author Stefan van Beusekom
  15.  * @version 1.0
  16.  */
  17. class CronController extends Zend_Controller_Action {
  18.  
  19.     // Layout has been disabled
  20.     public function init() {
  21.         $this->_helper->layout()->disableLayout();
  22.         $this->_helper->viewRenderer->setNoRender(true);
  23.     }
  24.  
  25.     public function calculateTimeout($timelimit) {
  26.         set_time_limit($timelimit);
  27.     }
  28.  
  29.     /**
  30.      * Index of the cron controller, which it defaults to when no action is
  31.      * given. The index of the cron controller loops through the cron classes
  32.      * and calls the 'execute' method to perform the cron job.
  33.      *
  34.      * @author Stefan van Beusekom
  35.      * @since 1.0
  36.      */
  37.     public function indexAction() {
  38.         // Retrieve all of the cronjob classes
  39.         $list = $this->getCronClassList();
  40.         $objectList = array();
  41.         foreach ($list as $className) {
  42.             $cronObject = new $className;
  43.             if ($cronObject::CRON_ACTIVE)
  44.                 array_push($objectList, $cronObject);
  45.         }
  46.  
  47.         // Sort the object array based on the priority of the cronjobs
  48.         usort($objectList, function($classA, $classB) {
  49.                     return strcmp($classA::CRON_PRIORITY, $classB::CRON_PRIORITY);
  50.                 });
  51.  
  52.         // Execute the objects, ordered by priority
  53.         foreach ($objectList as $object) {
  54.             $object->execute();
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * Get a list of all the cron classes currently in the cron folder, and put
  60.      * them into a collective array.
  61.      *
  62.      * @author Stefan van Beusekom
  63.      * @since 1.0
  64.      * @return array Array of available cron classes
  65.      */
  66.     private function getCronClassList() {
  67.         $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
  68.         $classPref = $config->get('appnamespace') . "_Model_Cron_";
  69.         $classPath = APPLICATION_PATH . "/models/Cron/";
  70.         $classList = array();
  71.  
  72.         $dirHandle = opendir($classPath);
  73.  
  74.         while (false !== ($entry = readdir($dirHandle))) {
  75.             $info = pathinfo($classPath . $entry);
  76.             if (!is_dir($classPath . $entry) && strtolower($info['filename']) != "abstract") {
  77.                 $tempClassName = $classPref . $info['filename'];
  78.                 array_push($classList, $tempClassName);
  79.             }
  80.         }
  81.  
  82.         return $classList;
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment