Skip to content

Commit eb703aa

Browse files
Add AbstractController and common twig templates rendering
1 parent f402aae commit eb703aa

11 files changed

+182
-55
lines changed
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2024 Teclib' and contributors.
11+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
12+
*
13+
* ---------------------------------------------------------------------
14+
*
15+
* LICENSE
16+
*
17+
* This file is part of GLPI.
18+
*
19+
* This program is free software: you can redistribute it and/or modify
20+
* it under the terms of the GNU General Public License as published by
21+
* the Free Software Foundation, either version 3 of the License, or
22+
* (at your option) any later version.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
31+
*
32+
* ---------------------------------------------------------------------
33+
*/
34+
35+
namespace Glpi\Controller;
36+
37+
use Glpi\Application\View\TemplateRenderer;
38+
use Glpi\DependencyInjection\PublicService;
39+
use Symfony\Component\HttpFoundation\Response;
40+
41+
abstract class AbstractController implements PublicService
42+
{
43+
/**
44+
* Helper method to get a response containing the content of a rendered
45+
* twig template.
46+
*
47+
* @param string $view Path to a twig template, which will be looked for in
48+
* the "templates" folder.
49+
* For example, "my_template.html.twig" will be resolved to `templates/my_template.html.twig`.
50+
* For plugins, you must use the "@my_plugin_name" prefix.
51+
* For example, "@formcreator/my_template.html.twig will resolve to
52+
* `(plugins|marketplace)/formcreator/templates/my_template.html.twig`.
53+
* @param array $parameters The expected parameters of the twig template.
54+
* @param Response $response Optional parameter which serves as the "base"
55+
* response into which the renderer twig content will be inserted.
56+
* You should only use it if you need to set some specific headers into the
57+
* response or to set an http return code different than 200.
58+
*
59+
* @return Response
60+
*/
61+
final protected function render(
62+
string $view,
63+
array $parameters = [],
64+
Response $response = new Response(),
65+
): Response {
66+
$twig = TemplateRenderer::getInstance();
67+
68+
// We must use output buffering here as Html::header() and Html::footer()
69+
// output content directly.
70+
// TODO: fix header() and footer() methods and remove output buffering
71+
ob_start();
72+
$twig->display($view, $parameters);
73+
$content = ob_get_clean();
74+
75+
$response->setContent($content);
76+
return $response;
77+
}
78+
}

src/Glpi/Controller/ApiController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
namespace Glpi\Controller;
3636

37-
use Glpi\Api\HL\Controller\AbstractController;
37+
use Glpi\Api\HL\Controller\AbstractController as ApiAbstractController;
3838
use Glpi\Api\HL\Router;
3939
use Glpi\Application\ErrorHandler;
4040
use Glpi\Http\JSONResponse;
@@ -46,7 +46,7 @@
4646
use Symfony\Component\HttpFoundation\StreamedResponse;
4747
use Symfony\Component\Routing\Attribute\Route;
4848

49-
final readonly class ApiController implements Controller
49+
final class ApiController extends AbstractController
5050
{
5151
#[Route(
5252
"/api.php{request_parameters}",
@@ -105,8 +105,8 @@ private function call(): void
105105
$response->send();
106106
} catch (\InvalidArgumentException $e) {
107107
$response = new JSONResponse(
108-
AbstractController::getErrorResponseBody(
109-
AbstractController::ERROR_INVALID_PARAMETER,
108+
ApiAbstractController::getErrorResponseBody(
109+
ApiAbstractController::ERROR_INVALID_PARAMETER,
110110
$e->getMessage()
111111
),
112112
400

src/Glpi/Controller/ApiRestController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
use Symfony\Component\HttpFoundation\StreamedResponse;
4343
use Symfony\Component\Routing\Attribute\Route;
4444

45-
final readonly class ApiRestController implements Controller
45+
final class ApiRestController extends AbstractController
4646
{
4747
#[Route(
4848
"/apirest.php{request_parameters}",

src/Glpi/Controller/CaldavController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
use Symfony\Component\HttpFoundation\StreamedResponse;
4141
use Symfony\Component\Routing\Attribute\Route;
4242

43-
final readonly class CaldavController implements Controller
43+
final class CaldavController extends AbstractController
4444
{
4545
#[Route(
4646
"/caldav.php{request_parameters}",

src/Glpi/Controller/Controller.php

-44
This file was deleted.

src/Glpi/Controller/ErrorController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
use Symfony\Component\HttpFoundation\StreamedResponse;
4444
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
4545

46-
class ErrorController implements Controller
46+
class ErrorController extends AbstractController
4747
{
4848
public function __invoke(Request $request, ?\Throwable $exception = null): Response
4949
{

src/Glpi/Controller/Form/TagListController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
namespace Glpi\Controller\Form;
3636

37-
use Glpi\Controller\Controller;
37+
use Glpi\Controller\AbstractController;
3838
use Glpi\Form\Form;
3939
use Glpi\Form\Tag\FormTagsManager;
4040
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -45,7 +45,7 @@
4545
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
4646
use Symfony\Component\Routing\Attribute\Route;
4747

48-
final class TagListController implements Controller
48+
final class TagListController extends AbstractController
4949
{
5050
#[Route(
5151
"/Form/TagList",

src/Glpi/Controller/IndexController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
use Symfony\Component\HttpFoundation\StreamedResponse;
4949
use Symfony\Component\Routing\Attribute\Route;
5050

51-
final readonly class IndexController implements Controller
51+
final class IndexController extends AbstractController
5252
{
5353
#[Route(
5454
[

src/Glpi/Controller/StatusController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
use Symfony\Component\HttpFoundation\StreamedResponse;
4545
use Symfony\Component\Routing\Attribute\Route;
4646

47-
final readonly class StatusController implements Controller
47+
final class StatusController extends AbstractController
4848
{
4949
#[Route(
5050
"/status.php",
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{#
2+
# ---------------------------------------------------------------------
3+
#
4+
# GLPI - Gestionnaire Libre de Parc Informatique
5+
#
6+
# http://glpi-project.org
7+
#
8+
# @copyright 2015-2024 Teclib' and contributors.
9+
# @licence https://www.gnu.org/licenses/gpl-3.0.html
10+
#
11+
# ---------------------------------------------------------------------
12+
#
13+
# LICENSE
14+
#
15+
# This file is part of GLPI.
16+
#
17+
# This program is free software: you can redistribute it and/or modify
18+
# it under the terms of the GNU General Public License as published by
19+
# the Free Software Foundation, either version 3 of the License, or
20+
# (at your option) any later version.
21+
#
22+
# This program is distributed in the hope that it will be useful,
23+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
# GNU General Public License for more details.
26+
#
27+
# You should have received a copy of the GNU General Public License
28+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
#
30+
# ---------------------------------------------------------------------
31+
#}
32+
33+
{% do call(['Html', 'header'], {
34+
title: title,
35+
sector: menu[0] ?? 'none',
36+
item : menu[1] ?? 'none',
37+
option: menu[2] ?? '',
38+
}) %}
39+
40+
{% block content %}
41+
{% endblock content %}
42+
43+
{% do call(['Html', 'footer']) %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{#
2+
# ---------------------------------------------------------------------
3+
#
4+
# GLPI - Gestionnaire Libre de Parc Informatique
5+
#
6+
# http://glpi-project.org
7+
#
8+
# @copyright 2015-2024 Teclib' and contributors.
9+
# @licence https://www.gnu.org/licenses/gpl-3.0.html
10+
#
11+
# ---------------------------------------------------------------------
12+
#
13+
# LICENSE
14+
#
15+
# This file is part of GLPI.
16+
#
17+
# This program is free software: you can redistribute it and/or modify
18+
# it under the terms of the GNU General Public License as published by
19+
# the Free Software Foundation, either version 3 of the License, or
20+
# (at your option) any later version.
21+
#
22+
# This program is distributed in the hope that it will be useful,
23+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
# GNU General Public License for more details.
26+
#
27+
# You should have received a copy of the GNU General Public License
28+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
#
30+
# ---------------------------------------------------------------------
31+
#}
32+
33+
{% extends "layout/page_skeleton.html.twig" %}
34+
35+
{% block content %}
36+
<div class="page-header mt-0">
37+
<div class="container container-{{ container_size ?? "xl" }}">
38+
<h1 class="page title mb-0">
39+
{% block content_title %}
40+
{% endblock content_title %}
41+
</h1>
42+
</div>
43+
</div>
44+
<div class="page-body mt-3">
45+
<div class="container container-{{ container_size ?? "xl" }}">
46+
{% block content_body %}
47+
{% endblock content_body %}
48+
</div>
49+
</div>
50+
{% endblock content %}

0 commit comments

Comments
 (0)