Skip to content

Commit

Permalink
view
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-suffi committed Mar 9, 2018
1 parent 140964c commit 8724a21
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/Core/Components/View/PhpView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace suffi\Simple\Components\View;

use suffi\Simple\Core\View;

class PhpView extends View
{
public function render($template, $data = [])
{
echo $this->renderFile($template, $data);
}

/**
* @param $template
* @param $data
* @return string
* @throws \Exception
* @throws \Throwable
*/
protected function renderFile($template, $data): string
{
ob_start();
ob_implicit_flush(false);
extract($data, EXTR_OVERWRITE);
try {
require $this->templateDir . $template . '.php';
return ob_get_clean();
} catch (\Exception $e) {
ob_clean();
throw $e;
} catch (\Throwable $e) {
ob_clean();
throw $e;
}
}

}
11 changes: 9 additions & 2 deletions src/Core/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace suffi\Simple\Core;

/**
* todo
* Class View
*
* Класс для реализации уровня представления
Expand All @@ -14,6 +15,12 @@ abstract class View
const CSS = 'CSS';
const JS = 'JS';

/**
* Папка с шаблонами
* @var string
*/
public $templateDir = 'View';

/**
* Отображение шаблона
* @param string $template Название шаблона
Expand All @@ -27,14 +34,14 @@ abstract public function render($template, $data = []);
* @param $addTemplateDir
* @return mixed
*/
abstract public function addTemplateDir($addTemplateDir);
//abstract public function addTemplateDir($addTemplateDir);

/**
* Получение директории с шаблоном
* @param $index
* @return mixed
*/
abstract public function getTemplateDir($index);
//abstract public function getTemplateDir($index);

/**
* Очистка временных данных
Expand Down
29 changes: 29 additions & 0 deletions tests/Components/View/PhpViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace suffi\Simple\tests\Components\View;


use PHPUnit\Framework\TestCase;
use suffi\Simple\Components\View\PhpView;

class PhpViewTest extends TestCase
{

public function testRender()
{
$view = new PhpView();
$view->templateDir = __DIR__ . DIRECTORY_SEPARATOR;

ob_start();
ob_implicit_flush(false);
$view->render("view", [
"name" => "Kristian",
"age" => 30
]);
$r = ob_get_clean();

$this->assertEquals($r, "Привет, я Kristian, мне 30 лет.");

}

}
1 change: 1 addition & 0 deletions tests/Components/View/view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Привет, я <?= $name ?>, мне <?= $age ?> лет.
3 changes: 3 additions & 0 deletions tests/phpunit_clover.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
<exclude>
<directory suffix=".php">../src/Core/Exceptions</directory>
</exclude>
</whitelist>
</filter>
<logging>
Expand Down

0 comments on commit 8724a21

Please sign in to comment.