SmartyEngine::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2014-2019 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelSmarty\Engines;
21
22
use Illuminate\Contracts\View\Engine as EngineInterface;
23
use Symfony\Component\Debug\Exception\FatalThrowableError;
24
use Throwable;
25
use Ytake\LaravelSmarty\Smarty;
26
27
/**
28
 * Class SmartyEngine
29
 *
30
 * @author  yuuki.takezawa <[email protected]>
31
 * @license http://opensource.org/licenses/MIT MIT
32
 */
33
class SmartyEngine implements EngineInterface
34
{
35
    /** @var Smarty $smarty */
36
    protected $smarty;
37
38
    /**
39
     * @param Smarty $smarty
40
     */
41
    public function __construct(Smarty $smarty)
42
    {
43
        $this->smarty = $smarty;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function get($path, array $data = [])
50
    {
51
        return $this->evaluatePath($path, $data);
52
    }
53
54
    /**
55
     * Get the evaluated contents of the view at the given path.
56
     *
57
     * @param string $path
58
     * @param array  $data
59
     *
60
     * @throws \Exception
61
     * @return string
62
     */
63
    protected function evaluatePath(string $path, array $data = [])
64
    {
65
        extract($data, EXTR_SKIP);
66
        try {
67
            if (!$this->smarty->isCached($path)) {
68
                foreach ($data as $var => $val) {
69
                    $this->smarty->assign($var, $val);
70
                }
71
            }
72
            // render
73
            $cacheId = isset($data['smarty.cache_id']) ? $data['smarty.cache_id'] : null;
74
            $compileId = isset($data['smarty.compile_id']) ? $data['smarty.compile_id'] : null;
75
76
            return $this->smarty->fetch($path, $cacheId, $compileId);
77
            // @codeCoverageIgnoreStart
78
        } catch (\SmartyException $e) {
79
            $this->handleViewException($e);
80
        } catch (Throwable $e) {
81
            $this->handleViewException(new FatalThrowableError($e));
82
        }
83
    }
84
    // @codeCoverageIgnoreEnd
85
86
    /**
87
     * @codeCoverageIgnore
88
     *
89
     * @param \Exception $e
90
     *
91
     * @throws \Exception
92
     */
93
    protected function handleViewException(\Exception $e)
94
    {
95
        throw $e;
96
    }
97
}
98