Beispiel #1
0
 /**
  * Create a new HTTP text response.
  * 
  * @param string $contents Text to be transfered as response body.
  * @param string $type media type of the text payload.
  * @param string $charset Charset to be used (default to UTF-8)..
  */
 public function __construct(string $contents, string $type = 'text/plain', string $charset = null)
 {
     $type = new ContentType($type);
     if ($type->getMediaType()->isText()) {
         $type->setParam('charset', $charset ?? 'utf-8');
     }
     parent::__construct(Http::OK, ['Content-Type' => (string) $type]);
     $this->body = new StringBody($contents);
 }
Beispiel #2
0
 /**
  * Create HTTP file response.
  * 
  * @param string $file Absolute path of the file to be transfered.
  * @param string $type Media type of the file (will be guessed from extension if not provided).
  * @param string $charset Charset to be supplied when media type is text format.
  */
 public function __construct(string $file, string $type = null, string $charset = null)
 {
     $type = new ContentType($type ?? Filesystem::guessMimeTypeFromFilename($file));
     if ($type->getMediaType()->isText()) {
         $type->setParam('charset', $charset ?? 'utf-8');
     }
     parent::__construct(Http::OK, ['Content-Type' => (string) $type]);
     $this->body = new FileBody($file);
     $this->file = $file;
 }