[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/ -> Scheduler.php (source)

   1  <?php
   2  
   3  require_once  'Horde.php';
   4  require_once 'VFS.php';
   5  
   6  /**
   7   * Horde_Scheduler
   8   *
   9   * $Horde: framework/Scheduler/Scheduler.php,v 1.23.10.8 2005/12/01 15:27:23 chuck Exp $
  10   *
  11   * @package Horde_Scheduler
  12   */
  13  class Horde_Scheduler {
  14  
  15      /**
  16       * Name of the sleep function.
  17       *
  18       * @var string
  19       */
  20      var $_sleep;
  21  
  22      /**
  23       * Adjustment factor to sleep in microseconds.
  24       *
  25       * @var integer
  26       */
  27      var $_sleep_adj;
  28  
  29      /**
  30       * Constructor.
  31       *
  32       * Figures out how we can best sleep with microsecond precision
  33       * based on what platform we're running on.
  34       */
  35      function Horde_Scheduler()
  36      {
  37          if (substr(PHP_OS, 0, 3) == 'WIN') {
  38              $this->_sleep = 'sleep';
  39              $this->_sleep_adj = 1000000;
  40          } else {
  41              $this->_sleep = 'usleep';
  42              $this->_sleep_adj = 1;
  43          }
  44      }
  45  
  46      /**
  47       * Main loop/action function.
  48       *
  49       * @abstract
  50       */
  51      function run()
  52      {
  53      }
  54  
  55      /**
  56       * Preserve the internal state of the scheduler object that we are
  57       * passed, and save it to the Horde VFS backend. Horde_Scheduler
  58       * objects should define __sleep() and __wakeup() serialization
  59       * callbacks for anything that needs to be done at object
  60       * serialization or deserialization - handling database
  61       * connections, etc.
  62       *
  63       * @param string  $id  An id to uniquely identify this scheduler from
  64       *                     others of the same class.
  65       */
  66      function serialize($id = '')
  67      {
  68          $vfs = &VFS::singleton($GLOBALS['conf']['vfs']['type'],
  69                                 Horde::getDriverConfig('vfs', $GLOBALS['conf']['vfs']['type']));
  70          if (is_a($vfs, 'PEAR_Error')) {
  71              Horde::logMessage($vfs, __FILE__, __LINE__, PEAR_LOG_ERR);
  72              return false;
  73          }
  74  
  75          $result = $vfs->writeData('.horde/scheduler', String::lower(get_class($this)) . $id, serialize($this), true);
  76          if (is_a($result, 'PEAR_Error')) {
  77              Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
  78              return false;
  79          }
  80  
  81          return true;
  82      }
  83  
  84      /**
  85       * Restore a Horde_Scheduler object from the cache.
  86       *
  87       * @param string  $class     The name of the Horde_Scheduler object to restore.
  88       * @param string  $id        An id to uniquely identify this
  89       *                           scheduler from others of the same class.
  90       * @param boolean $autosave  Automatically store (serialize) the returned
  91       *                           object at script shutdown.
  92       *
  93       * @see Horde_Scheduler::serialize()
  94       */
  95      function &unserialize($class, $id = '', $autosave = true)
  96      {
  97          // Need a lowercase version of the classname, and a default
  98          // instance of the scheduler object in case we can't retrieve
  99          // one.
 100          $class = strtolower($class);
 101          $scheduler = &new $class;
 102  
 103          $vfs = &VFS::singleton($GLOBALS['conf']['vfs']['type'],
 104                                 Horde::getDriverConfig('vfs', $GLOBALS['conf']['vfs']['type']));
 105          if (is_a($vfs, 'PEAR_Error')) {
 106              Horde::logMessage($vfs, __FILE__, __LINE__, PEAR_LOG_ERR);
 107          } else {
 108              $data = $vfs->read('.horde/scheduler', $class . $id);
 109              if (is_a($data, 'PEAR_Error')) {
 110                  Horde::logMessage($data, __FILE__, __LINE__, PEAR_LOG_INFO);
 111              } else {
 112                  $scheduler = @unserialize($data);
 113                  if (!$scheduler) {
 114                      $scheduler = &new $class;
 115                  }
 116              }
 117          }
 118  
 119          if ($autosave) {
 120              register_shutdown_function(array(&$scheduler, 'serialize'));
 121          }
 122  
 123          return $scheduler;
 124      }
 125  
 126      /**
 127       * Platform-independant sleep $msec microseconds.
 128       *
 129       * @param integer $msec  Microseconds to sleep.
 130       */
 131      function sleep($msec)
 132      {
 133          call_user_func($this->_sleep, $msec / $this->_sleep_adj);
 134      }
 135  
 136      /**
 137       * Attempts to return a concrete Horde_Scheduler instance based on $driver.
 138       *
 139       * @param mixed $driver  The type of concrete Horde_Scheduler subclass to
 140       *                       return. If $driver is an array, then we will look
 141       *                       in $driver[0]/lib/Horde_Scheduler/ for the
 142       *                       subclass implementation named $driver[1].php.
 143       * @param array $params  A hash containing any additional configuration or
 144       *                       connection parameters a subclass might need.
 145       *
 146       * @return Horde_Scheduler  The newly created concrete Horde_Scheduler
 147       *                          instance, or false on an error.
 148       */
 149      function &factory($driver, $params = null)
 150      {
 151          if (is_array($driver)) {
 152              $app = $driver[0];
 153              $driver = $driver[1];
 154          }
 155  
 156          $driver = basename($driver);
 157  
 158          if (empty($driver) || (strcmp($driver, 'none') == 0)) {
 159              $scheduler = &new Horde_Scheduler();
 160              return $scheduler;
 161          }
 162  
 163          if (!empty($app)) {
 164              include_once $GLOBALS['registry']->get('fileroot', $app) . '/lib/Scheduler/' . $driver . '.php';
 165          } elseif (@file_exists(dirname(__FILE__) . '/Scheduler/' . $driver . '.php')) {
 166              include_once dirname(__FILE__) . '/Scheduler/' . $driver . '.php';
 167          } else {
 168              // Use include_once here to avoid a fatal error if the
 169              // class isn't found.
 170              @include_once 'Horde/Scheduler/' . $driver . '.php';
 171          }
 172          $class = 'Horde_Scheduler_' . $driver;
 173          if (class_exists($class)) {
 174              $scheduler = &new $class($params);
 175          } else {
 176              $scheduler = PEAR::raiseError('Class definition of ' . $class . ' not found.');
 177          }
 178  
 179          return $scheduler;
 180      }
 181  
 182      /**
 183       * Attempts to return a reference to a concrete Horde_Scheduler
 184       * instance based on $driver. It will only create a new instance
 185       * if no Horde_Scheduler instance with the same parameters
 186       * currently exists.
 187       *
 188       * This should be used if multiple schedulers (and, thus, multiple
 189       * Horde_Scheduler instances) are required.
 190       *
 191       * This method must be invoked as: $var = &Horde_Scheduler::singleton()
 192       *
 193       * @param string $driver  The type of concrete Horde_Scheduler subclass to
 194       *                        return.
 195       * @param array $params   A hash containing any additional configuration or
 196       *                        connection parameters a subclass might need.
 197       *
 198       * @return Horde_Scheduler  The concrete Horde_Scheduler reference, or
 199       *                          false on an error.
 200       */
 201      function &singleton($driver, $params = null)
 202      {
 203          static $instances = array();
 204  
 205          $signature = serialize(array($driver, $params));
 206          if (!isset($instances[$signature])) {
 207              $instances[$signature] = &Horde_Scheduler::factory($driver, $params);
 208          }
 209  
 210          return $instances[$signature];
 211      }
 212  
 213  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7