Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add own implementation of Controller #265

Open
wants to merge 1 commit into
base: 0.9
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading