Mercurial > SimpleWebPresenter
comparison Page.inc @ 86:b9654b9c4a66
Make headers a part of the content, rather than setting them directly.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Mon, 15 Oct 2012 18:32:56 +0200 |
| parents | 2a8e2f571b24 |
| children | 7a9c45b53866 |
comparison
equal
deleted
inserted
replaced
| 85:590e6950fa7f | 86:b9654b9c4a66 |
|---|---|
| 4 /// @cond | 4 /// @cond |
| 5 $baseDir = dirname(__FILE__); | 5 $baseDir = dirname(__FILE__); |
| 6 $cache = ScriptIncludeCache::instance(__FILE__); | 6 $cache = ScriptIncludeCache::instance(__FILE__); |
| 7 $cache->includeOnce('Options.inc', dirname(__FILE__)); | 7 $cache->includeOnce('Options.inc', dirname(__FILE__)); |
| 8 /// @endcond | 8 /// @endcond |
| 9 | |
| 10 class PageContent | |
| 11 { | |
| 12 public $headers = array(); | |
| 13 public $content; | |
| 14 | |
| 15 function __construct($content = "") | |
| 16 { | |
| 17 $this->content = $content; | |
| 18 } | |
| 19 | |
| 20 function addHeader($txt) | |
| 21 { | |
| 22 array_push($this->headers, $txt); | |
| 23 } | |
| 24 | |
| 25 function __toString() | |
| 26 { | |
| 27 return $this->content; | |
| 28 } | |
| 29 } | |
| 9 | 30 |
| 10 /** | 31 /** |
| 11 * Master class for generating a page | 32 * Master class for generating a page |
| 12 */ | 33 */ |
| 13 abstract class Page | 34 abstract class Page |
| 57 { | 78 { |
| 58 return (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')); | 79 return (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')); |
| 59 } | 80 } |
| 60 | 81 |
| 61 /** | 82 /** |
| 83 * Decide wether or not this page may be validated. | |
| 84 * | |
| 85 * Normally this is a check for the option novalidate, but this may | |
| 86 * be overridden | |
| 87 * | |
| 88 * @return bool if this page may be validated | |
| 89 */ | |
| 90 function mayValidate() | |
| 91 { | |
| 92 return !$_GET['novalidate']; | |
| 93 } | |
| 94 | |
| 95 | |
| 96 /** | |
| 62 * Turns on compression for this page | 97 * Turns on compression for this page |
| 63 * | 98 * |
| 64 * @note This may not be reversed | 99 * @note This may not be reversed |
| 65 */ | 100 */ |
| 66 function startCompression() | 101 function startCompression() |
| 94 } | 129 } |
| 95 $res = $this->generateContent(); | 130 $res = $this->generateContent(); |
| 96 if ($this->mayCompress()) { | 131 if ($this->mayCompress()) { |
| 97 $this->startCompression(); | 132 $this->startCompression(); |
| 98 } | 133 } |
| 134 $t = gettype($res); | |
| 135 if ($t === "string") { | |
| 136 $res = new Content($res); | |
| 137 } | |
| 138 elseif (get_class($res) !== "PageContent") { | |
| 139 throw new InvalidArgumentException("generateContent returned an unexpected type"); | |
| 140 } | |
| 141 return $res; | |
| 142 } | |
| 143 | |
| 144 function display() | |
| 145 { | |
| 146 $res = $this->genPage(); | |
| 147 foreach ($res->headers as $header) { | |
| 148 header($header); | |
| 149 } | |
| 99 print $res; | 150 print $res; |
| 100 } | 151 } |
| 101 | 152 |
| 102 | 153 |
| 103 } | 154 } |
