Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
drupal
Commits
81658d5c
Commit
81658d5c
authored
Jul 15, 2015
by
catch
Browse files
Issue
#2512132
by Wim Leers, effulgentsia, Fabianx: Make CSRF links cacheable
parent
5db18653
Changes
42
Hide whitespace changes
Inline
Side-by-side
core/core.services.yml
View file @
81658d5c
...
...
@@ -24,6 +24,11 @@ services:
arguments
:
[
'
@request_stack'
]
tags
:
-
{
name
:
cache.context
}
cache_context.session
:
class
:
Drupal\Core\Cache\Context\SessionCacheContext
arguments
:
[
'
@request_stack'
]
tags
:
-
{
name
:
cache.context
}
cache_context.request_format
:
class
:
Drupal\Core\Cache\Context\RequestFormatCacheContext
arguments
:
[
'
@request_stack'
]
...
...
core/lib/Drupal.php
View file @
81658d5c
...
...
@@ -504,22 +504,22 @@ public static function urlGenerator() {
* (optional) An associative array of parameter names and values.
* @param array $options
* (optional) An associative array of additional options.
* @param bool $collect_
cacheability
_metadata
* @param bool $collect_
bubbleable
_metadata
* (optional) Defaults to FALSE. When TRUE, both the generated URL and its
* associated
cacheability
metadata are returned.
* associated
bubbleable
metadata are returned.
*
* @return string|\Drupal\Core\GeneratedUrl
* A string containing a URL to the given path.
* When $collect_
cacheability
_metadata is TRUE, a GeneratedUrl object is
* returned, containing the generated URL plus
cacheability
metadata.
* When $collect_
bubbleable
_metadata is TRUE, a GeneratedUrl object is
* returned, containing the generated URL plus
bubbleable
metadata.
*
* @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
* @see \Drupal\Core\Url
* @see \Drupal\Core\Url::fromRoute()
* @see \Drupal\Core\Url::fromUri()
*/
public
static
function
url
(
$route_name
,
$route_parameters
=
array
(),
$options
=
array
(),
$collect_
cacheability
_metadata
=
FALSE
)
{
return
static
::
getContainer
()
->
get
(
'url_generator'
)
->
generateFromRoute
(
$route_name
,
$route_parameters
,
$options
,
$collect_
cacheability
_metadata
);
public
static
function
url
(
$route_name
,
$route_parameters
=
array
(),
$options
=
array
(),
$collect_
bubbleable
_metadata
=
FALSE
)
{
return
static
::
getContainer
()
->
get
(
'url_generator'
)
->
generateFromRoute
(
$route_name
,
$route_parameters
,
$options
,
$collect_
bubbleable
_metadata
);
}
/**
...
...
@@ -542,20 +542,20 @@ public static function linkGenerator() {
* The link text for the anchor tag.
* @param \Drupal\Core\Url $url
* The URL object used for the link.
* @param bool $collect_
cacheability
_metadata
* @param bool $collect_
bubbleable
_metadata
* (optional) Defaults to FALSE. When TRUE, both the generated URL and its
* associated
cacheability
metadata are returned.
* associated
bubbleable
metadata are returned.
*
* @return string|\Drupal\Core\GeneratedLink
* An HTML string containing a link to the given route and parameters.
* When $collect_
cacheability
_metadata is TRUE, a GeneratedLink object is
* returned, containing the generated link plus
cacheability
metadata.
* When $collect_
bubbleable
_metadata is TRUE, a GeneratedLink object is
* returned, containing the generated link plus
bubbleable
metadata.
*
* @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
* @see \Drupal\Core\Url
*/
public
static
function
l
(
$text
,
Url
$url
,
$collect_
cacheability
_metadata
=
FALSE
)
{
return
static
::
getContainer
()
->
get
(
'link_generator'
)
->
generate
(
$text
,
$url
,
$collect_
cacheability
_metadata
);
public
static
function
l
(
$text
,
Url
$url
,
$collect_
bubbleable
_metadata
=
FALSE
)
{
return
static
::
getContainer
()
->
get
(
'link_generator'
)
->
generate
(
$text
,
$url
,
$collect_
bubbleable
_metadata
);
}
/**
...
...
core/lib/Drupal/Core/Access/RouteProcessorCsrf.php
View file @
81658d5c
...
...
@@ -7,7 +7,7 @@
namespace
Drupal\Core\Access
;
use
Drupal\Core\
Cache\Cach
eableMetadata
;
use
Drupal\Core\
Render\Bubbl
eableMetadata
;
use
Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface
;
use
Symfony\Component\Routing\Route
;
...
...
@@ -36,7 +36,7 @@ function __construct(CsrfTokenGenerator $csrf_token) {
/**
* {@inheritdoc}
*/
public
function
processOutbound
(
$route_name
,
Route
$route
,
array
&
$parameters
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
public
function
processOutbound
(
$route_name
,
Route
$route
,
array
&
$parameters
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
if
(
$route
->
hasRequirement
(
'_csrf_token'
))
{
$path
=
ltrim
(
$route
->
getPath
(),
'/'
);
// Replace the path parameters with values from the parameters array.
...
...
@@ -45,13 +45,44 @@ public function processOutbound($route_name, Route $route, array &$parameters, C
}
// Adding this to the parameters means it will get merged into the query
// string when the route is compiled.
$parameters
[
'token'
]
=
$this
->
csrfToken
->
get
(
$path
);
if
(
$cacheable_metadata
)
{
// Tokens are per user and per session, so not cacheable.
// @todo Improve in https://www.drupal.org/node/2351015.
$cacheable_metadata
->
setCacheMaxAge
(
0
);
if
(
!
$bubbleable_metadata
)
{
$parameters
[
'token'
]
=
$this
->
csrfToken
->
get
(
$path
);
}
else
{
// Generate a placeholder and a render array to replace it.
$placeholder
=
hash
(
'sha1'
,
$path
);
$placeholder_render_array
=
[
'#lazy_builder'
=>
[
'route_processor_csrf:renderPlaceholderCsrfToken'
,
[
$path
]],
];
// Instead of setting an actual CSRF token as the query string, we set
// the placeholder, which will be replaced at the very last moment. This
// ensures links with CSRF tokens don't break cacheability.
$parameters
[
'token'
]
=
$placeholder
;
$bubbleable_metadata
->
addAttachments
([
'placeholders'
=>
[
$placeholder
=>
$placeholder_render_array
]]);
}
}
}
/**
* #lazy_builder callback; gets a CSRF token for the given path.
*
* @param string $path
* The path to get a CSRF token for.
*
* @return array
* A renderable array representing the CSRF token.
*/
public
function
renderPlaceholderCsrfToken
(
$path
)
{
return
[
'#markup'
=>
$this
->
csrfToken
->
get
(
$path
),
// Tokens are per session.
'#cache'
=>
[
'contexts'
=>
[
'session'
,
],
],
];
}
}
core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php
0 → 100644
View file @
81658d5c
<?php
/**
* @file
* Contains \Drupal\Core\Cache\Context\SessionCacheContext.
*/
namespace
Drupal\Core\Cache\Context
;
/**
* Defines the SessionCacheContext service, for "per session" caching.
*
* Cache context ID: 'session'.
*/
class
SessionCacheContext
extends
RequestStackCacheContextBase
{
/**
* {@inheritdoc}
*/
public
static
function
getLabel
()
{
return
t
(
'Session'
);
}
/**
* {@inheritdoc}
*/
public
function
getContext
()
{
return
$this
->
requestStack
->
getCurrentRequest
()
->
getSession
()
->
getId
();
}
}
core/lib/Drupal/Core/GeneratedLink.php
View file @
81658d5c
...
...
@@ -7,7 +7,7 @@
namespace
Drupal\Core
;
use
Drupal\Core\
Cache\Cach
eableMetadata
;
use
Drupal\Core\
Render\Bubbl
eableMetadata
;
/**
* Used to return generated links, along with associated cacheability metadata.
...
...
@@ -15,7 +15,7 @@
* Note: not to be confused with \Drupal\Core\Link, which is for passing around
* ungenerated links (typically link text + route name + route parameters).
*/
class
GeneratedLink
extends
Cach
eableMetadata
{
class
GeneratedLink
extends
Bubbl
eableMetadata
{
/**
* The HTML string value containing a link.
...
...
core/lib/Drupal/Core/GeneratedUrl.php
View file @
81658d5c
...
...
@@ -7,15 +7,15 @@
namespace
Drupal\Core
;
use
Drupal\Core\
Cache\Cach
eableMetadata
;
use
Drupal\Core\
Render\Bubbl
eableMetadata
;
/**
* Used to return generated URLs, along with associated
cacheability
metadata.
* Used to return generated URLs, along with associated
bubbleable
metadata.
*
* Note: not to be confused with \Drupal\Core\Url, which is for passing around
* ungenerated URLs (typically route name + route parameters).
*/
class
GeneratedUrl
extends
Cach
eableMetadata
{
class
GeneratedUrl
extends
Bubbl
eableMetadata
{
/**
* The string value of the URL.
...
...
core/lib/Drupal/Core/Link.php
View file @
81658d5c
...
...
@@ -122,17 +122,17 @@ public function setUrl(Url $url) {
/**
* Generates the HTML for this Link object.
*
* @param bool $collect_
cacheability
_metadata
* @param bool $collect_
bubbleable
_metadata
* (optional) Defaults to FALSE. When TRUE, both the generated link and its
* associated
cacheability
metadata are returned.
* associated
bubbleable
metadata are returned.
*
* @return string|\Drupal\Core\GeneratedLink
* The link HTML markup.
* When $collect_
cacheability
_metadata is TRUE, a GeneratedLink object is
* returned, containing the generated link plus
cacheability
metadata.
* When $collect_
bubbleable
_metadata is TRUE, a GeneratedLink object is
* returned, containing the generated link plus
bubbleable
metadata.
*/
public
function
toString
(
$collect_
cacheability
_metadata
=
FALSE
)
{
return
$this
->
getLinkGenerator
()
->
generateFromLink
(
$this
,
$collect_
cacheability
_metadata
);
public
function
toString
(
$collect_
bubbleable
_metadata
=
FALSE
)
{
return
$this
->
getLinkGenerator
()
->
generateFromLink
(
$this
,
$collect_
bubbleable
_metadata
);
}
}
core/lib/Drupal/Core/PathProcessor/OutboundPathProcessorInterface.php
View file @
81658d5c
...
...
@@ -7,7 +7,7 @@
namespace
Drupal\Core\PathProcessor
;
use
Drupal\Core\
Cache\Cach
eableMetadata
;
use
Drupal\Core\
Render\Bubbl
eableMetadata
;
use
Symfony\Component\HttpFoundation\Request
;
/**
...
...
@@ -25,12 +25,12 @@ interface OutboundPathProcessorInterface {
* generateFromPath() method.
* @param \Symfony\Component\HttpFoundation\Request $request
* The HttpRequest object representing the current request.
* @param \Drupal\Core\
Cache\Cach
eableMetadata $
cach
eable_metadata
* (optional) Object to collect path processors'
cacheability
.
* @param \Drupal\Core\
Render\Bubbl
eableMetadata $
bubbl
eable_metadata
* (optional) Object to collect path processors'
bubbleable metadata
.
*
* @return
* The processed path.
*/
public
function
processOutbound
(
$path
,
&
$options
=
array
(),
Request
$request
=
NULL
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
);
public
function
processOutbound
(
$path
,
&
$options
=
array
(),
Request
$request
=
NULL
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
);
}
core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php
View file @
81658d5c
...
...
@@ -7,8 +7,8 @@
namespace
Drupal\Core\PathProcessor
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Path\AliasManagerInterface
;
use
Drupal\Core\Render\BubbleableMetadata
;
use
Symfony\Component\HttpFoundation\Request
;
/**
...
...
@@ -44,7 +44,7 @@ public function processInbound($path, Request $request) {
/**
* Implements Drupal\Core\PathProcessor\OutboundPathProcessorInterface::processOutbound().
*/
public
function
processOutbound
(
$path
,
&
$options
=
array
(),
Request
$request
=
NULL
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
public
function
processOutbound
(
$path
,
&
$options
=
array
(),
Request
$request
=
NULL
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
if
(
empty
(
$options
[
'alias'
]))
{
$langcode
=
isset
(
$options
[
'language'
])
?
$options
[
'language'
]
->
getId
()
:
NULL
;
$path
=
$this
->
aliasManager
->
getAliasByPath
(
$path
,
$langcode
);
...
...
core/lib/Drupal/Core/PathProcessor/PathProcessorFront.php
View file @
81658d5c
...
...
@@ -7,8 +7,8 @@
namespace
Drupal\Core\PathProcessor
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Render\BubbleableMetadata
;
use
Symfony\Component\HttpFoundation\Request
;
/**
...
...
@@ -48,7 +48,7 @@ public function processInbound($path, Request $request) {
/**
* Implements Drupal\Core\PathProcessor\OutboundPathProcessorInterface::processOutbound().
*/
public
function
processOutbound
(
$path
,
&
$options
=
array
(),
Request
$request
=
NULL
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
public
function
processOutbound
(
$path
,
&
$options
=
array
(),
Request
$request
=
NULL
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
// The special path '<front>' links to the default front page.
if
(
$path
===
'/<front>'
)
{
$path
=
'/'
;
...
...
core/lib/Drupal/Core/PathProcessor/PathProcessorManager.php
View file @
81658d5c
...
...
@@ -7,7 +7,7 @@
namespace
Drupal\Core\PathProcessor
;
use
Drupal\Core\
Cache\Cach
eableMetadata
;
use
Drupal\Core\
Render\Bubbl
eableMetadata
;
use
Symfony\Component\HttpFoundation\Request
;
/**
...
...
@@ -108,10 +108,10 @@ public function addOutbound(OutboundPathProcessorInterface $processor, $priority
/**
* Implements Drupal\Core\PathProcessor\OutboundPathProcessorInterface::processOutbound().
*/
public
function
processOutbound
(
$path
,
&
$options
=
array
(),
Request
$request
=
NULL
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
public
function
processOutbound
(
$path
,
&
$options
=
array
(),
Request
$request
=
NULL
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
$processors
=
$this
->
getOutbound
();
foreach
(
$processors
as
$processor
)
{
$path
=
$processor
->
processOutbound
(
$path
,
$options
,
$request
,
$
cach
eable_metadata
);
$path
=
$processor
->
processOutbound
(
$path
,
$options
,
$request
,
$
bubbl
eable_metadata
);
}
return
$path
;
}
...
...
core/lib/Drupal/Core/Render/BubbleableMetadata.php
View file @
81658d5c
...
...
@@ -73,6 +73,27 @@ public static function createFromRenderArray(array $build) {
return
$meta
;
}
/**
* Creates a bubbleable metadata object from a depended object.
*
* @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object
* The object whose cacheability metadata to retrieve. If it implements
* CacheableDependencyInterface, its cacheability metadata will be used,
* otherwise, the passed in object must be assumed to be uncacheable, so
* max-age 0 is set.
*
* @return static
*/
public
static
function
createFromObject
(
$object
)
{
$meta
=
parent
::
createFromObject
(
$object
);
if
(
$object
instanceof
AttachmentsInterface
)
{
$meta
->
attachments
=
$object
->
getAttachments
();
}
return
$meta
;
}
/**
* Merges two attachments arrays (which live under the '#attached' key).
*
...
...
core/lib/Drupal/Core/Render/Element/Link.php
View file @
81658d5c
...
...
@@ -10,6 +10,7 @@
use
Drupal\Component\Utility\NestedArray
;
use
Drupal\Component\Utility\Html
as
HtmlUtility
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Render\BubbleableMetadata
;
use
Drupal\Core\Url
as
CoreUrl
;
/**
...
...
@@ -83,7 +84,7 @@ public static function preRenderLink($element) {
$link_generator
=
\
Drupal
::
service
(
'link_generator'
);
$generated_link
=
$link_generator
->
generate
(
$element
[
'#title'
],
$element
[
'#url'
]
->
setOptions
(
$options
),
TRUE
);
$element
[
'#markup'
]
=
$generated_link
->
getGeneratedLink
();
$generated_link
->
merge
(
Cach
eableMetadata
::
createFromRenderArray
(
$element
))
$generated_link
->
merge
(
Bubbl
eableMetadata
::
createFromRenderArray
(
$element
))
->
applyTo
(
$element
);
}
return
$element
;
...
...
core/lib/Drupal/Core/RouteProcessor/OutboundRouteProcessorInterface.php
View file @
81658d5c
...
...
@@ -7,7 +7,7 @@
namespace
Drupal\Core\RouteProcessor
;
use
Drupal\Core\
Cache\Cach
eableMetadata
;
use
Drupal\Core\
Render\Bubbl
eableMetadata
;
use
Symfony\Component\Routing\Route
;
/**
...
...
@@ -25,12 +25,12 @@ interface OutboundRouteProcessorInterface {
* @param array $parameters
* An array of parameters to be passed to the route compiler. Passed by
* reference.
* @param \Drupal\Core\
Cache\Cach
eableMetadata $
cach
eable_metadata
* (optional) Object to collect route processors'
cacheability
.
* @param \Drupal\Core\
Render\Bubbl
eableMetadata $
bubbl
eable_metadata
* (optional) Object to collect route processors'
bubbleable metadata
.
*
* @return
* The processed path.
*/
public
function
processOutbound
(
$route_name
,
Route
$route
,
array
&
$parameters
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
);
public
function
processOutbound
(
$route_name
,
Route
$route
,
array
&
$parameters
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
);
}
core/lib/Drupal/Core/RouteProcessor/RouteProcessorCurrent.php
View file @
81658d5c
...
...
@@ -8,6 +8,7 @@
namespace
Drupal\Core\RouteProcessor
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Render\BubbleableMetadata
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Symfony\Component\Routing\Route
;
...
...
@@ -36,7 +37,7 @@ public function __construct(RouteMatchInterface $route_match) {
/**
* {@inheritdoc}
*/
public
function
processOutbound
(
$route_name
,
Route
$route
,
array
&
$parameters
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
public
function
processOutbound
(
$route_name
,
Route
$route
,
array
&
$parameters
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
if
(
$route_name
===
'<current>'
)
{
if
(
$current_route
=
$this
->
routeMatch
->
getRouteObject
())
{
$requirements
=
$current_route
->
getRequirements
();
...
...
@@ -52,8 +53,8 @@ public function processOutbound($route_name, Route $route, array &$parameters, C
$route
->
setOptions
(
$current_route
->
getOptions
());
$route
->
setDefaults
(
$current_route
->
getDefaults
());
$parameters
=
array_merge
(
$parameters
,
$this
->
routeMatch
->
getRawParameters
()
->
all
());
if
(
$
cach
eable_metadata
)
{
$
cach
eable_metadata
->
addCacheContexts
([
'route'
]);
if
(
$
bubbl
eable_metadata
)
{
$
bubbl
eable_metadata
->
addCacheContexts
([
'route'
]);
}
}
else
{
...
...
core/lib/Drupal/Core/RouteProcessor/RouteProcessorManager.php
View file @
81658d5c
...
...
@@ -7,7 +7,7 @@
namespace
Drupal\Core\RouteProcessor
;
use
Drupal\Core\
Cache\Cach
eableMetadata
;
use
Drupal\Core\
Render\Bubbl
eableMetadata
;
use
Symfony\Component\Routing\Route
;
/**
...
...
@@ -51,10 +51,10 @@ public function addOutbound(OutboundRouteProcessorInterface $processor, $priorit
/**
* {@inheritdoc}
*/
public
function
processOutbound
(
$route_name
,
Route
$route
,
array
&
$parameters
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
public
function
processOutbound
(
$route_name
,
Route
$route
,
array
&
$parameters
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
$processors
=
$this
->
getOutbound
();
foreach
(
$processors
as
$processor
)
{
$processor
->
processOutbound
(
$route_name
,
$route
,
$parameters
,
$
cach
eable_metadata
);
$processor
->
processOutbound
(
$route_name
,
$route
,
$parameters
,
$
bubbl
eable_metadata
);
}
}
...
...
core/lib/Drupal/Core/Routing/NullGenerator.php
View file @
81658d5c
...
...
@@ -7,7 +7,7 @@
namespace
Drupal\Core\Routing
;
use
Drupal\Core\
Cache\Cach
eableMetadata
;
use
Drupal\Core\
Render\Bubbl
eableMetadata
;
use
Symfony\Component\HttpFoundation\RequestStack
;
use
Symfony\Component\Routing\RequestContext
as
SymfonyRequestContext
;
use
Symfony\Component\Routing\Exception\RouteNotFoundException
;
...
...
@@ -51,7 +51,7 @@ protected function getRoute($name) {
/**
* {@inheritdoc}
*/
protected
function
processRoute
(
$name
,
Route
$route
,
array
&
$parameters
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
protected
function
processRoute
(
$name
,
Route
$route
,
array
&
$parameters
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
}
/**
...
...
@@ -76,7 +76,7 @@ public function getContext() {
/**
* Overrides Drupal\Core\Routing\UrlGenerator::processPath().
*/
protected
function
processPath
(
$path
,
&
$options
=
array
(),
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
protected
function
processPath
(
$path
,
&
$options
=
array
(),
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
return
$path
;
}
}
core/lib/Drupal/Core/Routing/UrlGenerator.php
View file @
81658d5c
...
...
@@ -7,8 +7,8 @@
namespace
Drupal\Core\Routing
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\GeneratedUrl
;
use
Drupal\Core\Render\BubbleableMetadata
;
use
Symfony\Component\HttpFoundation\RequestStack
;
use
Symfony\Component\Routing\RequestContext
as
SymfonyRequestContext
;
use
Symfony\Component\Routing\Route
as
SymfonyRoute
;
...
...
@@ -277,8 +277,8 @@ public function generate($name, $parameters = array(), $absolute = FALSE) {
/**
* {@inheritdoc}
*/
public
function
generateFromRoute
(
$name
,
$parameters
=
array
(),
$options
=
array
(),
$collect_
cacheability
_metadata
=
FALSE
)
{
$generated_url
=
$collect_
cacheability
_metadata
?
new
GeneratedUrl
()
:
NULL
;
public
function
generateFromRoute
(
$name
,
$parameters
=
array
(),
$options
=
array
(),
$collect_
bubbleable
_metadata
=
FALSE
)
{
$generated_url
=
$collect_
bubbleable
_metadata
?
new
GeneratedUrl
()
:
NULL
;
$options
+=
array
(
'prefix'
=>
''
);
$route
=
$this
->
getRoute
(
$name
);
...
...
@@ -321,7 +321,7 @@ public function generateFromRoute($name, $parameters = array(), $options = array
}
$url
=
$base_url
.
$path
.
$fragment
;
return
$collect_
cacheability
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
return
$collect_
bubbleable
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
}
$base_url
=
$this
->
context
->
getBaseUrl
();
...
...
@@ -330,11 +330,11 @@ public function generateFromRoute($name, $parameters = array(), $options = array
if
(
!
$absolute
||
!
$host
=
$this
->
context
->
getHost
())
{
if
(
$route
->
getOption
(
'_only_fragment'
))
{
return
$collect_
cacheability
_metadata
?
$generated_url
->
setGeneratedUrl
(
$fragment
)
:
$fragment
;
return
$collect_
bubbleable
_metadata
?
$generated_url
->
setGeneratedUrl
(
$fragment
)
:
$fragment
;
}
$url
=
$base_url
.
$path
.
$fragment
;
return
$collect_
cacheability
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
return
$collect_
bubbleable
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
}
// Prepare an absolute URL by getting the correct scheme, host and port from
...
...
@@ -355,19 +355,19 @@ public function generateFromRoute($name, $parameters = array(), $options = array
}
elseif
(
'https'
===
$scheme
&&
443
!=
$this
->
context
->
getHttpsPort
())
{
$port
=
':'
.
$this
->
context
->
getHttpsPort
();
}
if
(
$collect_
cacheability
_metadata
)
{
if
(
$collect_
bubbleable
_metadata
)
{
$generated_url
->
addCacheContexts
([
'url.site'
]);
}
$url
=
$scheme
.
'://'
.
$host
.
$port
.
$base_url
.
$path
.
$fragment
;
return
$collect_
cacheability
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
return
$collect_
bubbleable
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
}
/**
* {@inheritdoc}
*/
public
function
generateFromPath
(
$path
=
NULL
,
$options
=
array
(),
$collect_
cacheability
_metadata
=
FALSE
)
{
$generated_url
=
$collect_
cacheability
_metadata
?
new
GeneratedUrl
()
:
NULL
;
public
function
generateFromPath
(
$path
=
NULL
,
$options
=
array
(),
$collect_
bubbleable
_metadata
=
FALSE
)
{
$generated_url
=
$collect_
bubbleable
_metadata
?
new
GeneratedUrl
()
:
NULL
;
$request
=
$this
->
requestStack
->
getCurrentRequest
();
$current_base_path
=
$request
->
getBasePath
()
.
'/'
;
...
...
@@ -432,7 +432,7 @@ public function generateFromPath($path = NULL, $options = array(), $collect_cach
}
// Reassemble.
$url
=
$path
.
$options
[
'fragment'
];
return
$collect_
cacheability
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
return
$collect_
bubbleable
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
}
else
{
$path
=
ltrim
(
$this
->
processPath
(
'/'
.
$path
,
$options
,
$generated_url
),
'/'
);
...
...
@@ -463,20 +463,20 @@ public function generateFromPath($path = NULL, $options = array(), $collect_cach
$base
=
$options
[
'absolute'
]
?
$options
[
'base_url'
]
:
$current_base_path
;
$prefix
=
empty
(
$path
)
?
rtrim
(
$options
[
'prefix'
],
'/'
)
:
$options
[
'prefix'
];
if
(
$options
[
'absolute'
]
&&
$collect_
cacheability
_metadata
)
{
if
(
$options
[
'absolute'
]
&&
$collect_
bubbleable
_metadata
)
{
$generated_url
->
addCacheContexts
([
'url.site'
]);
}
$path
=
str_replace
(
'%2F'
,
'/'
,
rawurlencode
(
$prefix
.
$path
));
$query
=
$options
[
'query'
]
?
(
'?'
.
UrlHelper
::
buildQuery
(
$options
[
'query'
]))
:
''
;
$url
=
$base
.
$options
[
'script'
]
.
$path
.
$query
.
$options
[
'fragment'
];
return
$collect_
cacheability
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
return
$collect_
bubbleable
_metadata
?
$generated_url
->
setGeneratedUrl
(
$url
)
:
$url
;
}
/**
* Passes the path to a processor manager to allow alterations.
*/
protected
function
processPath
(
$path
,
&
$options
=
array
(),
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
protected
function
processPath
(
$path
,
&
$options
=
array
(),
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
// Router-based paths may have a querystring on them.
if
(
$query_pos
=
strpos
(
$path
,
'?'
))
{
// We don't need to do a strict check here because position 0 would mean we
...
...
@@ -488,7 +488,7 @@ protected function processPath($path, &$options = array(), CacheableMetadata $ca
$actual_path
=
$path
;
$query_string
=
''
;
}
$path
=
$this
->
pathProcessor
->
processOutbound
(
$actual_path
===
'/'
?
$actual_path
:
rtrim
(
$actual_path
,
'/'
),
$options
,
$this
->
requestStack
->
getCurrentRequest
(),
$
cach
eable_metadata
);
$path
=
$this
->
pathProcessor
->
processOutbound
(
$actual_path
===
'/'
?
$actual_path
:
rtrim
(
$actual_path
,
'/'
),
$options
,
$this
->
requestStack
->
getCurrentRequest
(),
$
bubbl
eable_metadata
);
$path
.
=
$query_string
;
return
$path
;
}
...
...
@@ -502,11 +502,11 @@ protected function processPath($path, &$options = array(), CacheableMetadata $ca
* The route object to process.
* @param array $parameters
* An array of parameters to be passed to the route compiler.
* @param \Drupal\Core\
Cache\Cach
eableMetadata $
cach
eable_metadata
* (optional) Object to collect route processors'
cacheability
.
* @param \Drupal\Core\
Render\Bubbl
eableMetadata $
bubbl
eable_metadata
* (optional) Object to collect route processors'
bubbleable metadata
.
*/
protected
function
processRoute
(
$name
,
SymfonyRoute
$route
,
array
&
$parameters
,
Cach
eableMetadata
$
cach
eable_metadata
=
NULL
)
{
$this
->
routeProcessor
->
processOutbound
(
$name
,
$route
,
$parameters
,
$
cach
eable_metadata
);
protected
function
processRoute
(
$name
,
SymfonyRoute
$route
,
array
&
$parameters
,
Bubbl
eableMetadata
$
bubbl
eable_metadata
=
NULL
)
{
$this
->
routeProcessor
->
processOutbound
(
$name
,
$route
,
$parameters
,
$
bubbl
eable_metadata
);
}
/**
...
...
core/lib/Drupal/Core/Routing/UrlGeneratorInterface.php
View file @
81658d5c
...
...
@@ -70,14 +70,14 @@ interface UrlGeneratorInterface extends VersatileGeneratorInterface {
* set if _url() is invoked by Drupal\Core\Entity\Entity::uri().
* - 'entity': The entity object (such as a node) for which the URL is being
* generated. Only set if _url() is invoked by Drupal\Core\Entity\Entity::uri().
* @param bool $collect_
cacheability
_metadata
* @param bool $collect_
bubbleable
_metadata
* (optional) Defaults to FALSE. When TRUE, both the generated URL and its
* associated
cacheability
metadata are returned.
* associated
bubbleable
metadata are returned.