Formats: Remove display & related method (#2776)

Format should not be responsible for sending HTTP response.
This commit is contained in:
Jan Tojnar 2022-06-07 18:05:33 +02:00 committed by GitHub
parent e85932b1a5
commit fb501652d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 11 additions and 178 deletions

View File

@ -244,8 +244,14 @@ class DisplayAction extends ActionAbstract {
$format = $formatFac->create($format); $format = $formatFac->create($format);
$format->setItems($items); $format->setItems($items);
$format->setExtraInfos($infos); $format->setExtraInfos($infos);
$format->setLastModified($cache->getTime()); $lastModified = $cache->getTime();
$format->display(); $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) { } catch(Error $e) {
error_log($e); error_log($e);
header('Content-Type: text/html', true, $e->getCode()); header('Content-Type: text/html', true, $e->getCode());

View File

@ -14,14 +14,6 @@ Find a [template](#template) at the end of this file
# Functions # 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
The `stringify` function returns the items received by [`setItems`](#the-setitem-function) as string. 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 return ''; // Return items as string
} }
public function display(){
// Implement your code here
echo $this->stringify();
return $this;
}
public function setItems(array $items){ public function setItems(array $items){
$this->items = $items; $this->items = $items;
return $this; return $this;

View File

@ -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: 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) * [sanitizeHtml](#the-sanitizehtml-function)
# Functions # 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
The `sanitizeHtml` function receives an HTML formatted string and returns the string with disabled `<script>`, `<iframe>` and `<link>` tags. The `sanitizeHtml` function receives an HTML formatted string and returns the string with disabled `<script>`, `<iframe>` and `<link>` tags.

View File

@ -156,14 +156,6 @@ EOD;
return $toReturn; return $toReturn;
} }
public function display(){
$this
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType();
return parent::display();
}
private function xml_encode($text){ private function xml_encode($text){
return htmlspecialchars($text, ENT_XML1); return htmlspecialchars($text, ENT_XML1);
} }

View File

@ -137,14 +137,6 @@ EOD;
return $toReturn; return $toReturn;
} }
public function display() {
$this
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType();
return parent::display();
}
private function buildButton($format, $query) { private function buildButton($format, $query) {
return <<<EOD return <<<EOD
<a href="./?{$query}"><button class="rss-feed">{$format}</button></a> <a href="./?{$query}"><button class="rss-feed">{$format}</button></a>

View File

@ -122,14 +122,6 @@ class JsonFormat extends FormatAbstract {
return $json; return $json;
} }
public function display(){
$this
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType();
return parent::display();
}
private function isHTML($text) { private function isHTML($text) {
return (strlen(strip_tags($text)) != strlen($text)); return (strlen(strip_tags($text)) != strlen($text));
} }

View File

@ -150,14 +150,6 @@ EOD;
return $toReturn; return $toReturn;
} }
public function display(){
$this
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType();
return parent::display();
}
private function xml_encode($text){ private function xml_encode($text){
return htmlspecialchars($text, ENT_XML1); return htmlspecialchars($text, ENT_XML1);
} }

View File

@ -21,12 +21,4 @@ class PlaintextFormat extends FormatAbstract {
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8'); $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn; return $toReturn;
} }
public function display(){
$this
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType();
return parent::display();
}
} }

View File

@ -24,9 +24,6 @@ abstract class FormatAbstract implements FormatInterface {
/** MIME type of format output */ /** MIME type of format output */
const MIME_TYPE = 'text/plain'; const MIME_TYPE = 'text/plain';
/** @var string|null $contentType The content type */
protected $contentType = null;
/** @var string $charset The charset */ /** @var string $charset The charset */
protected $charset; protected $charset;
@ -65,18 +62,6 @@ abstract class FormatAbstract implements FormatInterface {
return is_null($charset) ? static::DEFAULT_CHARSET : $charset; return is_null($charset) ? static::DEFAULT_CHARSET : $charset;
} }
/**
* 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 * Set the last modified time
* *
@ -87,34 +72,6 @@ abstract class FormatAbstract implements FormatInterface {
$this->lastModified = $lastModified; $this->lastModified = $lastModified;
} }
/**
* 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} * {@inheritdoc}
* *

View File

@ -26,13 +26,6 @@ interface FormatInterface {
*/ */
public function stringify(); public function stringify();
/**
* Display the current data to the user
*
* @return self The format object
*/
public function display();
/** /**
* Set items * Set items
* *

View File

@ -14,21 +14,6 @@ class AtomFormatTest extends TestCase {
private $format; private $format;
private $data; private $data;
/**
* @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 * @dataProvider sampleProvider
* @runInSeparateProcess * @runInSeparateProcess
@ -81,9 +66,7 @@ class AtomFormatTest extends TestCase {
$this->format->setExtraInfos($this->sample->meta); $this->format->setExtraInfos($this->sample->meta);
$this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); $this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
$_ = $this->format->display(); $this->data = $this->format->stringify();
$this->data = $this->getActualOutput();
$this->assertNotFalse(simplexml_load_string($this->data)); $this->assertNotFalse(simplexml_load_string($this->data));
ob_clean();
} }
} }

View File

@ -15,21 +15,6 @@ class JsonFormatTest extends TestCase {
private $format; private $format;
private $data; private $data;
/**
* @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 * @dataProvider sampleProvider
* @runInSeparateProcess * @runInSeparateProcess
@ -82,9 +67,7 @@ class JsonFormatTest extends TestCase {
$this->format->setExtraInfos($this->sample->meta); $this->format->setExtraInfos($this->sample->meta);
$this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); $this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
$_ = $this->format->display(); $this->data = $this->format->stringify();
$this->data = $this->getActualOutput();
$this->assertNotNull(json_decode($this->data), 'invalid JSON output: ' . json_last_error_msg()); $this->assertNotNull(json_decode($this->data), 'invalid JSON output: ' . json_last_error_msg());
ob_clean();
} }
} }

View File

@ -15,21 +15,6 @@ class MrssFormatTest extends TestCase {
private $format; private $format;
private $data; private $data;
/**
* @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 * @dataProvider sampleProvider
* @runInSeparateProcess * @runInSeparateProcess
@ -82,9 +67,7 @@ class MrssFormatTest extends TestCase {
$this->format->setExtraInfos($this->sample->meta); $this->format->setExtraInfos($this->sample->meta);
$this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); $this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
$_ = $this->format->display(); $this->data = $this->format->stringify();
$this->data = $this->getActualOutput();
$this->assertNotFalse(simplexml_load_string($this->data)); $this->assertNotFalse(simplexml_load_string($this->data));
ob_clean();
} }
} }