Skip to content

Commit c5351a9

Browse files
authored
[Feat] Add View Helper (#157)
* feat: add view helper * test: add git ignore to keep floder clean * feat: add aditional options * refactor: use animus function call view render * feat: add three parameter options
1 parent 989dcbd commit c5351a9

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

src/System/Integrate/helper.php

+17
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,20 @@ function config()
262262
return new System\Collection\CollectionImmutable(app()->get('config'));
263263
}
264264
}
265+
266+
if (!function_exists('view')) {
267+
/**
268+
* Render with costume template engine, wrap in `Route\Controller`.
269+
*/
270+
function view(string $view_path, array $data = [], array $option = []): System\Http\Response
271+
{
272+
/** @var System\Http\Response */
273+
$view = app()->get('view.response');
274+
$status_code = $option['status'] ?? 200;
275+
$headers = $option['header'] ?? [];
276+
277+
return $view($view_path, $data)
278+
->setResponeCode($status_code)
279+
->setHeaders($headers);
280+
}
281+
}

tests/Integrate/Helper/ViewTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace System\Test\Integrate\Helper;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use System\Http\Response;
9+
use System\Integrate\Application;
10+
use System\Text\Str;
11+
use System\View\Templator;
12+
13+
final class ViewTest extends TestCase
14+
{
15+
public function testItCanGetResponeFromeContainer()
16+
{
17+
$app = new Application('/');
18+
19+
$app->set(
20+
'view.response',
21+
fn () => fn (string $view_path, array $portal = []): Response => (new Response())
22+
->setContent(
23+
(new Templator(__DIR__ . '/assets/view/', __DIR__ . '/assets/cache'))
24+
->render($view_path, $portal)
25+
)
26+
);
27+
28+
$view = view('test.php', [], ['status' => 500]);
29+
$this->assertEquals(500, $view->getStatusCode());
30+
$this->assertTrue(
31+
Str::contains($view->getContent(), 'savanna')
32+
);
33+
34+
$app->flush();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<p>savanna</p>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)