77use EasyCorp \Bundle \EasyAdminBundle \Contracts \Controller \DashboardControllerInterface ;
88use EasyCorp \Bundle \EasyAdminBundle \EasyAdminBundle ;
99use EasyCorp \Bundle \EasyAdminBundle \Factory \AdminContextFactory ;
10+ use EasyCorp \Bundle \EasyAdminBundle \Factory \ControllerFactory ;
1011use EasyCorp \Bundle \EasyAdminBundle \Registry \CrudControllerRegistry ;
1112use EasyCorp \Bundle \EasyAdminBundle \Registry \DashboardControllerRegistry ;
1213use Symfony \Component \HttpFoundation \Request ;
2526class AdminContextListener
2627{
2728 private $ adminContextFactory ;
28- private $ dashboardControllers ;
29- private $ crudControllers ;
30- private $ controllerResolver ;
29+ private $ controllerFactory ;
3130 private $ twig ;
3231
33- public function __construct (AdminContextFactory $ adminContextFactory , DashboardControllerRegistry $ dashboardControllers , CrudControllerRegistry $ crudControllers , ControllerResolverInterface $ controllerResolver , Environment $ twig )
32+ public function __construct (AdminContextFactory $ adminContextFactory , ControllerFactory $ controllerFactory , Environment $ twig )
3433 {
3534 $ this ->adminContextFactory = $ adminContextFactory ;
36- $ this ->dashboardControllers = $ dashboardControllers ;
37- $ this ->crudControllers = $ crudControllers ;
38- $ this ->controllerResolver = $ controllerResolver ;
35+ $ this ->controllerFactory = $ controllerFactory ;
3936 $ this ->twig = $ twig ;
4037 }
4138
@@ -49,7 +46,7 @@ public function onKernelController(ControllerEvent $event): void
4946
5047 $ dashboardControllerInstance = $ currentControllerInstance instanceof DashboardControllerInterface
5148 ? $ currentControllerInstance
52- : $ this ->getDashboardControllerInstanceFromContextId ($ contextId , $ event ->getRequest ());
49+ : $ this ->controllerFactory -> getDashboardControllerInstanceFromContextId ($ contextId , $ event ->getRequest ());
5350 if (null === $ dashboardControllerInstance ) {
5451 // this can only happen when a malicious user tries to hack the contextId value in the query string
5552 // don't throw an exception to prevent hackers from causing lots of exceptions in applications using EasyAdmin
@@ -59,7 +56,7 @@ public function onKernelController(ControllerEvent $event): void
5956
6057 $ crudId = $ event ->getRequest ()->query ->get ('crudId ' );
6158 $ crudAction = $ event ->getRequest ()->query ->get ('crudAction ' );
62- $ crudControllerInstance = $ this ->getCrudControllerInstance ($ crudId , $ crudAction , $ event ->getRequest ());
59+ $ crudControllerInstance = $ this ->controllerFactory -> getCrudControllerInstance ($ crudId , $ crudAction , $ event ->getRequest ());
6360
6461 if (null !== $ crudId && null === $ dashboardControllerInstance ) {
6562 // this can only happen when a malicious user tries to hack the crudId value in the query string
@@ -122,55 +119,6 @@ private function getCurrentControllerInstance(ControllerEvent $event)
122119 return $ controller [0 ];
123120 }
124121
125- private function getDashboardControllerInstanceFromContextId (string $ contextId , Request $ request ): ?DashboardControllerInterface
126- {
127- $ dashboardControllerFqcn = $ this ->dashboardControllers ->getControllerFqcnByContextId ($ contextId );
128- if (null === $ dashboardControllerFqcn ) {
129- return null ;
130- }
131-
132- $ newRequest = $ request ->duplicate (null , null , ['_controller ' => [$ dashboardControllerFqcn , 'index ' ]]);
133- $ dashboardControllerCallable = $ this ->controllerResolver ->getController ($ newRequest );
134-
135- if (false === $ dashboardControllerCallable ) {
136- throw new NotFoundHttpException (sprintf ('Unable to find the controller "%s::%s". ' , $ dashboardControllerFqcn , 'index ' ));
137- }
138-
139- if (!\is_array ($ dashboardControllerCallable )) {
140- return null ;
141- }
142-
143- $ dashboardControllerInstance = $ dashboardControllerCallable [0 ];
144-
145- return $ dashboardControllerInstance instanceof DashboardControllerInterface ? $ dashboardControllerInstance : null ;
146- }
147-
148- private function getCrudControllerInstance (?string $ crudId , ?string $ crudAction , Request $ request ): ?CrudControllerInterface
149- {
150- if (null === $ crudId || null === $ crudAction ) {
151- return null ;
152- }
153-
154- if (null === $ crudControllerFqcn = $ this ->crudControllers ->findCrudFqcnByCrudId ($ crudId )) {
155- return null ;
156- }
157-
158- $ newRequest = $ request ->duplicate (null , null , ['_controller ' => [$ crudControllerFqcn , $ crudAction ]]);
159- $ crudControllerCallable = $ this ->controllerResolver ->getController ($ newRequest );
160-
161- if (false === $ crudControllerCallable ) {
162- throw new NotFoundHttpException (sprintf ('Unable to find the controller "%s::%s". ' , $ crudControllerFqcn , $ crudAction ));
163- }
164-
165- if (!\is_array ($ crudControllerCallable )) {
166- return null ;
167- }
168-
169- $ crudControllerInstance = $ crudControllerCallable [0 ];
170-
171- return $ crudControllerInstance instanceof CrudControllerInterface ? $ crudControllerInstance : null ;
172- }
173-
174122 private function createAdminContext (Request $ request , DashboardControllerInterface $ dashboardController , ?CrudControllerInterface $ crudController ): AdminContext
175123 {
176124 return $ this ->adminContextFactory ->create ($ request , $ dashboardController , $ crudController );
0 commit comments