• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sulu / SuluContentBundle / 11978668947

22 Nov 2024 07:11PM UTC coverage: 97.052% (-2.9%) from 100.0%
11978668947

Pull #271

github

web-flow
Merge 85d51e611 into a23a5b050
Pull Request #271: Draft: Base implementation of website content resolving

268 of 353 new or added lines in 17 files covered. (75.92%)

2798 of 2883 relevant lines covered (97.05%)

10.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Content/UserInterface/Controller/Website/ContentController.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Sulu.
7
 *
8
 * (c) Sulu GmbH
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13

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

16
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
17
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
18
use Sulu\Bundle\PreviewBundle\Preview\Preview;
19
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
24

25
/**
26
 * TODO this controller will later replace the DefaultController of the WebsiteBundle
27
 *      and should be base for all content controllers.
28
 *
29
 * @template T of DimensionContentInterface
30
 */
31
class ContentController extends AbstractController
32
{
33
    /**
34
     * @param T $object
35
     */
36
    public function indexAction(
37
        Request $request,
38
        DimensionContentInterface $object,
39
        string $view, // TODO maybe inject metadata where we also get the cachelifetime from
40
        bool $preview = false,
41
        bool $partial = false,
42
    ): Response {
NEW
43
        $requestFormat = $request->getRequestFormat() ?? 'html';
×
44

NEW
45
        $parameters = $this->resolveSuluParameters($object, 'json' === $requestFormat);
×
46

NEW
47
        if ('json' === $requestFormat) {
×
NEW
48
            $response = new JsonResponse($parameters);
×
49
        } else {
NEW
50
            $response = new Response($this->renderSuluView($view, $requestFormat, $parameters, $preview, $partial));
×
51
        }
52

NEW
53
        $response->setPublic();
×
54

55
        // TODO resolve cachelifetime
56
        // TODO implement cacheLifetimeRequestStore
57

NEW
58
        return $response;
×
59
    }
60

61
    /**
62
     * @param T $object
63
     *
64
     * @return array<string, mixed>
65
     */
66
    protected function resolveSuluParameters(DimensionContentInterface $object, bool $normalize): array
67
    {
NEW
68
        if (false === $normalize) {
×
NEW
69
            return $this->container->get('sulu_content.content_resolver')->resolve($object);
×
70
        }
71

NEW
72
        return []; // TODO normalize JSON response
×
73
    }
74

75
    /**
76
     * @param array<string, mixed> $parameters
77
     *
78
     * @throws NotAcceptableHttpException
79
     */
80
    protected function renderSuluView(
81
        string $view,
82
        string $requestFormat, // TODO maybe we should avoid this and resolve it before
83
        array $parameters,
84
        bool $preview,
85
        bool $partial,
86
    ): string {
NEW
87
        $viewTemplate = $view . '.' . $requestFormat . '.twig';
×
88

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

NEW
93
        if ($partial) {
×
NEW
94
            return $this->renderBlockView($viewTemplate, 'content', $parameters);
×
NEW
95
        } elseif ($preview) {
×
NEW
96
            $parameters['previewParentTemplate'] = $viewTemplate;
×
NEW
97
            $parameters['previewContentReplacer'] = Preview::CONTENT_REPLACER;
×
NEW
98
            $viewTemplate = '@SuluWebsite/Preview/preview.html.twig';
×
99
        }
100

NEW
101
        return $this->renderView($viewTemplate, $parameters);
×
102
    }
103

104
    public static function getSubscribedServices(): array
105
    {
NEW
106
        $services = parent::getSubscribedServices();
×
107

NEW
108
        $services['sulu_content.content_resolver'] = ContentResolverInterface::class;
×
109

NEW
110
        return $services;
×
111
    }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc