Skip to content

Commit

Permalink
controller no longer extends Presenter class
Browse files Browse the repository at this point in the history
  • Loading branch information
dakorpar committed Aug 30, 2021
1 parent 00704f0 commit af143cb
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Wedo\Api\Controllers;

use Nette\Application\AbortException;
use Nette\Application\IPresenter;
use Nette\Application\Request;
use Nette\Application\Response;
use Nette\Application\Responses\JsonResponse;
use Nette\Application\UI\Presenter;
use Nette\Http\IRequest;
use Nette\Http\IResponse;
use Nette\Localization\Translator;
use Nette\Utils\Json;
use Nette\Utils\Strings;
Expand All @@ -32,7 +34,7 @@
/**
* @codeCoverageIgnore To hard to test ATM and this class is not likely to change
*/
class Controller extends Presenter
class Controller implements IPresenter
{

public mixed $payload = [];
Expand All @@ -43,13 +45,18 @@ class Controller extends Presenter

protected Translator $translator;

private IRequest $httpRequest;

private IResponse $httpResponse;

private IResponse $response;

/**
* @return JsonResponse
*/
public function run(Request $request): Response
{
$this->request = $request;
$this->setParent($this->getParent(), $request->getPresenterName());
if ($this->getHttpRequest()->getHeader('origin') !== null) {
$this->getHttpResponse()->addHeader('Access-Control-Allow-Origin', $this->getHttpRequest()->getHeader('origin'));
}
Expand Down Expand Up @@ -79,6 +86,41 @@ public function injectTranslator(Translator $translator): void
$this->translator = $translator;
}

final public function getHttpRequest(): IRequest
{
return $this->httpRequest;
}

final public function getHttpResponse(): IResponse
{
return $this->httpResponse;
}

final public function injectRequestAndResponse(
IRequest $httpRequest,
IResponse $httpResponse
): void
{
$this->httpRequest = $httpRequest;
$this->httpResponse = $httpResponse;
}

public function sendJson(mixed $item): void
{
$this->payload = $item;
$this->terminate();
}

/**
* Correctly terminates controller.
*
* @throws AbortException
*/
public function terminate(): void
{
throw new AbortException();
}

/**
* @param mixed[] $params
* @throws NotFoundException
Expand Down

0 comments on commit af143cb

Please sign in to comment.