Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2014-2022 Yuuki Takezawa
16
 *
17
 */
18
 
19
declare(strict_types=1);
20
 
21
namespace Ytake\LaravelSmarty\Engines;
22
 
23
use Illuminate\Contracts\View\Engine as EngineInterface;
24
use Throwable;
25
use Ytake\LaravelSmarty\Smarty;
26
 
27
use function extract;
28
 
29
use const EXTR_SKIP;
30
 
31
/**
32
 * Class SmartyEngine
33
 *
34
 * @author  yuuki.takezawa <yuuki.takezawa@comnect.jp.net>
35
 * @license http://opensource.org/licenses/MIT MIT
36
 */
37
class SmartyEngine implements EngineInterface
38
{
39
    /**
40
     * @param Smarty $smarty
41
     */
42
    public function __construct(
43
        protected Smarty $smarty
44
    ) {
45
    }
46
 
47
    /**
48
     * {@inheritdoc}
49
     * @throws Throwable
50
     */
51
    public function get($path, array $data = []): string
52
    {
53
        return $this->evaluatePath($path, $data);
54
    }
55
 
56
    /**
57
     * Get the evaluated contents of the view at the given path.
58
     *
59
     * @param string $path
60
     * @param array $data
61
     *
62
     * @return string
63
     * @throws Throwable
64
     */
65
    protected function evaluatePath(string $path, array $data = []): string
66
    {
67
        extract($data, EXTR_SKIP);
68
        if (!$this->smarty->isCached($path)) {
69
            foreach ($data as $var => $val) {
70
                $this->smarty->assign($var, $val);
71
            }
72
        }
73
        // render
74
        $cacheId = $data['smarty.cache_id'] ?? null;
75
        $compileId = $data['smarty.compile_id'] ?? null;
76
        return $this->smarty->fetch($path, $cacheId, $compileId);
77
    }
78
}