Example #1
0
 /**
  * Load Cached Data
  *
  * @param string $key
  *   Cache Key
  *
  * @param boolean|optional $keepSerialized
  *   Flag to condition if Cache Data will keep serialized or not
  *
  * @return mixed|boolean
  *
  *   FALSE if:
  *
  *   <ul>
  *
  *       <li>There is no Cached entry for given key</li>
  *
  *       <li>We were unable to read Cached Data</li>
  *
  *       <li>
  *
  *           Cache Integrity Validation is enabled and read
  *           Cached Data has a different hash
  *
  *       </li>
  *
  *   </ul>
  *
  *   <p>
  *       Otherwise, if <strong>$keepSerialized</strong> is set as TRUE
  *       the Cached Data will be returned as string, just like it
  *       was stored.
  *   </p>
  *
  *   <p>
  *       If this argument is set as FALSE, we'll unserialize it to
  *       the original state
  *   </p>
  */
 public function load($key, $keepSerialized = TRUE)
 {
     if (!$this->test($key)) {
         return FALSE;
     }
     try {
         // Setting Up Reader Object
         $reader = new Reader(new Socket($this->buildFilePath($key), 'r'));
         // Reading Cached Content
         $data = $reader->readAll();
         // Closing Stream
         $reader->getStream()->close();
         if (empty($data)) {
             return NULL;
         }
         $meta = $this->meta->load($key);
         if ($meta !== FALSE) {
             // Should we check if Cache's Hash is valid?
             if ($this->options->testValidity && $meta['hash'] !== FALSE) {
                 // Comparing the Hashes
                 if ($this->hash($data, $meta['cacheControl']) !== $meta['hash']) {
                     // We have a Invalid Cache, should we delete it?
                     if ($this->options->removeCorrupted !== FALSE) {
                         try {
                             $this->remove($key);
                         } catch (CacheException $e) {
                         }
                     }
                     return FALSE;
                 }
             }
             // Should we Decompress the Cached Content?
             if ($meta['compressed']) {
                 if (function_exists('gzuncompress')) {
                     $data = gzuncompress($data);
                 }
             }
             return (bool) $keepSerialized !== FALSE ? $data : unserialize($data);
         }
         return $data;
     } catch (ReaderException $e) {
         /**
          * @internal
          *
          * As we are trying to read a file, if we got an Exception
          * means the file doesn't exists or it's not readable
          *
          * So the Cache Data must be regenerated
          */
         return FALSE;
     }
 }
Example #2
0
 /**
  * Load Data from Meta File
  *
  * @param string|optional $key
  *   Optional Metadata Entry
  *
  * @return boolean|string|array|null
  *
  *   FALSE if unable to read Metadata File
  *
  *   If <strong>$key</strong> argument IS null the whole Metadata
  *   Content will be returned
  *
  *   If <strong>$key</strong> is NOT null and given key is present in
  *   Metadata Content, for simple Information, a string will returned.
  *   For complex Information, an array will.
  *
  *   If given key is not present in Metadata Content OR an Exception is thrown,
  *   NULL will returned
  *
  * @throws Next\Cache\CacheException
  *   Fail to read Meta Data
  *
  * @throws Next\Cache\CacheException
  *   Meta Data is Corrupted (not an array)
  */
 public function load($key = NULL)
 {
     try {
         // Setting Up Reader Object
         $reader = new Reader(new Socket(sprintf('%s/%s', $this->backendOptions->meta->path, $this->backendOptions->meta->file), 'r'));
         // Reading Cached Content
         $data = $reader->readAll();
         // Closing Stream
         $reader->getStream()->close();
         if (!$data) {
             throw CacheException::noMetadata();
         }
         // Decompressing Data
         if (function_exists('gzuncompress')) {
             $data = gzuncompress($data);
         }
         // Unserializing Data
         $data = unserialize($data);
         if (!is_array($data)) {
             throw CacheException::corruptedMetadata();
         }
         // What should we return?
         if (!is_null($key)) {
             $key = md5($key);
             return array_key_exists($key, $data) ? $data[$key] : NULL;
         } else {
             return $data;
         }
     } catch (ReaderException $e) {
         /**
          * @internal
          * As we are trying to read a file, if we got an Exception
          * means the file doesn't exists or it's not readable
          *
          * So the Meta Data File must be regenerated
          */
         return FALSE;
     }
 }
Example #3
0
 /**
  * Send the Request
  *
  * @return Next\HTTP\Response|NULL
  *
  *   If an AdapterException is caught
  *   something is wrong with Response being send and thus NULL is returned
  *
  *   Otherwise, if everything is fine, a Next\HTTP\Response instance will
  *
  * @throws Next\HTTP\Request\RequestException
  *   No HTTP Stream Adapter provided
  *
  * @throws Next\HTTP\Headers\Fields\FieldsException
  *   Invalid or mal-formed Cookie (s) Value (s)
  *
  * @see Next\HTTP\Stream\Adapter\AdapterException
  */
 public function send()
 {
     // Setting Up a Fallback HTTP Stream Adapter
     if (is_null($this->adapter)) {
         $this->adapter = new Socket($this->uri, 'r', new SocketContext(array('http' => array('method' => $this->method))));
     }
     // Shortening HTTP Stream Context
     $context = $this->adapter->getContext();
     // Request Method
     $context->setOptions(array('http' => array('method' => $this->method)));
     //---------------
     // Request Method-related tasks
     switch ($this->method) {
         case self::GET:
             // Nothing so far...
             break;
         case self::POST:
             $this->sendPost();
             break;
         case self::PUT:
             // Nothing so far...
             break;
         case self::DELETE:
             // Nothing so far...
             break;
     }
     /**
      * @internal
      *
      * Cookies
      *
      * Cookies are defined in a Header Field too, so they come first
      */
     try {
         $this->headers->addHeader($this->cookies->getCookies(TRUE));
     } catch (FieldsException $e) {
         throw new RequestException($e->getMessage());
     }
     // Headers
     $context->setOptions(array('http' => array('header' => $this->headers->getHeaders(TRUE))));
     // Building Response
     try {
         $reader = new Reader($this->adapter);
         return new Response($reader->readAll(), $this->adapter->getMetaData());
     } catch (AdapterException $e) {
         return new Response();
     }
 }