Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Commits
dc8fcbaf
Commit
dc8fcbaf
authored
12 years ago
by
Angie Byron
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#1875504
by fabpot: Add the possibility to use services as controllers.
parent
fd838b4f
No related branches found
No related tags found
2 merge requests
!7452
Issue #1797438. HTML5 validation is preventing form submit and not fully...
,
!789
Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/lib/Drupal/Core/ControllerResolver.php
+37
-10
37 additions, 10 deletions
core/lib/Drupal/Core/ControllerResolver.php
with
37 additions
and
10 deletions
core/lib/Drupal/Core/ControllerResolver.php
+
37
−
10
View file @
dc8fcbaf
...
...
@@ -15,9 +15,17 @@
/**
* ControllerResolver to enhance controllers beyond Symfony's basic handling.
*
* When creating a new object-based controller that implements
* ContainerAwareInterface, inject the container into it. While not always
* necessary, that allows a controller to vary the services it needs at runtime.
* It adds two behaviors:
*
* - When creating a new object-based controller that implements
* ContainerAwareInterface, inject the container into it. While not always
* necessary, that allows a controller to vary the services it needs at
* runtime.
*
* - By default, a controller name follows the class::method notation. This
* class adds the possibility to use a service from the container as a
* controller by using a service:method notation (Symfony uses the same
* convention).
*/
class
ControllerResolver
extends
BaseControllerResolver
{
...
...
@@ -50,17 +58,36 @@ public function __construct(ContainerInterface $container, LoggerInterface $logg
*
* @return mixed
* A PHP callable.
*
* @throws \LogicException
* If the controller cannot be parsed
*
* @throws \InvalidArgumentException
* If the controller class does not exist
*/
protected
function
createController
(
$controller
)
{
$controller
=
parent
::
createController
(
$controller
);
// class::method
if
(
strpos
(
$controller
,
'::'
)
!==
FALSE
)
{
list
(
$class
,
$method
)
=
explode
(
'::'
,
$controller
,
2
);
if
(
!
class_exists
(
$class
))
{
throw
new
\InvalidArgumentException
(
sprintf
(
'Class "%s" does not exist.'
,
$class
));
}
$controller
=
new
$class
();
if
(
$controller
instanceof
ContainerAwareInterface
)
{
$controller
->
setContainer
(
$this
->
container
);
}
return
array
(
$controller
,
$method
);
}
//
$controller will be an array of object and method name, per PHP's
// definition of a callable. Index 0 therefore is the object we want to
//
enhance.
if
(
$controller
[
0
]
instanceof
ContainerAwareInterface
)
{
$controller
[
0
]
->
setContainer
(
$this
->
container
);
//
service:method
if
(
substr_count
(
$controller
,
':'
)
==
1
)
{
//
controller in the service:method notation
list
(
$service
,
$method
)
=
explode
(
':'
,
$controller
,
2
);
return
array
(
$this
->
container
->
get
(
$service
),
$method
);
}
return
$controller
;
throw
new
\LogicException
(
sprintf
(
'Unable to parse the controller name "%s".'
,
$controller
))
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment