123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- /**
- * Pimf
- *
- * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
- * @license http://krsteski.de/new-bsd-license New BSD License
- */
- namespace Pimf;
- use \Pimf\Util\Header, Pimf\Util\Json as UtilJson;
- /**
- * Provides a simple interface around the HTTP an HTTPCache-friendly response generating.
- * Use this class to build and the current HTTP response before it is returned to the client.
- *
- * @package Pimf
- * @author Gjero Krsteski <[email protected]>
- */
- class Response
- {
- /**
- * The request method send by the client-browser.
- *
- * @var string
- */
- protected $method = null;
- /**
- * If the response attempts to send any cached headers.
- *
- * @var bool
- */
- protected static $cached = false;
- /**
- * Type of the data will be send to the client-browser.
- *
- * @var string
- */
- protected static $typed = null;
- /**
- * @param string $requestMethod
- *
- * @throws \RuntimeException
- */
- public function __construct($requestMethod)
- {
- $this->method = '' . strtoupper($requestMethod);
- // it is PIMF framework restriction
- if (!in_array($this->method, array('POST', 'GET', null))) {
- throw new \RuntimeException('unsupported request-method given');
- }
- Header::clear();
- }
- /**
- * @return string
- */
- public function getMethod()
- {
- return $this->method;
- }
- public function asJSON()
- {
- $this->preventMultipleTypes();
- self::$typed = __FUNCTION__;
- Header::asJSON();
- return $this;
- }
- public function asHTML()
- {
- $this->preventMultipleTypes();
- self::$typed = __FUNCTION__;
- Header::asTextHTML();
- return $this;
- }
- public function asPDF()
- {
- $this->preventMultipleTypes();
- self::$typed = __FUNCTION__;
- Header::asPDF();
- return $this;
- }
- public function asCSV()
- {
- $this->preventMultipleTypes();
- self::$typed = __FUNCTION__;
- Header::asCSV();
- return $this;
- }
- public function asTEXT()
- {
- $this->preventMultipleTypes();
- self::$typed = __FUNCTION__;
- Header::asTextPlain();
- return $this;
- }
- public function asZIP()
- {
- $this->preventMultipleTypes();
- self::$typed = __FUNCTION__;
- Header::asZIP();
- return $this;
- }
- public function asXZIP()
- {
- $this->preventMultipleTypes();
- self::$typed = __FUNCTION__;
- Header::asXZIP();
- return $this;
- }
- public function asMSWord()
- {
- $this->preventMultipleTypes();
- self::$typed = __FUNCTION__;
- Header::asMSWord();
- return $this;
- }
- /**
- * Sends a download dialog to the browser.
- *
- * @param string $stream Can be a file-path or a string.
- * @param string $name Name of the stream/file should be shown.
- * @param boolean $exit Optional for testing
- */
- public function sendStream($stream, $name, $exit = true)
- {
- Header::clear();
- Header::sendDownloadDialog($stream, $name, $exit);
- }
- /**
- * @param mixed $data
- * @param bool $exit
- */
- public function send($data, $exit = true)
- {
- $body = $data;
- if (self::$typed === 'asJSON') {
- $body = UtilJson::encode($data);
- } elseif ($data instanceof \Pimf\View) {
- $body = $data->render();
- }
- echo '' . $body;
- if ($exit) {
- exit(0);
- }
- }
- /**
- * If instead you have a page that has personalization on it
- * (say, for example, the splash page contains local news as well),
- * you can set a copy to be cached only by the browser.
- *
- * @param int $seconds Interval in seconds
- *
- * @return $this
- */
- public function cacheBrowser($seconds)
- {
- self::preventMultipleCaching();
- self::$cached = true;
- Header::cacheBrowser($seconds);
- return $this;
- }
- /**
- * If you want to try as hard as possible to keep a page from being cached anywhere.
- *
- * @return $this
- */
- public function cacheNone()
- {
- self::preventMultipleCaching();
- self::$cached = true;
- Header::cacheNone();
- return $this;
- }
- /**
- * If you want to allow a page to be cached by shared proxies for one minute.
- *
- * @param int $seconds Interval in seconds
- *
- * @return $this
- */
- public function cacheNoValidate($seconds = 60)
- {
- self::preventMultipleCaching();
- self::$cached = true;
- Header::cacheNoValidate($seconds);
- return $this;
- }
- /**
- * Handles setting pages that are always to be revalidated for freshness by any cache.
- *
- * @param int $last_modified Timestamp in seconds
- *
- * @return $this
- */
- public function exitIfNotModifiedSince($last_modified)
- {
- self::preventMultipleCaching();
- self::$cached = true;
- Header::exitIfNotModifiedSince($last_modified);
- return $this;
- }
- /**
- * @throws \RuntimeException
- */
- private function preventMultipleTypes()
- {
- if (!is_empty(self::$typed)) {
- Header::clear();
- throw new \RuntimeException('only one HTTP content-type can be sent!');
- }
- }
- /**
- * @throws \RuntimeException
- */
- private function preventMultipleCaching()
- {
- if ($this->method != 'GET') {
- Header::clear();
- throw new \RuntimeException('HTTP cache headers can only take effect if request was sent via GET method!');
- }
- if (self::$cached === true) {
- Header::clear();
- throw new \RuntimeException('only one HTTP cache-control can be sent!');
- }
- }
- }
|