Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 314111

[meta] Stop using $request->attributes->get(MAGIC_KEY) as a public API

$
0
0

Problem/Motivation

$request->attributes is currently a dumping ground for:

  • Route parameters defined within braces of the route's path pattern (e.g., 'node' if the path pattern is /node/{node}).
  • Various other things that should start with "_" in order to not collide with route parameters, whose values either come from the 'defaults' portion of a route definition or from any other code that calls $request->attributes->set().

For the former, see #2256669: [meta] Finalize module-facing API of getting the matched route name and a matched route argument for a request. This issue is now focused on the latter.

The problems with the latter are:

  1. It's not friendly DX to access them directly. For example, for $request->attributes->get('_system_path'), there's no documentation about the existence of '_system_path' as a magic string and what it returns. Even if we created such documentation, it wouldn't be discoverable by IDEs.
  2. It's not clear which magic keys are intended for public use and which are internal. Historically in Drupal, a leading underscore would denote internal use only.
  3. Before or after 8.0.0 is released, there may be technical reasons to change from storing the data on $request to storing it elsewhere or to computing it dynamically. If contrib modules hardcode their access via $request->attributes, this will lead to BC breaks.
  4. These problems get compounded if contrib modules follow core practices and add their own magic key/value pairs in $request->attributes without exposing a real API around them.

Here's a grep of HEAD as of May 20, 2014 that orders keys that start with "_" by number of lines of code that access it:

grep -hor --exclude-dir core/vendor "\->attributes->get('_[^,)]*" core | sort | uniq -c | sort -nr
  12 ->attributes->get('_system_path'
      9 ->attributes->get('_raw_variables'
      4 ->attributes->get('_controller'
      3 ->attributes->get('_route_params'
      2 ->attributes->get('_format'
      2 ->attributes->get('_authentication_provider'
      1 ->attributes->get('_previous_optional_argument'
      1 ->attributes->get('_optional_argument'
      1 ->attributes->get('_locale'
      1 ->attributes->get('_http_statuscode'
      1 ->attributes->get('_hello'
      1 ->attributes->get('_form'

Proposed resolution

Remaining tasks

See above.


Viewing all articles
Browse latest Browse all 314111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>