1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestIt\KitchensinkBundle\Controller; |
4
|
|
|
|
5
|
|
|
use BestIt\KitchensinkBundle\DataProviderInterface; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Twig\Environment; |
8
|
|
|
use Twig\Error\LoaderError; |
9
|
|
|
use Twig\Error\RuntimeError; |
10
|
|
|
use Twig\Error\SyntaxError; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Simple kitchensink controller. |
14
|
|
|
* |
15
|
|
|
* @author blange <[email protected]> |
16
|
|
|
* @package BestIt\KitchensinkBundle |
17
|
|
|
*/ |
18
|
|
|
class KitchensinkController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The data provider. |
22
|
|
|
* |
23
|
|
|
* @var DataProviderInterface |
24
|
|
|
*/ |
25
|
|
|
private $dataProvider; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The template engine |
29
|
|
|
* |
30
|
|
|
* @var Environment |
31
|
|
|
*/ |
32
|
|
|
private $templateEngine; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The template name. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $templateName; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* KitchensinkController constructor. |
43
|
|
|
* |
44
|
|
|
* @param DataProviderInterface $provider |
45
|
|
|
* @param Environment $engine |
46
|
|
|
* @param string $template |
47
|
|
|
*/ |
48
|
1 |
|
public function __construct(DataProviderInterface $provider, Environment $engine, string $template) |
49
|
|
|
{ |
50
|
1 |
|
$this->dataProvider = $provider; |
51
|
1 |
|
$this->templateEngine = $engine; |
52
|
1 |
|
$this->templateName = $template; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the view data from the data provider. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
1 |
|
private function getViewData(): array |
61
|
|
|
{ |
62
|
1 |
|
$viewData = []; |
63
|
|
|
|
64
|
1 |
|
foreach ($this->dataProvider->getTemplateVars() as $key => $value) { |
65
|
1 |
|
$withGetter = !is_numeric($key); |
66
|
|
|
|
67
|
1 |
|
$viewData[!$withGetter ? $value : $key] = !$withGetter |
68
|
1 |
|
? $this->dataProvider->{'get' . ucfirst($value)}() |
69
|
1 |
|
: $this->dataProvider->$value(); |
70
|
|
|
} |
71
|
1 |
|
return $viewData; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Renders the template for the kitchensink. |
76
|
|
|
* |
77
|
|
|
* @throws LoaderError Error while loading |
78
|
|
|
* @throws RuntimeError Runtime errors |
79
|
|
|
* @throws SyntaxError Syntax errors |
80
|
|
|
* |
81
|
|
|
* @return Response |
82
|
|
|
*/ |
83
|
1 |
|
public function indexAction(): Response |
84
|
|
|
{ |
85
|
1 |
|
return new Response( |
86
|
1 |
|
$this->templateEngine->render($this->templateName, $this->getViewData()) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|