You are here

class JsonFileResponse in Helper 8

A JSON PSR-7 response implementation.

Allows creating a response by passing a file URI to the constructor; by default, loads the data from the file as the body (assumes the file already contains serialized JSON), sets a status code of 200 and sets the Content-Type header to application/json.

Hierarchy

  • class \Drupal\helper\Response\JsonFileResponse extends \GuzzleHttp\Psr7\Response

Expanded class hierarchy of JsonFileResponse

File

src/Response/JsonFileResponse.php, line 15

Namespace

Drupal\helper\Response
View source
class JsonFileResponse extends Response {

  /**
   * {@inheritdoc}
   *
   * @param string $uri
   *   The URI containing an encoded JSON string.
   * @param int $status
   *   The status.
   * @param array $headers
   *   The headers.
   * @param mixed $version
   *   The version.
   * @param bool $reason
   *   The reason.
   */
  public function __construct($uri, $status = 200, array $headers = [], $version = '1.1', $reason = NULL) {
    if (!is_file($uri)) {
      throw new \InvalidArgumentException("URI {$uri} is not a file.");
    }
    if (!is_readable($uri)) {
      throw new \InvalidArgumentException("URI {$uri} is not readable.");
    }
    $body = fopen($uri, 'r');
    $headers += [
      'Content-Type' => 'application/json',
    ];
    parent::__construct($status, $headers, $body, $version, $reason);
  }

}

Members