Skip to content

Commit

Permalink
Add own implementation of Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Aug 8, 2024
1 parent f36e36d commit 194363c
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions Content/UserInterface/Controller/Website/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,90 @@

namespace Sulu\Bundle\ContentBundle\Content\UserInterface\Controller\Website;

use Sulu\Bundle\WebsiteBundle\Controller\DefaultController;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\PreviewBundle\Preview\Preview;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

/**
* TODO this controller will later replace the DefaultController of the WebsiteBundle
* and should be base for all content controllers.
*
* @template T of DimensionContentInterface
*/
class ContentController extends DefaultController
class ContentController extends AbstractController
{
/**
* @param T $object
*/
public function indexAction(
Request $request,
DimensionContentInterface $object,
string $view, // TODO maybe inject metadata where we also get the cachelifetime from
bool $preview,
bool $partial,
): Response {
$requestFormat = $request->getRequestFormat() ?? 'html';

$parameters = $this->resolveSuluParameters($object, $requestFormat === 'json');

if ($requestFormat === 'json') {
$response = new JsonResponse($parameters);
} else {
$response = new Response($this->renderSuluView($view, $requestFormat, $parameters, $preview, $partial));
}

$response->setPublic();

// TODO resolve cachelifetime
// TODO implement cacheLifetimeRequestStore

return $response;
}

/**
* @param T $object
*
* @return array<string, mixed>
*/
protected function resolveSuluParameters(DimensionContentInterface $object, bool $normalize): array
{
return [
'resource' => $object->getResource(), // TODO normalize JSON response

// TODO resolve content
];
}

/**
* @param array<string, mixed> $parameters
*
* @throws NotAcceptableHttpException
*/
protected function renderSuluView(
string $view,
string $requestFormat, // TODO maybe we should avoid this and resolve it before
array $parameters,
bool $preview,
bool $partial,
): string {
$viewTemplate = $view . '.' . $requestFormat . '.twig';

if (!$this->container->get('twig')->getLoader()->exists($viewTemplate)) {
throw new NotAcceptableHttpException(\sprintf('Page does not exist in "%s" format.', $requestFormat));
}

if ($partial) {
return $this->renderBlockView($viewTemplate, 'content', $parameters);
} elseif ($preview) {
$parameters['previewParentTemplate'] = $viewTemplate;
$parameters['previewContentReplacer'] = Preview::CONTENT_REPLACER;
$viewTemplate = '@SuluWebsite/Preview/preview.html.twig';
}

return $this->renderView($viewTemplate, $parameters);
}
}

0 comments on commit 194363c

Please sign in to comment.