Passed
Push — master ( 6f214c...ffcc9b )
by WEBEWEB
13:06
created

TwigController::getStylesheetManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
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\JavascriptManagerTrait;
23
use WBW\Library\Symfony\Manager\StylesheetManager;
24
use WBW\Library\Symfony\Manager\StylesheetManagerTrait;
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
    use JavascriptManagerTrait {
37
        setJavascriptManager as public;
38
    }
39
    use StylesheetManagerTrait {
40
        setStylesheetManager as public;
41
    }
42
43
    /**
44
     * Service name.
45
     *
46
     * @var string
47
     */
48
    const SERVICE_NAME = "wbw.core.controller.twig";
49
50
    /**
51
     * Function.
52
     *
53
     * @param Request $request The request.
54
     * @param string $name The name.
55
     * @return Response Returns the response.
56
     * @throws Throwable Throws an exception if an error occurs.
57
     */
58
    public function functionAction(Request $request, string $name): Response {
59
60
        /** @var Environment $twig */
61
        $twig = $this->getTwig();
62
63
        $function = $twig->getFunction($name);
64
        if (false === ($function instanceof TwigFunction)) {
65
            return new JsonResponse([], 404);
66
        }
67
68
        $callable = $function->getCallable();
69
70
        $content = call_user_func_array([
71
            $callable[0],
72
            $callable[1],
73
        ], $request->get("args", []));
74
75
        return new JsonResponse(true === is_array($content) ? $content : [$content]);
76
    }
77
78
    /**
79
     * Get the last modified.
80
     *
81
     * @param string $view The view.
82
     * @return DateTime|null Returns the last modified.
83
     * @throws Throwable Throws an exception if an error occurs.
84
     */
85
    protected function getLastModified(string $view): ?DateTime {
86
87
        $twig = $this->getTwig();
88
        if (null === $twig) {
89
            return null;
90
        }
91
92
        $path = $twig->getLoader()->getSourceContext($view)->getPath();
93
        $time = filemtime($path);
94
        if (false === $time) {
95
            return null;
96
        }
97
98
        return new DateTime("@$time");
99
    }
100
101
    /**
102
     * Resource.
103
     *
104
     * @param Request $request The request.
105
     * @param string $type The type.
106
     * @param string $name The name.
107
     * @return Response Returns the response.
108
     * @throws Throwable Throws an exception if an error occurs.
109
     */
110
    public function resourceAction(Request $request, string $type, string $name): Response {
111
112
        $resources   = [];
113
        $contentType = null;
114
115
        switch ($type) {
116
117
            case JavascriptProviderInterface::JAVASCRIPT_PROVIDER_EXTENSION:
118
119
                /** @var JavascriptManager $manager */
120
                $manager   = $this->getJavascriptManager();
121
                $resources = $manager->getJavascripts();
122
123
                $contentType = JavascriptProviderInterface::JAVASCRIPT_PROVIDER_CONTENT_TYPE;
124
                break;
125
126
            case StylesheetProviderInterface::STYLESHEET_PROVIDER_EXTENSION:
127
128
                /** @var StylesheetManager $manager */
129
                $manager   = $this->getStylesheetManager();
130
                $resources = $manager->getStylesheets();
131
132
                $contentType = StylesheetProviderInterface::STYLESHEET_PROVIDER_CONTENT_TYPE;
133
                break;
134
        }
135
136
        if (false === array_key_exists($name, $resources)) {
137
            throw $this->createNotFoundException();
138
        }
139
140
        $modified = $this->getLastModified($resources[$name]);
141
        if (null === $modified) {
142
            $modified = new DateTime();
143
        }
144
145
        $content = $this->renderView($resources[$name], $request->query->all());
146
147
        return new Response($content, 200, [
148
            "Content-Type"  => $contentType,
149
            "Last-Modified" => $modified->format("D, d M Y H:i:s T"),
150
        ]);
151
    }
152
}
153