|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2021 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
use Throwable; |
|
19
|
|
|
use Twig\Environment; |
|
20
|
|
|
use Twig\TwigFunction; |
|
21
|
|
|
use WBW\Library\Symfony\Manager\JavascriptManager; |
|
22
|
|
|
use WBW\Library\Symfony\Manager\JavascriptManagerInterface; |
|
23
|
|
|
use WBW\Library\Symfony\Manager\StylesheetManager; |
|
24
|
|
|
use WBW\Library\Symfony\Manager\StylesheetManagerInterface; |
|
25
|
|
|
use WBW\Library\Symfony\Provider\JavascriptProviderInterface; |
|
26
|
|
|
use WBW\Library\Symfony\Provider\StylesheetProviderInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Twig controller. |
|
30
|
|
|
* |
|
31
|
|
|
* @author webeweb <https://github.com/webeweb> |
|
32
|
|
|
* @package WBW\Bundle\CoreBundle\Controller |
|
33
|
|
|
*/ |
|
34
|
|
|
class TwigController extends AbstractController { |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Service name. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
const SERVICE_NAME = "wbw.core.controller.twig"; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Function. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Request $request The request. |
|
47
|
|
|
* @param string $name The name. |
|
48
|
|
|
* @return Response Returns the response. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function functionAction(Request $request, string $name): Response { |
|
51
|
|
|
|
|
52
|
|
|
/** @var Environment $twig */ |
|
53
|
|
|
$twig = $this->getTwig(); |
|
54
|
|
|
|
|
55
|
|
|
$function = $twig->getFunction($name); |
|
56
|
|
|
if (false === ($function instanceof TwigFunction)) { |
|
57
|
|
|
return new JsonResponse([], 404); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$callable = $function->getCallable(); |
|
61
|
|
|
|
|
62
|
|
|
$content = call_user_func_array([ |
|
63
|
|
|
$callable[0], |
|
64
|
|
|
$callable[1], |
|
65
|
|
|
], $request->get("args", [])); |
|
66
|
|
|
|
|
67
|
|
|
return new JsonResponse(true === is_array($content) ? $content : [$content]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the javascript manager. |
|
72
|
|
|
* |
|
73
|
|
|
* @return JavascriptManagerInterface Returns the javascript manager. |
|
74
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getJavascriptManager(): JavascriptManagerInterface { |
|
77
|
|
|
return $this->container->get(JavascriptManager::SERVICE_NAME); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the last modified. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $view The view. |
|
84
|
|
|
* @return DateTime|null Returns the last modified. |
|
85
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getLastModified(string $view): ?DateTime { |
|
88
|
|
|
|
|
89
|
|
|
$twig = $this->getTwig(); |
|
90
|
|
|
if (null === $twig) { |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$path = $twig->getLoader()->getSourceContext($view)->getPath(); |
|
95
|
|
|
$time = filemtime($path); |
|
96
|
|
|
if (false === $time) { |
|
97
|
|
|
return null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return new DateTime("@$time"); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the stylesheet manager. |
|
105
|
|
|
* |
|
106
|
|
|
* @return StylesheetManagerInterface Returns the stylesheet manager. |
|
107
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getStylesheetManager(): StylesheetManagerInterface { |
|
110
|
|
|
return $this->container->get(StylesheetManager::SERVICE_NAME); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Resource. |
|
115
|
|
|
* |
|
116
|
|
|
* @param Request $request The request. |
|
117
|
|
|
* @param string $type The type. |
|
118
|
|
|
* @param string $name The name. |
|
119
|
|
|
* @return Response Returns the response. |
|
120
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function resourceAction(Request $request, string $type, string $name): Response { |
|
123
|
|
|
|
|
124
|
|
|
$resources = []; |
|
125
|
|
|
$contentType = null; |
|
126
|
|
|
|
|
127
|
|
|
switch ($type) { |
|
128
|
|
|
|
|
129
|
|
|
case JavascriptProviderInterface::JAVASCRIPT_PROVIDER_EXTENSION: |
|
130
|
|
|
|
|
131
|
|
|
/** @var JavascriptManager $manager */ |
|
132
|
|
|
$manager = $this->getJavascriptManager(); |
|
133
|
|
|
$resources = $manager->getJavascripts(); |
|
134
|
|
|
|
|
135
|
|
|
$contentType = JavascriptProviderInterface::JAVASCRIPT_PROVIDER_CONTENT_TYPE; |
|
136
|
|
|
break; |
|
137
|
|
|
|
|
138
|
|
|
case StylesheetProviderInterface::STYLESHEET_PROVIDER_EXTENSION: |
|
139
|
|
|
|
|
140
|
|
|
/** @var StylesheetManager $manager */ |
|
141
|
|
|
$manager = $this->getStylesheetManager(); |
|
142
|
|
|
$resources = $manager->getStylesheets(); |
|
143
|
|
|
|
|
144
|
|
|
$contentType = StylesheetProviderInterface::STYLESHEET_PROVIDER_CONTENT_TYPE; |
|
145
|
|
|
break; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (false === array_key_exists($name, $resources)) { |
|
149
|
|
|
throw $this->createNotFoundException(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$modified = $this->getLastModified($resources[$name]); |
|
153
|
|
|
if (null === $modified) { |
|
154
|
|
|
$modified = new DateTime(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$content = $this->renderView($resources[$name], $request->query->all()); |
|
158
|
|
|
|
|
159
|
|
|
return new Response($content, 200, [ |
|
160
|
|
|
"Content-Type" => $contentType, |
|
161
|
|
|
"Last-Modified" => $modified->format("D, d M Y H:i:s T"), |
|
162
|
|
|
]); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|