DefaultResponse.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 /**
19  * Default Response implementation.
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
24 
25  private $formatter = null;
26  private $request = null;
27  private $cacheId = null;
28  private $cacheLifetime = null;
29  private $status = 200;
30  private $document = null;
31 
32  /**
33  * Constructor
34  * @param $formatter
35  */
36  public function __construct(Formatter $formatter) {
37  parent::__construct($formatter);
38  $this->formatter = $formatter;
39  }
40 
41  /**
42  * @see Response::setRequest()
43  */
44  public function setRequest(Request $request) {
45  $this->request = $request;
46  if ($request->getResponse() !== $this) {
47  $request->setResponse($this);
48  }
49  }
50 
51  /**
52  * @see Response::getRequest()
53  */
54  public function getRequest() {
55  return $this->request;
56  }
57 
58  /**
59  * @see Response::setCacheId()
60  */
61  public function setCacheId($cacheId) {
62  $this->cacheId = $cacheId;
63  }
64 
65  /**
66  * @see Response::getCacheId()
67  */
68  public function getCacheId() {
69  return $this->cacheId;
70  }
71 
72  /**
73  * @see Response::setCacheLifetime()
74  */
75  public function setCacheLifetime($seconds) {
76  $this->cacheLifetime = $seconds !== null ? intval($seconds) : null;
77  }
78 
79  /**
80  * @see Response::getCacheLifetime()
81  */
82  public function getCacheLifetime() {
83  return $this->cacheLifetime;
84  }
85 
86  /**
87  * @see Response::isCached()
88  */
89  public function isCached() {
90  $format = $this->formatter->getFormat($this->getFormat());
91  return $this->getCacheId() != null && $format->isCached($this);
92  }
93 
94  /**
95  * @see Response::getCacheDate()
96  */
97  public function getCacheDate() {
98  $format = $this->formatter->getFormat($this->getFormat());
99  return $this->isCached() ? $format->getCacheDate($this) : null;
100  }
101 
102  /**
103  * @see Response::setStatus()
104  */
105  public function setStatus($status) {
106  $this->status = $status;
107  }
108 
109  /**
110  * @see Response::getStatus()
111  */
112  public function getStatus() {
113  return $this->status;
114  }
115 
116  /**
117  * @see Response::setFile()
118  */
119  public function setFile($filename, $isDownload, $content='', $type='') {
120  $document = (strlen($content) > 0) ?
121  new MemoryDocument($content, $type, $isDownload, $filename) :
122  new FileDocument($filename, $isDownload);
123  $this->setDocument($document);
124  }
125 
126  /**
127  * @see Response::getFile()
128  */
129  public function getFile() {
130  if ($this->document) {
131  return [
132  'isDownload' => $this->document->isDownload(),
133  'filename' => $this->document->getFilename(),
134  'content' => $this->document->getContent(),
135  'type' => $this->document->getMimeType(),
136  ];
137  }
138  return null;
139  }
140 
141  /**
142  * @see Response::setDocument()
143  */
144  public function setDocument(ResponseDocument $document) {
145  $this->document = $document;
146  $this->setFormat('download');
147  }
148 
149  /**
150  * @see Response::getDocument()
151  */
152  public function getDocument() {
153  return $this->document;
154  }
155 }
156 ?>
FileDocument represents a local file.
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:18
getResponse()
Get the Response instance belonging to the request.
__construct(Formatter $formatter)
Constructor.
AbstractControllerMessage is the base class for request/response implementations.
Formatter is the single entry point for request/response formatting.
Definition: Formatter.php:23
MemoryDocument represents content that resides in memory.
ResponseDocument is the interface for media returned in a response when using the DownloadFormat.
Default Response implementation.
setResponse(Response $response)
Set the Response instance belonging to the request and vice versa.
setFile($filename, $isDownload, $content='', $type='')