Exemplo n.º 1
0
 /**
  * Get the latest version (update) information, either from the cache or
  * from the update server.
  * 
  * @param $force bool Set to true to force fetching fresh data from the server
  * 
  * @return stdClass The update information, in object format
  */
 public function getUpdateInformation($force = false)
 {
     // Get the Live Update configuration
     $config = LiveUpdateConfig::getInstance();
     // Get an instance of the storage class
     $storageOptions = $config->getStorageAdapterPreferences();
     require_once dirname(__FILE__) . '/storage/storage.php';
     $this->storage = LiveUpdateStorage::getInstance($storageOptions['adapter'], $storageOptions['config']);
     $storage = $this->storage;
     // If we are requested to forcibly reload the information, clear old data first
     if ($force) {
         $this->storage->set('lastcheck', 0);
         $this->storage->set('updatedata', '');
         $this->storage->save();
         $registry = $storage->getRegistry();
         $registry->set('update.lastcheck', null);
         $registry->set('update.updatedata', null);
         $storage->setRegistry($registry);
     }
     // Fetch information from the cache
     $registry = $storage->getRegistry();
     $lastCheck = $registry->get('lastcheck', 0);
     $cachedData = $registry->get('updatedata', null);
     if (is_string($cachedData)) {
         $cachedData = trim($cachedData, '"');
         $cachedData = json_decode($cachedData);
     }
     if (empty($cachedData)) {
         $lastCheck = 0;
     }
     // Check if the cache is at most $cacheTTL hours old
     $now = time();
     $maxDifference = $this->cacheTTL * 3600;
     $difference = abs($now - $lastCheck);
     if (!$force && $difference <= $maxDifference) {
         // The cache is fresh enough; return cached data
         return $cachedData;
     } else {
         // The cache is stale; fetch new data, cache it and return it to the caller
         $data = $this->getUpdateData($force);
         $this->storage->set('lastcheck', $now);
         $this->storage->set('updatedata', json_encode($data));
         $this->storage->save();
         return $data;
     }
 }
 public function setRegistry($registry)
 {
     self::$registry = $registry;
 }