diff options
author | 2022-06-07 18:05:33 +0200 | |
---|---|---|
committer | 2022-06-07 18:05:33 +0200 | |
commit | fb501652d5ef8519470fa1faa585b3668ebd2a34 (patch) | |
tree | a9603f0c09d6f4b710fbde1c10297330bac87ad1 | |
parent | e85932b1a524f88db6af66d4c3aac3a7983c169b (diff) | |
download | rss-bridge-fb501652d5ef8519470fa1faa585b3668ebd2a34.tar.gz rss-bridge-fb501652d5ef8519470fa1faa585b3668ebd2a34.tar.zst rss-bridge-fb501652d5ef8519470fa1faa585b3668ebd2a34.zip |
Formats: Remove display & related method (#2776)
Format should not be responsible for sending HTTP response.
-rw-r--r-- | actions/DisplayAction.php | 10 | ||||
-rw-r--r-- | docs/08_Format_API/02_FormatInterface.md | 14 | ||||
-rw-r--r-- | docs/08_Format_API/03_FormatAbstract.md | 18 | ||||
-rw-r--r-- | formats/AtomFormat.php | 8 | ||||
-rw-r--r-- | formats/HtmlFormat.php | 8 | ||||
-rw-r--r-- | formats/JsonFormat.php | 8 | ||||
-rw-r--r-- | formats/MrssFormat.php | 8 | ||||
-rw-r--r-- | formats/PlaintextFormat.php | 8 | ||||
-rw-r--r-- | lib/FormatAbstract.php | 43 | ||||
-rw-r--r-- | lib/FormatInterface.php | 7 | ||||
-rw-r--r-- | tests/AtomFormatTest.php | 19 | ||||
-rw-r--r-- | tests/JsonFormatTest.php | 19 | ||||
-rw-r--r-- | tests/MrssFormatTest.php | 19 |
13 files changed, 11 insertions, 178 deletions
diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php index 99dd21ce..52853d36 100644 --- a/actions/DisplayAction.php +++ b/actions/DisplayAction.php @@ -244,8 +244,14 @@ class DisplayAction extends ActionAbstract { $format = $formatFac->create($format); $format->setItems($items); $format->setExtraInfos($infos); - $format->setLastModified($cache->getTime()); - $format->display(); + $lastModified = $cache->getTime(); + $format->setLastModified($lastModified); + if ($lastModified) { + header('Last-Modified: ' . gmdate('D, d M Y H:i:s ', $lastModified) . 'GMT'); + } + header('Content-Type: ' . $format->getMimeType() . '; charset=' . $format->getCharset()); + + echo $format->stringify(); } catch(Error $e) { error_log($e); header('Content-Type: text/html', true, $e->getCode()); diff --git a/docs/08_Format_API/02_FormatInterface.md b/docs/08_Format_API/02_FormatInterface.md index 28ac60b3..88448c38 100644 --- a/docs/08_Format_API/02_FormatInterface.md +++ b/docs/08_Format_API/02_FormatInterface.md @@ -14,14 +14,6 @@ Find a [template](#template) at the end of this file # Functions -## The `display` function - -The `display` function shows the contents to the user and must return the object instance. - -```PHP -display(): self -``` - ## The `stringify` function The `stringify` function returns the items received by [`setItems`](#the-setitem-function) as string. @@ -109,12 +101,6 @@ class MyTypeFormat implements FormatInterface { return ''; // Return items as string } - public function display(){ - // Implement your code here - echo $this->stringify(); - return $this; - } - public function setItems(array $items){ $this->items = $items; return $this; diff --git a/docs/08_Format_API/03_FormatAbstract.md b/docs/08_Format_API/03_FormatAbstract.md index 82e42718..8cf0418f 100644 --- a/docs/08_Format_API/03_FormatAbstract.md +++ b/docs/08_Format_API/03_FormatAbstract.md @@ -1,27 +1,9 @@ The `FormatAbstract` class implements the [`FormatInterface`](../08_Format_API/02_FormatInterface.md) interface with basic functional behavior and adds common helper functions for new formats: -* [setContentType](#the-setcontenttype-function) -* [callContentType](#the-callcontenttype-function) * [sanitizeHtml](#the-sanitizehtml-function) # Functions -## The `setContentType` function - -The `setContentType` function receives a string defining the content type for the HTML header and must return the object instance. - -```PHP -setContentType(string): self -``` - -## The `callContentType` function - -The `callContentType` function applies the content type to the header data and must return the object instance. - -```PHP -callContentType(): self -``` - ## The `sanitizeHtml` function The `sanitizeHtml` function receives an HTML formatted string and returns the string with disabled `<script>`, `<iframe>` and `<link>` tags. diff --git a/formats/AtomFormat.php b/formats/AtomFormat.php index 62ab3818..81aaf441 100644 --- a/formats/AtomFormat.php +++ b/formats/AtomFormat.php @@ -156,14 +156,6 @@ EOD; return $toReturn; } - public function display(){ - $this - ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset()) - ->callContentType(); - - return parent::display(); - } - private function xml_encode($text){ return htmlspecialchars($text, ENT_XML1); } diff --git a/formats/HtmlFormat.php b/formats/HtmlFormat.php index b9a7aba1..b3f02099 100644 --- a/formats/HtmlFormat.php +++ b/formats/HtmlFormat.php @@ -137,14 +137,6 @@ EOD; return $toReturn; } - public function display() { - $this - ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset()) - ->callContentType(); - - return parent::display(); - } - private function buildButton($format, $query) { return <<<EOD <a href="./?{$query}"><button class="rss-feed">{$format}</button></a> diff --git a/formats/JsonFormat.php b/formats/JsonFormat.php index f0aa0e65..90bae863 100644 --- a/formats/JsonFormat.php +++ b/formats/JsonFormat.php @@ -122,14 +122,6 @@ class JsonFormat extends FormatAbstract { return $json; } - public function display(){ - $this - ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset()) - ->callContentType(); - - return parent::display(); - } - private function isHTML($text) { return (strlen(strip_tags($text)) != strlen($text)); } diff --git a/formats/MrssFormat.php b/formats/MrssFormat.php index 8bf569ae..0c51104a 100644 --- a/formats/MrssFormat.php +++ b/formats/MrssFormat.php @@ -150,14 +150,6 @@ EOD; return $toReturn; } - public function display(){ - $this - ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset()) - ->callContentType(); - - return parent::display(); - } - private function xml_encode($text){ return htmlspecialchars($text, ENT_XML1); } diff --git a/formats/PlaintextFormat.php b/formats/PlaintextFormat.php index 5a0522cf..a1ef9e7f 100644 --- a/formats/PlaintextFormat.php +++ b/formats/PlaintextFormat.php @@ -21,12 +21,4 @@ class PlaintextFormat extends FormatAbstract { $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8'); return $toReturn; } - - public function display(){ - $this - ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset()) - ->callContentType(); - - return parent::display(); - } } diff --git a/lib/FormatAbstract.php b/lib/FormatAbstract.php index cbc7dc97..5514b0b5 100644 --- a/lib/FormatAbstract.php +++ b/lib/FormatAbstract.php @@ -24,9 +24,6 @@ abstract class FormatAbstract implements FormatInterface { /** MIME type of format output */ const MIME_TYPE = 'text/plain'; - /** @var string|null $contentType The content type */ - protected $contentType = null; - /** @var string $charset The charset */ protected $charset; @@ -66,18 +63,6 @@ abstract class FormatAbstract implements FormatInterface { } /** - * Set the content type - * - * @param string $contentType The content type - * @return self The format object - */ - protected function setContentType($contentType){ - $this->contentType = $contentType; - - return $this; - } - - /** * Set the last modified time * * @param int $lastModified The last modified time @@ -88,34 +73,6 @@ abstract class FormatAbstract implements FormatInterface { } /** - * Send header with the currently specified content type - * - * @throws \LogicException if the content type is not set - * @throws \LogicException if the content type is not a string - * - * @return void - */ - protected function callContentType(){ - if(empty($this->contentType)) - throw new \LogicException('Content-Type is not set!'); - - if(!is_string($this->contentType)) - throw new \LogicException('Content-Type must be a string!'); - - header('Content-Type: ' . $this->contentType); - } - - /** {@inheritdoc} */ - public function display(){ - if ($this->lastModified) { - header('Last-Modified: ' . gmdate('D, d M Y H:i:s ', $this->lastModified) . 'GMT'); - } - echo $this->stringify(); - - return $this; - } - - /** * {@inheritdoc} * * @param array $items {@inheritdoc} diff --git a/lib/FormatInterface.php b/lib/FormatInterface.php index 82049d41..5fd46ef9 100644 --- a/lib/FormatInterface.php +++ b/lib/FormatInterface.php @@ -27,13 +27,6 @@ interface FormatInterface { public function stringify(); /** - * Display the current data to the user - * - * @return self The format object - */ - public function display(); - - /** * Set items * * @param array $bridges The items diff --git a/tests/AtomFormatTest.php b/tests/AtomFormatTest.php index 91d6c1d8..1d692c40 100644 --- a/tests/AtomFormatTest.php +++ b/tests/AtomFormatTest.php @@ -17,21 +17,6 @@ class AtomFormatTest extends TestCase { /** * @dataProvider sampleProvider * @runInSeparateProcess - * @requires function xdebug_get_headers - */ - public function testHeaders($path) { - $this->setSample($path); - $this->initFormat(); - - $this->assertContains( - 'Content-Type: application/atom+xml; charset=' . $this->format->getCharset(), - xdebug_get_headers() - ); - } - - /** - * @dataProvider sampleProvider - * @runInSeparateProcess */ public function testOutput($path) { $this->setSample($path); @@ -81,9 +66,7 @@ class AtomFormatTest extends TestCase { $this->format->setExtraInfos($this->sample->meta); $this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); - $_ = $this->format->display(); - $this->data = $this->getActualOutput(); + $this->data = $this->format->stringify(); $this->assertNotFalse(simplexml_load_string($this->data)); - ob_clean(); } } diff --git a/tests/JsonFormatTest.php b/tests/JsonFormatTest.php index 9e19fe8f..9b536c18 100644 --- a/tests/JsonFormatTest.php +++ b/tests/JsonFormatTest.php @@ -18,21 +18,6 @@ class JsonFormatTest extends TestCase { /** * @dataProvider sampleProvider * @runInSeparateProcess - * @requires function xdebug_get_headers - */ - public function testHeaders($path) { - $this->setSample($path); - $this->initFormat(); - - $this->assertContains( - 'Content-Type: application/json; charset=' . $this->format->getCharset(), - xdebug_get_headers() - ); - } - - /** - * @dataProvider sampleProvider - * @runInSeparateProcess */ public function testOutput($path) { $this->setSample($path); @@ -82,9 +67,7 @@ class JsonFormatTest extends TestCase { $this->format->setExtraInfos($this->sample->meta); $this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); - $_ = $this->format->display(); - $this->data = $this->getActualOutput(); + $this->data = $this->format->stringify(); $this->assertNotNull(json_decode($this->data), 'invalid JSON output: ' . json_last_error_msg()); - ob_clean(); } } diff --git a/tests/MrssFormatTest.php b/tests/MrssFormatTest.php index 5ece3904..ed7dc3a3 100644 --- a/tests/MrssFormatTest.php +++ b/tests/MrssFormatTest.php @@ -18,21 +18,6 @@ class MrssFormatTest extends TestCase { /** * @dataProvider sampleProvider * @runInSeparateProcess - * @requires function xdebug_get_headers - */ - public function testHeaders($path) { - $this->setSample($path); - $this->initFormat(); - - $this->assertContains( - 'Content-Type: application/rss+xml; charset=' . $this->format->getCharset(), - xdebug_get_headers() - ); - } - - /** - * @dataProvider sampleProvider - * @runInSeparateProcess */ public function testOutput($path) { $this->setSample($path); @@ -82,9 +67,7 @@ class MrssFormatTest extends TestCase { $this->format->setExtraInfos($this->sample->meta); $this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); - $_ = $this->format->display(); - $this->data = $this->getActualOutput(); + $this->data = $this->format->stringify(); $this->assertNotFalse(simplexml_load_string($this->data)); - ob_clean(); } } |