|
13 | 13 | use MiladRahimi\Router\Exceptions\InvalidControllerException; |
14 | 14 | use MiladRahimi\Router\Exceptions\InvalidMiddlewareException; |
15 | 15 | use MiladRahimi\Router\Exceptions\RouteNotFoundException; |
16 | | -use MiladRahimi\Router\Services\HeaderExposer; |
17 | | -use MiladRahimi\Router\Services\HeaderExposerInterface; |
| 16 | +use MiladRahimi\Router\Services\Publisher; |
| 17 | +use MiladRahimi\Router\Services\PublisherInterface; |
18 | 18 | use Psr\Http\Message\ResponseInterface; |
19 | 19 | use Psr\Http\Message\ServerRequestInterface; |
20 | 20 | use ReflectionException; |
@@ -84,17 +84,17 @@ class Router |
84 | 84 | private $response; |
85 | 85 |
|
86 | 86 | /** |
87 | | - * @var HeaderExposerInterface |
| 87 | + * @var PublisherInterface |
88 | 88 | */ |
89 | | - private $headerExposer; |
| 89 | + private $publisher; |
90 | 90 |
|
91 | 91 | /** |
92 | 92 | * Router constructor. |
93 | 93 | */ |
94 | 94 | public function __construct() |
95 | 95 | { |
96 | 96 | $this->initializeRequestAndResponse(); |
97 | | - $this->headerExposer = new HeaderExposer(); |
| 97 | + $this->publisher = new Publisher(); |
98 | 98 | } |
99 | 99 |
|
100 | 100 | /** |
@@ -211,7 +211,7 @@ public function dispatch() |
211 | 211 | RouteAttributes::URI => $routeAttributes[RouteAttributes::URI], |
212 | 212 | ]; |
213 | 213 |
|
214 | | - $this->publish($this->run($routeAttributes, $routeParameters)); |
| 214 | + $this->publisher->publish($this->run($routeAttributes, $routeParameters)); |
215 | 215 |
|
216 | 216 | return; |
217 | 217 | } |
@@ -384,40 +384,6 @@ function (ReflectionParameter $parameter) use ($parameters) { |
384 | 384 | ); |
385 | 385 | } |
386 | 386 |
|
387 | | - /** |
388 | | - * Publish response |
389 | | - * |
390 | | - * @param mixed $response |
391 | | - * @return bool |
392 | | - */ |
393 | | - private function publish($response = null) |
394 | | - { |
395 | | - $output = fopen("php://output", 'r+'); |
396 | | - |
397 | | - if ($response instanceof ResponseInterface) { |
398 | | - http_response_code($response->getStatusCode()); |
399 | | - |
400 | | - foreach ($response->getHeaders() as $name => $values) { |
401 | | - $value = $response->getHeaderLine($name); |
402 | | - $this->headerExposer->addHeaderLine($name, $value); |
403 | | - } |
404 | | - |
405 | | - fwrite($output, $response->getBody()); |
406 | | - |
407 | | - return fclose($output); |
408 | | - } |
409 | | - |
410 | | - if (is_scalar($response)) { |
411 | | - fwrite($output, $response); |
412 | | - |
413 | | - return fclose($output); |
414 | | - } |
415 | | - |
416 | | - fwrite($output, json_encode($response)); |
417 | | - |
418 | | - return fclose($output); |
419 | | - } |
420 | | - |
421 | 387 | /** |
422 | 388 | * Check if the route matches the uri and extract parameters if it does |
423 | 389 | * |
@@ -688,18 +654,18 @@ public function setResponse(ResponseInterface $response) |
688 | 654 | } |
689 | 655 |
|
690 | 656 | /** |
691 | | - * @return HeaderExposerInterface |
| 657 | + * @return PublisherInterface |
692 | 658 | */ |
693 | | - public function getHeaderExposer(): HeaderExposerInterface |
| 659 | + public function getPublisher(): PublisherInterface |
694 | 660 | { |
695 | | - return $this->headerExposer; |
| 661 | + return $this->publisher; |
696 | 662 | } |
697 | 663 |
|
698 | 664 | /** |
699 | | - * @param HeaderExposerInterface $headerExposer |
| 665 | + * @param PublisherInterface $publisher |
700 | 666 | */ |
701 | | - public function setHeaderExposer(HeaderExposerInterface $headerExposer) |
| 667 | + public function setPublisher(PublisherInterface $publisher) |
702 | 668 | { |
703 | | - $this->headerExposer = $headerExposer; |
| 669 | + $this->publisher = $publisher; |
704 | 670 | } |
705 | 671 | } |
0 commit comments