Skip to content

Commit 98e06c3

Browse files
committed
implement view feature
1 parent 7993a9d commit 98e06c3

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

src/Router.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use MiladRahimi\PhpRouter\Routing\Route;
1313
use MiladRahimi\PhpRouter\Routing\Storekeeper;
1414
use MiladRahimi\PhpRouter\Routing\Repository;
15-
use MiladRahimi\PhpRouter\Services\HttpPublisher;
16-
use MiladRahimi\PhpRouter\Services\Publisher;
15+
use MiladRahimi\PhpRouter\Publisher\HttpPublisher;
16+
use MiladRahimi\PhpRouter\View\PhpView;
17+
use MiladRahimi\PhpRouter\Publisher\Publisher;
18+
use MiladRahimi\PhpRouter\View\View;
1719
use Psr\Container\ContainerInterface;
1820
use Laminas\Diactoros\ServerRequestFactory;
1921

@@ -97,6 +99,19 @@ public static function create(): self
9799
return $container->instantiate(Router::class);
98100
}
99101

102+
/**
103+
* Setup (enable) View
104+
* @link View
105+
*
106+
* @param string $directory
107+
*/
108+
public function setupView(string $directory): void
109+
{
110+
$this->container->singleton(View::class, function () use ($directory) {
111+
return new PhpView($directory);
112+
});
113+
}
114+
100115
/**
101116
* Group routes with the given common attributes
102117
*

src/View/PhpView.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\View;
4+
5+
use Laminas\Diactoros\Response\EmptyResponse;
6+
7+
/**
8+
* Class PhpView
9+
* It makes views from PHP and HTML/PHP files
10+
*
11+
* @package MiladRahimi\PhpRouter\Services
12+
*/
13+
class PhpView implements View
14+
{
15+
/**
16+
* The root directory of view files
17+
*
18+
* @var string
19+
*/
20+
private $directory;
21+
22+
/**
23+
* Constructor
24+
*
25+
* @param string $directory
26+
*/
27+
public function __construct(string $directory)
28+
{
29+
$this->directory = $directory;
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function make(string $name, array $data = [], int $httpStatus = 200, array $httpHeaders = [])
36+
{
37+
$file = str_replace('.', DIRECTORY_SEPARATOR, $name) . '.phtml';
38+
$path = join('/', [$this->directory, $file]);
39+
40+
http_response_code($httpStatus);
41+
42+
foreach ($httpHeaders as $name => $values) {
43+
@header($name . ': ' . $values);
44+
}
45+
46+
extract($data);
47+
48+
/** @noinspection PhpIncludeInspection */
49+
require $path;
50+
51+
return null;
52+
}
53+
}

src/View/View.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\View;
4+
5+
/**
6+
* Interface View
7+
* It makes views
8+
*
9+
* @package MiladRahimi\PhpRouter\Services
10+
*/
11+
interface View
12+
{
13+
/**
14+
* Make a view
15+
*
16+
* @param string $name
17+
* @param array $data
18+
* @param int $httpStatus
19+
* @param string[] $httpHeaders
20+
* @return mixed
21+
*/
22+
public function make(string $name, array $data = [], int $httpStatus = 200, array $httpHeaders = []);
23+
}

tests/Features/ViewTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\Tests\Features;
4+
5+
use MiladRahimi\PhpRouter\Router;
6+
use MiladRahimi\PhpRouter\Tests\TestCase;
7+
use MiladRahimi\PhpRouter\View\View;
8+
use Throwable;
9+
10+
class ViewTest extends TestCase
11+
{
12+
/**
13+
* @throws Throwable
14+
*/
15+
public function test_with_the_sample_view()
16+
{
17+
ob_start();
18+
19+
$router = Router::create();
20+
$router->setupView(__DIR__ . '/../resources/views');
21+
22+
$router->get('/', function(View $view) {
23+
return $view->make('sample', ['user' => 'Milad']);
24+
});
25+
$router->dispatch();
26+
27+
$this->assertEquals('<h1>Hello Milad</h1>', ob_get_clean());
28+
}
29+
30+
/**
31+
* @throws Throwable
32+
*/
33+
public function test_with_the_sample_view_and_status_201_and_headers()
34+
{
35+
ob_start();
36+
37+
$router = Router::create();
38+
$router->setupView(__DIR__ . '/../resources/views');
39+
40+
$router->get('/', function(View $view) {
41+
return $view->make('sample', ['user' => 'Milad'], 201, [
42+
'X-Powered-By' => 'PhpRouter Test',
43+
]);
44+
});
45+
$router->dispatch();
46+
47+
$this->assertEquals('<h1>Hello Milad</h1>', ob_get_clean());
48+
$this->assertEquals(201, http_response_code());
49+
}
50+
}

tests/resources/views/sample.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello <?php echo $user; ?></h1>

0 commit comments

Comments
 (0)