Skip to content

Commit

Permalink
Add AbstractController and common twig templates rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienClairembault authored Aug 21, 2024
1 parent f402aae commit eb703aa
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 55 deletions.
78 changes: 78 additions & 0 deletions src/Glpi/Controller/AbstractController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Controller;

use Glpi\Application\View\TemplateRenderer;
use Glpi\DependencyInjection\PublicService;
use Symfony\Component\HttpFoundation\Response;

abstract class AbstractController implements PublicService
{
/**
* Helper method to get a response containing the content of a rendered
* twig template.
*
* @param string $view Path to a twig template, which will be looked for in
* the "templates" folder.
* For example, "my_template.html.twig" will be resolved to `templates/my_template.html.twig`.
* For plugins, you must use the "@my_plugin_name" prefix.
* For example, "@formcreator/my_template.html.twig will resolve to
* `(plugins|marketplace)/formcreator/templates/my_template.html.twig`.
* @param array $parameters The expected parameters of the twig template.
* @param Response $response Optional parameter which serves as the "base"
* response into which the renderer twig content will be inserted.
* You should only use it if you need to set some specific headers into the
* response or to set an http return code different than 200.
*
* @return Response
*/
final protected function render(
string $view,
array $parameters = [],
Response $response = new Response(),
): Response {
$twig = TemplateRenderer::getInstance();

// We must use output buffering here as Html::header() and Html::footer()
// output content directly.
// TODO: fix header() and footer() methods and remove output buffering
ob_start();
$twig->display($view, $parameters);
$content = ob_get_clean();

$response->setContent($content);
return $response;
}
}
8 changes: 4 additions & 4 deletions src/Glpi/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

namespace Glpi\Controller;

use Glpi\Api\HL\Controller\AbstractController;
use Glpi\Api\HL\Controller\AbstractController as ApiAbstractController;
use Glpi\Api\HL\Router;
use Glpi\Application\ErrorHandler;
use Glpi\Http\JSONResponse;
Expand All @@ -46,7 +46,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;

final readonly class ApiController implements Controller
final class ApiController extends AbstractController
{
#[Route(
"/api.php{request_parameters}",
Expand Down Expand Up @@ -105,8 +105,8 @@ private function call(): void
$response->send();
} catch (\InvalidArgumentException $e) {
$response = new JSONResponse(
AbstractController::getErrorResponseBody(
AbstractController::ERROR_INVALID_PARAMETER,
ApiAbstractController::getErrorResponseBody(
ApiAbstractController::ERROR_INVALID_PARAMETER,
$e->getMessage()
),
400
Expand Down
2 changes: 1 addition & 1 deletion src/Glpi/Controller/ApiRestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;

final readonly class ApiRestController implements Controller
final class ApiRestController extends AbstractController
{
#[Route(
"/apirest.php{request_parameters}",
Expand Down
2 changes: 1 addition & 1 deletion src/Glpi/Controller/CaldavController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;

final readonly class CaldavController implements Controller
final class CaldavController extends AbstractController
{
#[Route(
"/caldav.php{request_parameters}",
Expand Down
44 changes: 0 additions & 44 deletions src/Glpi/Controller/Controller.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Glpi/Controller/ErrorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ErrorController implements Controller
class ErrorController extends AbstractController
{
public function __invoke(Request $request, ?\Throwable $exception = null): Response
{
Expand Down
4 changes: 2 additions & 2 deletions src/Glpi/Controller/Form/TagListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

namespace Glpi\Controller\Form;

use Glpi\Controller\Controller;
use Glpi\Controller\AbstractController;
use Glpi\Form\Form;
use Glpi\Form\Tag\FormTagsManager;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -45,7 +45,7 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;

final class TagListController implements Controller
final class TagListController extends AbstractController
{
#[Route(
"/Form/TagList",
Expand Down
2 changes: 1 addition & 1 deletion src/Glpi/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;

final readonly class IndexController implements Controller
final class IndexController extends AbstractController
{
#[Route(
[
Expand Down
2 changes: 1 addition & 1 deletion src/Glpi/Controller/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;

final readonly class StatusController implements Controller
final class StatusController extends AbstractController
{
#[Route(
"/status.php",
Expand Down
43 changes: 43 additions & 0 deletions templates/layout/page_skeleton.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{#
# ---------------------------------------------------------------------
#
# GLPI - Gestionnaire Libre de Parc Informatique
#
# http://glpi-project.org
#
# @copyright 2015-2024 Teclib' and contributors.
# @licence https://www.gnu.org/licenses/gpl-3.0.html
#
# ---------------------------------------------------------------------
#
# LICENSE
#
# This file is part of GLPI.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------
#}

{% do call(['Html', 'header'], {
title: title,
sector: menu[0] ?? 'none',
item : menu[1] ?? 'none',
option: menu[2] ?? '',
}) %}

{% block content %}
{% endblock content %}

{% do call(['Html', 'footer']) %}
50 changes: 50 additions & 0 deletions templates/layout/page_without_tabs.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{#
# ---------------------------------------------------------------------
#
# GLPI - Gestionnaire Libre de Parc Informatique
#
# http://glpi-project.org
#
# @copyright 2015-2024 Teclib' and contributors.
# @licence https://www.gnu.org/licenses/gpl-3.0.html
#
# ---------------------------------------------------------------------
#
# LICENSE
#
# This file is part of GLPI.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------
#}

{% extends "layout/page_skeleton.html.twig" %}

{% block content %}
<div class="page-header mt-0">
<div class="container container-{{ container_size ?? "xl" }}">
<h1 class="page title mb-0">
{% block content_title %}
{% endblock content_title %}
</h1>
</div>
</div>
<div class="page-body mt-3">
<div class="container container-{{ container_size ?? "xl" }}">
{% block content_body %}
{% endblock content_body %}
</div>
</div>
{% endblock content %}

0 comments on commit eb703aa

Please sign in to comment.