15
15
16
16
use Sonata \BlockBundle \Templating \Helper \BlockHelper ;
17
17
use Sonata \PageBundle \CmsManager \CmsManagerSelectorInterface ;
18
- use Sonata \PageBundle \Exception \PageNotFoundException ;
19
18
use Sonata \PageBundle \Model \PageBlockInterface ;
20
19
use Sonata \PageBundle \Model \PageInterface ;
21
20
use Sonata \PageBundle \Site \SiteSelectorInterface ;
22
- use Symfony \ Bridge \Twig \Extension \ HttpKernelExtension ;
21
+ use Sonata \ PageBundle \Twig \PageRuntime ;
23
22
use Symfony \Component \HttpFoundation \RequestStack ;
24
23
use Symfony \Component \HttpKernel \Controller \ControllerReference ;
25
24
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
36
35
*/
37
36
final class PageExtension extends AbstractExtension
38
37
{
38
+ private PageRuntime $ pageRuntime ;
39
+
40
+ /**
41
+ * NEXT_MAJOR: Remove this constructor.
42
+ *
43
+ * @internal This class should only be used through Twig
44
+ */
39
45
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
46
52
) {
53
+ $ this ->pageRuntime = new PageRuntime (
54
+ $ cmsManagerSelector ,
55
+ $ siteSelector ,
56
+ $ router ,
57
+ $ blockHelper ,
58
+ $ requestStack ,
59
+ null ,
60
+ $ hideDisabledBlocks ,
61
+ );
47
62
}
48
63
49
64
public function getFunctions (): array
50
65
{
51
66
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 ' ]),
57
74
];
58
75
}
59
76
@@ -66,113 +83,63 @@ public function getFunctions(): array
66
83
*/
67
84
public function breadcrumb (Environment $ twig , ?PageInterface $ page = null , array $ options = []): string
68
85
{
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 );
110
95
}
111
96
112
97
/**
113
98
* @param array<string, mixed> $parameters
114
99
*/
115
100
public function ajaxUrl (PageBlockInterface $ block , array $ parameters = [], int $ absolute = UrlGeneratorInterface::ABSOLUTE_PATH ): string
116
101
{
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 );
125
111
}
126
112
127
113
/**
128
114
* @param array<string, mixed> $options
129
115
*/
130
116
public function renderContainer (string $ name , string |PageInterface |null $ page = null , array $ options = []): string
131
117
{
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 );
160
127
}
161
128
162
129
/**
163
130
* @param array<string, mixed> $options
164
131
*/
165
132
public function renderBlock (PageBlockInterface $ block , array $ options = []): string
166
133
{
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 );
176
143
}
177
144
178
145
/**
@@ -184,19 +151,14 @@ public function renderBlock(PageBlockInterface $block, array $options = []): str
184
151
*/
185
152
public function controller (string $ controller , array $ attributes = [], array $ query = []): ControllerReference
186
153
{
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 );
201
163
}
202
164
}
0 commit comments