|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of EC-CUBE |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* http://www.ec-cube.co.jp/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Eccube\Twig\Extension; |
|
15
|
|
|
|
|
16
|
|
|
use Eccube\Entity\Block; |
|
17
|
|
|
use Eccube\Repository\BlockRepository; |
|
18
|
|
|
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime; |
|
19
|
|
|
use Twig\Environment; |
|
20
|
|
|
use Twig\Extension\AbstractExtension; |
|
21
|
|
|
use Twig\TwigFunction; |
|
22
|
|
|
|
|
23
|
|
|
class EccubeDynamicBlockExtension extends AbstractExtension |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var BlockRepository */ |
|
26
|
|
|
protected $blockRepository; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(BlockRepository $blockRepository) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->blockRepository = $blockRepository; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getFunctions() |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
new TwigFunction('eccube_dynamic_block', [$this, 'eccube_dynamic_block'], [ |
|
37
|
|
|
'needs_environment' => true, |
|
38
|
|
|
'needs_context' => true, |
|
39
|
|
|
'pre_escape' => 'html', |
|
40
|
|
|
'is_safe' => [ |
|
41
|
|
|
'html', |
|
42
|
|
|
], |
|
43
|
|
|
]), |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function eccube_dynamic_block(Environment $env, $context, $fileName, $parameters = []) |
|
48
|
|
|
{ |
|
49
|
|
|
$Block = $this->blockRepository->findOneBy([ |
|
50
|
|
|
'file_name' => $fileName, |
|
51
|
|
|
]); |
|
52
|
|
|
if (!($Block instanceof Block)) { |
|
53
|
|
|
@trigger_error($fileName . ' block is not found', E_USER_WARNING); |
|
|
|
|
|
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
if ($Block->isUseController()) { |
|
57
|
|
|
$blockPath = sprintf('block_%s', $fileName); |
|
58
|
|
|
$runtime = $env->getRuntime(HttpKernelRuntime::class); |
|
59
|
|
|
assert($runtime instanceof HttpKernelRuntime); |
|
60
|
|
|
$path = $env->getFunction('path')->getCallable(); |
|
61
|
|
|
return $runtime->renderFragment( |
|
62
|
|
|
$path($blockPath, $parameters) |
|
63
|
|
|
); |
|
64
|
|
|
} else { |
|
65
|
|
|
$template = sprintf('Block/%s.twig', $fileName); |
|
66
|
|
|
return ($env->getFunction('include_dispatch')->getCallable())($context, $template); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: