Skip to content

Commit a0ecacf

Browse files
Introduce runtime and page path methods
1 parent f00641b commit a0ecacf

File tree

5 files changed

+413
-113
lines changed

5 files changed

+413
-113
lines changed

src/Resources/config/twig.php

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Sonata\PageBundle\Twig\Extension\PageExtension;
1717
use Sonata\PageBundle\Twig\GlobalVariables;
18+
use Sonata\PageBundle\Twig\PageRuntime;
1819

1920
return static function (ContainerConfigurator $containerConfigurator): void {
2021
$containerConfigurator->services()
@@ -36,5 +37,17 @@
3637
service('sonata.page.cms_manager_selector'),
3738
service('sonata.page.site.selector'),
3839
service('sonata.page.template_manager'),
40+
])
41+
42+
->set('sonata.page.twig.page_runtime', PageRuntime::class)
43+
->tag('twig.runtime')
44+
->args([
45+
service('sonata.page.cms_manager_selector'),
46+
service('sonata.page.site.selector'),
47+
service('router'),
48+
service('sonata.block.templating.helper'),
49+
service('request_stack'),
50+
service('sonata.page.router'),
51+
param('sonata.page.hide_disabled_blocks'),
3952
]);
4053
};

src/Twig/Extension/PageExtension.php

+75-113
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515

1616
use Sonata\BlockBundle\Templating\Helper\BlockHelper;
1717
use Sonata\PageBundle\CmsManager\CmsManagerSelectorInterface;
18-
use Sonata\PageBundle\Exception\PageNotFoundException;
1918
use Sonata\PageBundle\Model\PageBlockInterface;
2019
use Sonata\PageBundle\Model\PageInterface;
2120
use Sonata\PageBundle\Site\SiteSelectorInterface;
22-
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
21+
use Sonata\PageBundle\Twig\PageRuntime;
2322
use Symfony\Component\HttpFoundation\RequestStack;
2423
use Symfony\Component\HttpKernel\Controller\ControllerReference;
2524
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -36,24 +35,42 @@
3635
*/
3736
final class PageExtension extends AbstractExtension
3837
{
38+
private PageRuntime $pageRuntime;
39+
40+
/**
41+
* NEXT_MAJOR: Remove this constructor.
42+
*
43+
* @internal This class should only be used through Twig
44+
*/
3945
public function __construct(
40-
private CmsManagerSelectorInterface $cmsManagerSelector,
41-
private SiteSelectorInterface $siteSelector,
42-
private RouterInterface $router,
43-
private BlockHelper $blockHelper,
44-
private RequestStack $requestStack,
45-
private bool $hideDisabledBlocks = false
46+
CmsManagerSelectorInterface $cmsManagerSelector,
47+
SiteSelectorInterface $siteSelector,
48+
RouterInterface $router,
49+
BlockHelper $blockHelper,
50+
RequestStack $requestStack,
51+
bool $hideDisabledBlocks = false
4652
) {
53+
$this->pageRuntime = new PageRuntime(
54+
$cmsManagerSelector,
55+
$siteSelector,
56+
$router,
57+
$blockHelper,
58+
$requestStack,
59+
null,
60+
$hideDisabledBlocks,
61+
);
4762
}
4863

4964
public function getFunctions(): array
5065
{
5166
return [
52-
new TwigFunction('sonata_page_ajax_url', [$this, 'ajaxUrl']),
53-
new TwigFunction('sonata_page_breadcrumb', [$this, 'breadcrumb'], ['is_safe' => ['html'], 'needs_environment' => true]),
54-
new TwigFunction('sonata_page_render_container', [$this, 'renderContainer'], ['is_safe' => ['html']]),
55-
new TwigFunction('sonata_page_render_block', [$this, 'renderBlock'], ['is_safe' => ['html']]),
56-
new TwigFunction('controller', [$this, 'controller']),
67+
new TwigFunction('sonata_page_ajax_url', [PageRuntime::class, 'ajaxUrl']),
68+
new TwigFunction('sonata_page_breadcrumb', [PageRuntime::class, 'breadcrumb'], ['is_safe' => ['html'], 'needs_environment' => true]),
69+
new TwigFunction('sonata_page_render_container', [PageRuntime::class, 'renderContainer'], ['is_safe' => ['html']]),
70+
new TwigFunction('sonata_page_render_block', [PageRuntime::class, 'renderBlock'], ['is_safe' => ['html']]),
71+
new TwigFunction('sonata_page_url', [PageRuntime::class, 'getPageUrl']),
72+
new TwigFunction('sonata_page_path', [PageRuntime::class, 'getPagePath']),
73+
new TwigFunction('controller', [PageRuntime::class, 'controller']),
5774
];
5875
}
5976

@@ -66,113 +83,63 @@ public function getFunctions(): array
6683
*/
6784
public function breadcrumb(Environment $twig, ?PageInterface $page = null, array $options = []): string
6885
{
69-
if (null === $page) {
70-
$page = $this->cmsManagerSelector->retrieve()->getCurrentPage();
71-
}
72-
73-
$options = array_merge([
74-
'separator' => '',
75-
'current_class' => '',
76-
'last_separator' => '',
77-
'force_view_home_page' => true,
78-
'container_attr' => ['class' => 'sonata-page-breadcrumbs'],
79-
'elements_attr' => [],
80-
'template' => '@SonataPage/Page/breadcrumb.html.twig',
81-
], $options);
82-
83-
$breadcrumbs = [];
84-
85-
if (null !== $page) {
86-
$breadcrumbs = $page->getParents();
87-
88-
if (true === $options['force_view_home_page'] && (!isset($breadcrumbs[0]) || 'homepage' !== $breadcrumbs[0]->getRouteName())) {
89-
$site = $this->siteSelector->retrieve();
90-
91-
$homePage = null;
92-
try {
93-
if (null !== $site) {
94-
$homePage = $this->cmsManagerSelector->retrieve()->getPageByRouteName($site, 'homepage');
95-
}
96-
} catch (PageNotFoundException) {
97-
}
98-
99-
if (null !== $homePage) {
100-
array_unshift($breadcrumbs, $homePage);
101-
}
102-
}
103-
}
104-
105-
return $twig->render($options['template'], [
106-
'page' => $page,
107-
'breadcrumbs' => $breadcrumbs,
108-
'options' => $options,
109-
]);
86+
@trigger_error(sprintf(
87+
'The method "%s()" is deprecated since sonata-project/page-bundle 4.7 and will be removed in 5.0.'
88+
.' Use "%s::%s()" instead.',
89+
__METHOD__,
90+
PageRuntime::class,
91+
__FUNCTION__
92+
), \E_USER_DEPRECATED);
93+
94+
return $this->pageRuntime->breadcrumb($twig, $page, $options);
11095
}
11196

11297
/**
11398
* @param array<string, mixed> $parameters
11499
*/
115100
public function ajaxUrl(PageBlockInterface $block, array $parameters = [], int $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
116101
{
117-
$parameters['blockId'] = $block->getId();
118-
$page = $block->getPage();
119-
120-
if (null !== $page) {
121-
$parameters['pageId'] = $page->getId();
122-
}
123-
124-
return $this->router->generate('sonata_page_ajax_block', $parameters, $absolute);
102+
@trigger_error(sprintf(
103+
'The method "%s()" is deprecated since sonata-project/page-bundle 4.7 and will be removed in 5.0.'
104+
.' Use "%s::%s()" instead.',
105+
__METHOD__,
106+
PageRuntime::class,
107+
__FUNCTION__
108+
), \E_USER_DEPRECATED);
109+
110+
return $this->pageRuntime->ajaxUrl($block, $parameters, $absolute);
125111
}
126112

127113
/**
128114
* @param array<string, mixed> $options
129115
*/
130116
public function renderContainer(string $name, string|PageInterface|null $page = null, array $options = []): string
131117
{
132-
$cms = $this->cmsManagerSelector->retrieve();
133-
$site = $this->siteSelector->retrieve();
134-
$targetPage = null;
135-
136-
try {
137-
if (null === $page) {
138-
$targetPage = $cms->getCurrentPage();
139-
} elseif (null !== $site && !$page instanceof PageInterface) {
140-
$targetPage = $cms->getInternalRoute($site, $page);
141-
} elseif ($page instanceof PageInterface) {
142-
$targetPage = $page;
143-
}
144-
} catch (PageNotFoundException) {
145-
// the snapshot does not exist
146-
$targetPage = null;
147-
}
148-
149-
if (null === $targetPage) {
150-
return '';
151-
}
152-
153-
$container = $cms->findContainer($name, $targetPage);
154-
155-
if (null === $container) {
156-
return '';
157-
}
158-
159-
return $this->renderBlock($container, $options);
118+
@trigger_error(sprintf(
119+
'The method "%s()" is deprecated since sonata-project/page-bundle 4.7 and will be removed in 5.0.'
120+
.' Use "%s::%s()" instead.',
121+
__METHOD__,
122+
PageRuntime::class,
123+
__FUNCTION__
124+
), \E_USER_DEPRECATED);
125+
126+
return $this->pageRuntime->renderContainer($name, $page, $options);
160127
}
161128

162129
/**
163130
* @param array<string, mixed> $options
164131
*/
165132
public function renderBlock(PageBlockInterface $block, array $options = []): string
166133
{
167-
if (
168-
false === $block->getEnabled()
169-
&& !$this->cmsManagerSelector->isEditor()
170-
&& $this->hideDisabledBlocks
171-
) {
172-
return '';
173-
}
174-
175-
return $this->blockHelper->render($block, $options);
134+
@trigger_error(sprintf(
135+
'The method "%s()" is deprecated since sonata-project/page-bundle 4.7 and will be removed in 5.0.'
136+
.' Use "%s::%s()" instead.',
137+
__METHOD__,
138+
PageRuntime::class,
139+
__FUNCTION__
140+
), \E_USER_DEPRECATED);
141+
142+
return $this->pageRuntime->renderBlock($block, $options);
176143
}
177144

178145
/**
@@ -184,19 +151,14 @@ public function renderBlock(PageBlockInterface $block, array $options = []): str
184151
*/
185152
public function controller(string $controller, array $attributes = [], array $query = []): ControllerReference
186153
{
187-
if (!isset($attributes['pathInfo'])) {
188-
$site = $this->siteSelector->retrieve();
189-
190-
if (null !== $site) {
191-
$sitePath = $site->getRelativePath();
192-
$request = $this->requestStack->getCurrentRequest();
193-
194-
if (null !== $sitePath && null !== $request) {
195-
$attributes['pathInfo'] = $sitePath.$request->getPathInfo();
196-
}
197-
}
198-
}
199-
200-
return HttpKernelExtension::controller($controller, $attributes, $query);
154+
@trigger_error(sprintf(
155+
'The method "%s()" is deprecated since sonata-project/page-bundle 4.7 and will be removed in 5.0.'
156+
.' Use "%s::%s()" instead.',
157+
__METHOD__,
158+
PageRuntime::class,
159+
__FUNCTION__
160+
), \E_USER_DEPRECATED);
161+
162+
return $this->pageRuntime->controller($controller, $attributes, $query);
201163
}
202164
}

0 commit comments

Comments
 (0)