|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file was created by developers working at BitBag |
|
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
|
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace BitBag\SyliusCmsPlugin\Twig\Runtime; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface; |
|
14
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\BlockResourceResolverInterface; |
|
15
|
|
|
use Twig\Environment; |
|
16
|
|
|
|
|
17
|
|
|
final class RenderBlockRuntime implements RenderBlockRuntimeInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var BlockRepositoryInterface */ |
|
20
|
|
|
private $blockRepository; |
|
21
|
|
|
|
|
22
|
|
|
/** @var BlockResourceResolverInterface */ |
|
23
|
|
|
private $blockResourceResolver; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Environment */ |
|
26
|
|
|
private $templatingEngine; |
|
27
|
|
|
|
|
28
|
|
|
private const DEFAULT_TEMPLATE = '@BitBagSyliusCmsPlugin/Shop/Block/show.html.twig'; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
BlockRepositoryInterface $blockRepository, |
|
32
|
|
|
BlockResourceResolverInterface $blockResourceResolver, |
|
33
|
|
|
Environment $templatingEngine |
|
34
|
|
|
) { |
|
35
|
|
|
$this->blockRepository = $blockRepository; |
|
36
|
|
|
$this->blockResourceResolver = $blockResourceResolver; |
|
37
|
|
|
$this->templatingEngine = $templatingEngine; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function renderBlock(string $code, ?string $template = null): string |
|
41
|
|
|
{ |
|
42
|
|
|
$block = $this->blockResourceResolver->findOrLog($code); |
|
43
|
|
|
|
|
44
|
|
|
if (null !== $block) { |
|
45
|
|
|
$template = $template ?? self::DEFAULT_TEMPLATE; |
|
46
|
|
|
|
|
47
|
|
|
return $this->templatingEngine->render($template, ['block' => $block]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return ''; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|