|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\View\Twig; |
|
6
|
|
|
|
|
7
|
|
|
use Throwable; |
|
8
|
|
|
use Twig\Environment; |
|
9
|
|
|
use Yiisoft\View\TemplateRendererInterface; |
|
10
|
|
|
use Yiisoft\View\ViewInterface; |
|
11
|
|
|
|
|
12
|
|
|
use function array_merge; |
|
13
|
|
|
use function ob_end_clean; |
|
14
|
|
|
use function ob_get_clean; |
|
15
|
|
|
use function ob_get_level; |
|
16
|
|
|
use function ob_implicit_flush; |
|
17
|
|
|
use function ob_start; |
|
18
|
|
|
use function str_replace; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* TemplateRenderer allows using Twig with a View service. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class TemplateRenderer implements TemplateRendererInterface |
|
24
|
|
|
{ |
|
25
|
|
|
public function __construct(private Environment $environment) |
|
26
|
|
|
{ |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function render(ViewInterface $view, string $template, array $parameters): string |
|
30
|
|
|
{ |
|
31
|
|
|
$environment = $this->environment; |
|
32
|
|
|
$renderer = function () use ($view, $template, $parameters, $environment): void { |
|
33
|
|
|
$template = str_replace([$view->getBasePath(), $view->getContext()?->getViewPath() ?? ''], '', $template); |
|
|
|
|
|
|
34
|
|
|
$file = str_replace('\\', '/', $template); |
|
35
|
|
|
|
|
36
|
|
|
echo $environment->render($file, array_merge($parameters, ['this' => $view])); |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
|
|
$obInitialLevel = ob_get_level(); |
|
40
|
|
|
ob_start(); |
|
41
|
|
|
ob_implicit_flush(false); |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
|
|
/** @psalm-suppress PossiblyInvalidFunctionCall,PossiblyNullFunctionCall */ |
|
45
|
|
|
$renderer->bindTo($view)(); |
|
46
|
|
|
return ob_get_clean(); |
|
47
|
|
|
} catch (Throwable $e) { |
|
48
|
|
|
while (ob_get_level() > $obInitialLevel) { |
|
49
|
|
|
ob_end_clean(); |
|
50
|
|
|
} |
|
51
|
|
|
throw $e; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.