Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
injecting default headers via config added
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jan 22, 2019
1 parent 4d9a9d5 commit fda5556
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/ApplicationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ApplicationResponse
private $controllerResponse;
private $config;
private $view;
private $headers;

public function __construct(
string $controllerClass,
Expand All @@ -33,38 +34,45 @@ public function __construct(
$this->controllerClass = $controllerClass;
$this->controllerResponse = $controllerResponse;
$this->config = $config;
$this->headers = isset( $config->get('app')['default_headers']) ?
$config->get('app')->get('default_headers')->toArray() : [];
$this->view = $view;
}

public function getResponseHeaders() : array
{
return array_merge($this->headers, $this->controllerResponse->getHeaders());
}

public function returnResponse() : ResponseInterface
{
switch ($this->controllerResponse->getReturnType()) {
case Router::HTML:
return new HtmlResponse(
$this->renderResponse(),
$this->controllerResponse->getStatusCode(),
$this->controllerResponse->getHeaders()
$this->getResponseHeaders()
);
break;
case Router::JSON:
return new JsonResponse(
$this->controllerResponse->getData(),
$this->controllerResponse->getStatusCode(),
$this->controllerResponse->getHeaders()
$this->getResponseHeaders()
);
break;
case Router::TEXT:
return new TextResponse(
$this->renderResponse(),
$this->controllerResponse->getStatusCode(),
$this->controllerResponse->getHeaders()
$this->getResponseHeaders()
);
break;
case Router::XML:
return new XmlResponse(
$this->renderResponse(),
$this->controllerResponse->getStatusCode(),
$this->controllerResponse->getHeaders()
$this->getResponseHeaders()
);
break;
case Router::DOWNLOAD:
Expand All @@ -76,27 +84,27 @@ public function returnResponse() : ResponseInterface
return new Response(
$stream,
$this->controllerResponse->getStatusCode(),
$this->controllerResponse->getHeaders()
$this->getResponseHeaders()
);
break;
case Router::REDIRECT:
return new RedirectResponse(
$this->controllerResponse->getMetaData()['uri'],
$this->controllerResponse->getStatusCode(),
$this->controllerResponse->getHeaders()
$this->getResponseHeaders()
);
break;
case Router::CUSTOM:
return new HtmlResponse(
$this->renderResponse(),
$this->controllerResponse->getStatusCode(),
$this->controllerResponse->getHeaders()
$this->getResponseHeaders()
);
break;
case Router::EMPTY:
return new EmptyResponse(
$this->controllerResponse->getStatusCode(),
$this->controllerResponse->getHeaders()
$this->getResponseHeaders()
);
break;
}
Expand Down
3 changes: 3 additions & 0 deletions test/resources/app/config/autoload/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
return [
'app' => [
'app_namespace' => 'MyApp',
'default_headers' => [
'X-Powered-By' => 'Selami/PHP'
]
],
];

0 comments on commit fda5556

Please sign in to comment.