1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file was created by the developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusCmsPlugin\Twig\Extension; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusCmsPlugin\Exception\TemplateTypeNotFound; |
16
|
|
|
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface; |
17
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\BlockResourceResolverInterface; |
18
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\BlockTemplateResolverInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Mikołaj Król <[email protected]> |
22
|
|
|
* @author Patryk Drapik <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class RenderBlockExtension extends \Twig_Extension |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var BlockRepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $blockRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var BlockTemplateResolverInterface |
33
|
|
|
*/ |
34
|
|
|
private $blockTemplateResolver; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var BlockResourceResolverInterface |
38
|
|
|
*/ |
39
|
|
|
private $blockResourceResolver; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param BlockRepositoryInterface $blockRepository |
43
|
|
|
* @param BlockTemplateResolverInterface $blockTemplateResolver |
44
|
|
|
* @param BlockResourceResolverInterface $blockResourceResolver |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
BlockRepositoryInterface $blockRepository, |
48
|
|
|
BlockTemplateResolverInterface $blockTemplateResolver, |
49
|
|
|
BlockResourceResolverInterface $blockResourceResolver |
50
|
|
|
) |
51
|
|
|
{ |
52
|
|
|
$this->blockRepository = $blockRepository; |
53
|
|
|
$this->blockTemplateResolver = $blockTemplateResolver; |
54
|
|
|
$this->blockResourceResolver = $blockResourceResolver; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \Twig_SimpleFunction[] |
59
|
|
|
*/ |
60
|
|
|
public function getFunctions(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
new \Twig_SimpleFunction('bitbag_sylius_cms_plugin_render_block', [$this, 'renderBlock'], ['needs_environment' => true, 'is_safe' => ['html']]), |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param \Twig_Environment $twigEnvironment |
69
|
|
|
* @param string $code |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
* |
73
|
|
|
* @throws TemplateTypeNotFound |
74
|
|
|
*/ |
75
|
|
|
public function renderBlock(\Twig_Environment $twigEnvironment, string $code): string |
76
|
|
|
{ |
77
|
|
|
$block = $this->blockResourceResolver->findOrLog($code); |
78
|
|
|
|
79
|
|
|
if (null !== $block) { |
80
|
|
|
$template = $this->blockTemplateResolver->resolveTemplate($block); |
81
|
|
|
|
82
|
|
|
return $twigEnvironment->render($template, ['block' => $block]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return ''; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|