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 c394e84
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions Content/UserInterface/Controller/Website/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,91 @@

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\HttpCacheBundle\Cache\SuluHttpCache;
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 c394e84

Please sign in to comment.