public CacheContextsManager::validateTokens(array $context_tokens = [])
Validates an array of cache context tokens.
Can be called before using cache contexts in operations, to check validity.
Parameters
string[] $context_tokens: An array of cache context tokens.
Throws
\LogicException
See also
\Drupal\Core\Cache\Context\CacheContextsManager::parseTokens()
File
- core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php, line 258
Class
- CacheContextsManager
- Converts cache context tokens into cache keys.
Namespace
Drupal\Core\Cache\Context
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public function validateTokens( array $context_tokens = []) { if ( empty ( $context_tokens )) { return ; } // Initialize the set of valid context tokens with the container's contexts. if (!isset( $this ->validContextTokens)) { $this ->validContextTokens = array_flip ( $this ->contexts); } foreach ( $context_tokens as $context_token ) { if (! is_string ( $context_token )) { throw new \LogicException(sprintf( 'Cache contexts must be strings, %s given.' , gettype ( $context_token ))); } if (isset( $this ->validContextTokens[ $context_token ])) { continue ; } // If it's a valid context token, then the ID must be stored in the set // of valid context tokens (since we initialized it with the list of cache // context IDs using the container). In case of an invalid context token, // throw an exception, otherwise cache it, including the parameter, to // minimize the amount of work in future ::validateContexts() calls. $context_id = $context_token ; $colon_pos = strpos ( $context_id , ':' ); if ( $colon_pos !== FALSE) { $context_id = substr ( $context_id , 0, $colon_pos ); } if (isset( $this ->validContextTokens[ $context_id ])) { $this ->validContextTokens[ $context_token ] = TRUE; } else { throw new \LogicException(sprintf( '"%s" is not a valid cache context ID.' , $context_id )); } } } |
Please login to continue.