Skip to content

Commit 66296e6

Browse files
miladrahimiMilad Rahimi
authored andcommitted
implement publisher service
1 parent 5a29cf7 commit 66296e6

File tree

9 files changed

+226
-85
lines changed

9 files changed

+226
-85
lines changed

src/MiladRahimi/Router/Router.php

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use MiladRahimi\Router\Exceptions\InvalidControllerException;
1414
use MiladRahimi\Router\Exceptions\InvalidMiddlewareException;
1515
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;
1818
use Psr\Http\Message\ResponseInterface;
1919
use Psr\Http\Message\ServerRequestInterface;
2020
use ReflectionException;
@@ -84,17 +84,17 @@ class Router
8484
private $response;
8585

8686
/**
87-
* @var HeaderExposerInterface
87+
* @var PublisherInterface
8888
*/
89-
private $headerExposer;
89+
private $publisher;
9090

9191
/**
9292
* Router constructor.
9393
*/
9494
public function __construct()
9595
{
9696
$this->initializeRequestAndResponse();
97-
$this->headerExposer = new HeaderExposer();
97+
$this->publisher = new Publisher();
9898
}
9999

100100
/**
@@ -211,7 +211,7 @@ public function dispatch()
211211
RouteAttributes::URI => $routeAttributes[RouteAttributes::URI],
212212
];
213213

214-
$this->publish($this->run($routeAttributes, $routeParameters));
214+
$this->publisher->publish($this->run($routeAttributes, $routeParameters));
215215

216216
return;
217217
}
@@ -384,40 +384,6 @@ function (ReflectionParameter $parameter) use ($parameters) {
384384
);
385385
}
386386

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-
421387
/**
422388
* Check if the route matches the uri and extract parameters if it does
423389
*
@@ -688,18 +654,18 @@ public function setResponse(ResponseInterface $response)
688654
}
689655

690656
/**
691-
* @return HeaderExposerInterface
657+
* @return PublisherInterface
692658
*/
693-
public function getHeaderExposer(): HeaderExposerInterface
659+
public function getPublisher(): PublisherInterface
694660
{
695-
return $this->headerExposer;
661+
return $this->publisher;
696662
}
697663

698664
/**
699-
* @param HeaderExposerInterface $headerExposer
665+
* @param PublisherInterface $publisher
700666
*/
701-
public function setHeaderExposer(HeaderExposerInterface $headerExposer)
667+
public function setPublisher(PublisherInterface $publisher)
702668
{
703-
$this->headerExposer = $headerExposer;
669+
$this->publisher = $publisher;
704670
}
705671
}

src/MiladRahimi/Router/Services/HeaderExposer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
class HeaderExposer implements HeaderExposerInterface
1212
{
1313
/**
14-
* Add header line to http response headers
15-
*
16-
* @param string $name
17-
* @param string $value
14+
* @inheritdoc
1815
*/
1916
public function addHeaderLine(string $name, string $value)
2017
{
2118
header($name . ': ' . $value);
2219
}
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
public function setResponseCode(int $code)
25+
{
26+
http_response_code($code);
27+
}
2328
}

src/MiladRahimi/Router/Services/HeaderExposerInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ interface HeaderExposerInterface
1717
* @param string $value
1818
*/
1919
public function addHeaderLine(string $name, string $value);
20+
21+
/**
22+
* Set http response code
23+
*
24+
* @param int $code
25+
*/
26+
public function setResponseCode(int $code);
2027
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Milad Rahimi <[email protected]>
5+
* Date: 7/13/2018 AD
6+
* Time: 16:23
7+
*/
8+
9+
namespace MiladRahimi\Router\Services;
10+
11+
use Psr\Http\Message\ResponseInterface;
12+
13+
class Publisher implements PublisherInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $stream;
19+
20+
/**
21+
* @var HeaderExposerInterface
22+
*/
23+
private $headerExposer;
24+
25+
/**
26+
* Publisher constructor.
27+
*/
28+
public function __construct()
29+
{
30+
$this->setStream('php://output');
31+
$this->headerExposer = new HeaderExposer();
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function publish($content)
38+
{
39+
$output = fopen($this->stream, 'r+');
40+
41+
if ($content instanceof ResponseInterface) {
42+
$this->headerExposer->setResponseCode($content->getStatusCode());
43+
44+
foreach ($content->getHeaders() as $name => $values) {
45+
$value = $content->getHeaderLine($name);
46+
$this->headerExposer->addHeaderLine($name, $value);
47+
}
48+
49+
fwrite($output, $content->getBody());
50+
} elseif (is_scalar($content)) {
51+
fwrite($output, $content);
52+
} else {
53+
fwrite($output, json_encode($content));
54+
}
55+
56+
fclose($output);
57+
}
58+
59+
/**
60+
* Set header exposer
61+
*
62+
* @param HeaderExposerInterface $headerExposer
63+
*/
64+
public function setHeaderExposer(HeaderExposerInterface $headerExposer)
65+
{
66+
$this->headerExposer = $headerExposer;
67+
}
68+
69+
/**
70+
* Get header exposer
71+
*
72+
* @return HeaderExposerInterface
73+
*/
74+
public function getHeaderExposer(): HeaderExposerInterface
75+
{
76+
return $this->headerExposer;
77+
}
78+
79+
/**
80+
* @return string
81+
*/
82+
public function getStream(): string
83+
{
84+
return $this->stream;
85+
}
86+
87+
/**
88+
* @param string $stream
89+
*/
90+
public function setStream(string $stream)
91+
{
92+
$this->stream = $stream;
93+
}
94+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Milad Rahimi <[email protected]>
5+
* Date: 7/13/2018 AD
6+
* Time: 16:22
7+
*/
8+
9+
namespace MiladRahimi\Router\Services;
10+
11+
interface PublisherInterface
12+
{
13+
/**
14+
* Publish the output
15+
*
16+
* @param mixed $content
17+
*/
18+
public function publish($content);
19+
}

tests/Classes/Publisher.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Milad Rahimi <[email protected]>
5+
* Date: 7/13/2018 AD
6+
* Time: 17:07
7+
*/
8+
9+
namespace MiladRahimi\Router\Tests\Classes;
10+
11+
use MiladRahimi\Router\Services\PublisherInterface;
12+
use Psr\Http\Message\ResponseInterface;
13+
14+
class Publisher implements PublisherInterface
15+
{
16+
/**
17+
* @var string
18+
*/
19+
public $output = '';
20+
21+
/**
22+
* @var int
23+
*/
24+
public $responseCode = 0;
25+
26+
/**
27+
* @var string[]
28+
*/
29+
public $headerLines = [];
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function publish($content)
35+
{
36+
if ($content instanceof ResponseInterface) {
37+
$this->responseCode = $content->getStatusCode();
38+
39+
foreach ($content->getHeaders() as $name => $values) {
40+
$value = $content->getHeaderLine($name);
41+
$this->headerLines[] = $name . ': ' . $value;
42+
}
43+
44+
$this->output = $content->getBody();
45+
} elseif (is_scalar($content)) {
46+
$this->output = (string)$content;
47+
} else {
48+
$this->output = json_encode($content);
49+
}
50+
}
51+
}

tests/GroupedMappingTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function test_simple_group_routing()
2929

3030
$router->dispatch();
3131

32-
$this->assertEquals('Here I am!', ob_get_contents());
32+
$this->assertEquals('Here I am!', $this->getOutput($router));
3333
}
3434

3535
/**
@@ -51,7 +51,7 @@ public function test_group_routing_with_middleware()
5151

5252
$router->dispatch();
5353

54-
$this->assertEquals('Here I am!', ob_get_contents());
54+
$this->assertEquals('Here I am!', $this->getOutput($router));
5555
$this->assertContains(777, SampleMiddleware::$output);
5656
}
5757

@@ -75,7 +75,7 @@ public function test_group_routing_with_a_group_and_a_route_middleware()
7575

7676
$router->dispatch();
7777

78-
$this->assertEquals('Here I am!', ob_get_contents());
78+
$this->assertEquals('Here I am!', $this->getOutput($router));
7979
$this->assertContains(1001, SampleMiddleware::$output);
8080
$this->assertContains(1002, SampleMiddleware::$output);
8181
}
@@ -99,7 +99,7 @@ public function test_group_routing_with_prefix()
9999

100100
$router->dispatch();
101101

102-
$this->assertEquals('Here I am!', ob_get_contents());
102+
$this->assertEquals('Here I am!', $this->getOutput($router));
103103
}
104104

105105
/**
@@ -121,7 +121,7 @@ public function test_group_routing_with_domain()
121121

122122
$router->dispatch();
123123

124-
$this->assertEquals('Here I am!', ob_get_contents());
124+
$this->assertEquals('Here I am!', $this->getOutput($router));
125125
}
126126

127127
/**
@@ -143,7 +143,7 @@ public function test_group_routing_with_domains_it_should_ignore_group_domain_wh
143143

144144
$router->dispatch();
145145

146-
$this->assertEquals('Here I am!', ob_get_contents());
146+
$this->assertEquals('Here I am!', $this->getOutput($router));
147147
}
148148

149149
/**
@@ -161,7 +161,7 @@ public function test_simple_group_routing_it_should_remove_existing_name_before_
161161

162162
$router->dispatch();
163163

164-
$this->assertEquals('Here I am!', ob_get_contents());
164+
$this->assertEquals('Here I am!', $this->getOutput($router));
165165
$this->assertFalse($router->isRoute('NameForNothing'));
166166
}
167167
}

0 commit comments

Comments
 (0)