1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class AbstractViewHelper |
4
|
|
|
* |
5
|
|
|
* @method __invoke() |
6
|
|
|
* @package Faulancer\View |
7
|
|
|
* @author Florian Knapp <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
namespace Faulancer\View; |
10
|
|
|
|
11
|
|
|
use Faulancer\Exception\ConfigInvalidException; |
12
|
|
|
use Faulancer\Exception\ConstantMissingException; |
13
|
|
|
use Faulancer\Exception\FileNotFoundException; |
14
|
|
|
use Faulancer\Exception\ServiceNotFoundException; |
15
|
|
|
use Faulancer\Service\Config; |
16
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AbstractViewHelper |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractViewHelper |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** @var ViewController */ |
25
|
|
|
protected $view; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Render a view with given template and variables |
29
|
|
|
* |
30
|
|
|
* @param string $template |
31
|
|
|
* @param array $variables |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
* |
35
|
|
|
* @throws ServiceNotFoundException |
36
|
|
|
* @throws ConfigInvalidException |
37
|
|
|
* @throws FileNotFoundException |
38
|
|
|
*/ |
39
|
|
|
protected function renderView($template = '', array $variables = []) :string |
40
|
|
|
{ |
41
|
|
|
/** @var Config $config */ |
42
|
|
|
$config = $this->getServiceLocator()->get(Config::class); |
43
|
|
|
|
44
|
|
|
$templatePath = $config->get('viewsRoot') . '/helper'; |
45
|
|
|
|
46
|
|
|
return (new ViewController())->setTemplate($templatePath . $template)->setVariables($variables)->render(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param ViewController $view |
51
|
|
|
*/ |
52
|
|
|
public function setView(ViewController $view) |
53
|
|
|
{ |
54
|
|
|
$this->view = $view; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return ViewController |
59
|
|
|
*/ |
60
|
|
|
public function getView() |
61
|
|
|
{ |
62
|
|
|
return $this->view; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get instance of service locator |
67
|
|
|
* |
68
|
|
|
* @return ServiceLocator |
69
|
|
|
*/ |
70
|
|
|
public function getServiceLocator() :ServiceLocator |
71
|
|
|
{ |
72
|
|
|
return ServiceLocator::instance(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |