-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
SmartyEngine.php
97 lines (88 loc) · 2.63 KB
/
SmartyEngine.php
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
<?php
declare(strict_types=1);
/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2014-2019 Yuuki Takezawa
*
*/
namespace Ytake\LaravelSmarty\Engines;
use Illuminate\Contracts\View\Engine as EngineInterface;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Throwable;
use Ytake\LaravelSmarty\Smarty;
/**
* Class SmartyEngine
*
* @author yuuki.takezawa <yuuki.takezawa@comnect.jp.net>
* @license http://opensource.org/licenses/MIT MIT
*/
class SmartyEngine implements EngineInterface
{
/** @var Smarty $smarty */
protected $smarty;
/**
* @param Smarty $smarty
*/
public function __construct(Smarty $smarty)
{
$this->smarty = $smarty;
}
/**
* {@inheritdoc}
*/
public function get($path, array $data = [])
{
return $this->evaluatePath($path, $data);
}
/**
* Get the evaluated contents of the view at the given path.
*
* @param string $path
* @param array $data
*
* @throws \Exception
* @return string
*/
protected function evaluatePath(string $path, array $data = [])
{
extract($data, EXTR_SKIP);
try {
if (!$this->smarty->isCached($path)) {
foreach ($data as $var => $val) {
$this->smarty->assign($var, $val);
}
}
// render
$cacheId = isset($data['smarty.cache_id']) ? $data['smarty.cache_id'] : null;
$compileId = isset($data['smarty.compile_id']) ? $data['smarty.compile_id'] : null;
return $this->smarty->fetch($path, $cacheId, $compileId);
// @codeCoverageIgnoreStart
} catch (\SmartyException $e) {
$this->handleViewException($e);
} catch (Throwable $e) {
$this->handleViewException(new FatalThrowableError($e));
}
}
// @codeCoverageIgnoreEnd
/**
* @codeCoverageIgnore
*
* @param \Exception $e
*
* @throws \Exception
*/
protected function handleViewException(\Exception $e)
{
throw $e;
}
}