-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathViewModel.php
More file actions
180 lines (160 loc) · 3.49 KB
/
ViewModel.php
File metadata and controls
180 lines (160 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Cubex\View;
use Cubex\Cubex;
use Cubex\CubexAwareTrait;
use Cubex\I18n\TranslationTrait;
use Cubex\ICubexAware;
use Illuminate\Contracts\Support\Renderable as RenderableInterface;
use Packaged\Helpers\Path;
abstract class ViewModel implements RenderableInterface, ICubexAware
{
use CubexAwareTrait;
use TranslationTrait;
protected $_templateDirName = 'Templates';
/**
* @var string Directory containing all
*/
protected $_templateDir;
/**
* @var string Template file (without extension) to load
*/
protected $_templateFile;
/**
* @var int Depth of the view
*/
protected $_viewDepth = 0;
protected $_callingClass;
/**
* Set the base directory for templates to be read from
*
* @param $directory
*
* @return $this
*/
public function setTemplateDir($directory)
{
$this->_templateDir = $directory;
return $this;
}
/**
* Get the base directory templates are stored
*
* @return null|string
*/
public function getTemplateDir()
{
if($this->_templateDir === null)
{
$this->_calculateTemplateDefaults();
}
return $this->_templateDir;
}
/**
* Set the template file manually for this view
*
* @param $filename
*
* @return $this
*/
public function setTemplateFile($filename)
{
$this->_templateFile = $filename;
return $this;
}
/**
* Get the template file (excluding extension) relative to the template dir
*
* @return null|string
*/
public function getTemplateFile()
{
if($this->_templateFile === null)
{
$this->_calculateTemplateDefaults();
}
return $this->_templateFile;
}
/**
* Calculate the base template directory
*/
protected function _calculateTemplateDefaults()
{
if($this->_callingClass === null)
{
$this->_callingClass = static::class;
}
else if(!is_scalar($this->_callingClass))
{
$this->_callingClass = get_class($this->_callingClass);
}
$parts = explode('\\', $this->_callingClass);
$nesting = [];
if(!empty($parts) && in_array('Views', $parts))
{
$parts = array_reverse($parts);
foreach($parts as $part)
{
//Looks for the views directory
if($part === 'Views')
{
break;
}
$nesting[] = $part;
$this->_viewDepth++;
}
}
else
{
$nesting[] = class_basename($this->_callingClass);
$this->_viewDepth = 1;
}
$this->_templateDir = dirname(
(new \ReflectionClass($this->_callingClass))->getFileName()
);
for($i = 0; $i < $this->_viewDepth; $i++)
{
$this->_templateDir = dirname($this->_templateDir);
}
$this->_templateDir = Path::system(
$this->_templateDir,
$this->_templateDirName
);
if($this->_templateFile === null)
{
$this->_templateFile = Path::custom(
DIRECTORY_SEPARATOR,
array_reverse($nesting)
);
}
}
/**
* Get the full path to the template file for this viewmodel
*
* @param string $extension
*
* @return string
*/
public function getTemplatePath($extension = '.phtml')
{
return Path::system(
$this->getTemplateDir(),
$this->getTemplateFile()
) . $extension;
}
/**
* Convert the view model to a string by calling render
*
* @return string
*/
public function __toString()
{
try
{
return $this->render();
}
catch(\Exception $e)
{
return Cubex::exceptionAsString($e);
}
}
}