|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under New BSD license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/miniview |
|
7
|
|
|
* @licence New BSD |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @link http://maslosoft.com/miniview/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\MiniView; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\MiniView\Interfaces\OwnerAwareInterface; |
|
17
|
|
|
use Maslosoft\MiniView\Interfaces\RendererAwareInterface; |
|
18
|
|
|
use Maslosoft\MiniView\Interfaces\ViewRendererInterface; |
|
19
|
|
|
use ReflectionObject; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* MiniRender |
|
23
|
|
|
* Based on Yii CWidget |
|
24
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
25
|
|
|
* @author Qiang Xue <[email protected]> |
|
26
|
|
|
* @property string @version Current MiniView version |
|
27
|
|
|
*/ |
|
28
|
|
|
class MiniView implements ViewRendererInterface, OwnerAwareInterface, RendererAwareInterface |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
use Traits\OwnerAwareTrait, |
|
32
|
|
|
Traits\RendererAwareTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Current version |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $version = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* View path |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $path = ''; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* View path. This is relative to base path. |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $viewsPath = 'views'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create MiniView instance. If path is not set, it will be based on location of owner class. |
|
54
|
|
|
* @param object $owner |
|
55
|
|
|
* @param string $path |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct($owner, $path = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->path = $path; |
|
60
|
|
|
$this->setOwner($owner); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get current MiniView version |
|
65
|
|
|
* @return string Version string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getVersion() |
|
68
|
|
|
{ |
|
69
|
|
|
if (null === self::$version) |
|
70
|
|
|
{ |
|
71
|
|
|
self::$version = require __DIR__ . '/version.php'; |
|
72
|
|
|
} |
|
73
|
|
|
return self::$version; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Set views path. This is relative path for view resolving. |
|
78
|
|
|
* By default it's `views` folder. |
|
79
|
|
|
* @param string $path |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setViewsPath($path) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->viewsPath = $path; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Render view with data provided. |
|
88
|
|
|
* View name must not contain `php` extension. |
|
89
|
|
|
* @param string $view |
|
90
|
|
|
* @param mixed[] $data |
|
91
|
|
|
* @param bool $return |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function render($view, $data = null, $return = false) |
|
95
|
|
|
{ |
|
96
|
|
|
$viewFile = sprintf('%s/%s/%s', $this->getPath(), $this->viewsPath, $view); |
|
97
|
|
|
return $this->getRenderer()->render($viewFile, $data, $return); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Render file with data provided. |
|
102
|
|
|
* @param string $file |
|
103
|
|
|
* @param mixed[] $data |
|
104
|
|
|
* @param bool $return |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function renderFile($file, $data = null, $return = false) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->getRenderer()->render($file, $data, $return); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
function getPath() |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
if (empty($this->path)) |
|
115
|
|
|
{ |
|
116
|
|
|
$class = new ReflectionObject($this->getOwner()); |
|
117
|
|
|
$this->path = dirname($class->getFileName()); |
|
118
|
|
|
} |
|
119
|
|
|
return $this->path; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.