1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\CoreBundle\Helper\Templating; |
14
|
|
|
|
15
|
|
|
use ReflectionClass; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
19
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
20
|
|
|
use Twig_Environment as Environment; |
21
|
|
|
use WellCommerce\Bundle\CoreBundle\Controller\ControllerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class TemplatingHelper |
25
|
|
|
* |
26
|
|
|
* @author Adam Piotrowski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class TemplatingHelper implements TemplatingHelperInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var EngineInterface |
32
|
|
|
*/ |
33
|
|
|
protected $engine; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Environment |
37
|
|
|
*/ |
38
|
|
|
protected $environment; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var KernelInterface |
42
|
|
|
*/ |
43
|
|
|
protected $kernel; |
44
|
|
|
|
45
|
|
|
public function __construct(EngineInterface $engine, KernelInterface $kernel, Environment $environment) |
46
|
|
|
{ |
47
|
|
|
$this->engine = $engine; |
48
|
|
|
$this->environment = $environment; |
49
|
|
|
$this->kernel = $kernel; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function render(string $name, array $parameters = []): string |
53
|
|
|
{ |
54
|
|
|
return $this->engine->render($name, $parameters); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function renderTemplateString(string $template, array $parameters = []): string |
58
|
|
|
{ |
59
|
|
|
$template = $this->environment->createTemplate($template); |
60
|
|
|
|
61
|
|
|
return $template->render($parameters); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function renderControllerResponse(ControllerInterface $controller, string $templateName, array $parameters = []): Response |
65
|
|
|
{ |
66
|
|
|
$template = $this->resolveControllerTemplate($controller, $templateName); |
67
|
|
|
|
68
|
|
|
return $this->engine->renderResponse($template, $parameters); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function resolveControllerTemplate(ControllerInterface $controller, string $templateName): string |
72
|
|
|
{ |
73
|
|
|
$reflectionClass = new ReflectionClass($controller); |
74
|
|
|
$controllerName = $this->getControllerLogicalName($reflectionClass); |
75
|
|
|
$bundleName = $this->getBundleName($reflectionClass); |
76
|
|
|
|
77
|
|
|
return sprintf('%s:%s:%s.html.twig', $bundleName, $controllerName, $templateName); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getControllerLogicalName(ReflectionClass $reflectionClass): string |
81
|
|
|
{ |
82
|
|
|
$className = $reflectionClass->getName(); |
|
|
|
|
83
|
|
|
preg_match('/Controller\\\(.+)Controller$/', $className, $matchController); |
84
|
|
|
|
85
|
|
|
return $matchController[1]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getBundleName(ReflectionClass $reflectionClass): string |
89
|
|
|
{ |
90
|
|
|
$currentBundle = $this->getBundleForClass($reflectionClass); |
91
|
|
|
|
92
|
|
|
return $currentBundle->getName(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function getBundleForClass(ReflectionClass $reflectionClass): BundleInterface |
96
|
|
|
{ |
97
|
|
|
$bundles = $this->kernel->getBundles(); |
98
|
|
|
|
99
|
|
|
do { |
100
|
|
|
$namespace = $reflectionClass->getNamespaceName(); |
101
|
|
|
foreach ($bundles as $bundle) { |
102
|
|
|
if (0 === strpos($namespace, $bundle->getNamespace())) { |
103
|
|
|
return $bundle; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
$reflectionClass = $reflectionClass->getParentClass(); |
107
|
|
|
} while ($reflectionClass); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|