Issue #1629858 by aspilicious, amateescu: Convert the theme registry to PSR-0.
parent
480497f1a7
commit
a2f971e4c4
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
use Drupal\Core\Utility\CacheArray;
|
|
||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* The theme system, which controls the output of Drupal.
|
* The theme system, which controls the output of Drupal.
|
||||||
|
@ -8,6 +8,8 @@ use Drupal\Core\Utility\CacheArray;
|
||||||
* customized by user themes.
|
* customized by user themes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use Drupal\Core\Utility\ThemeRegistry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @defgroup content_flags Content markers
|
* @defgroup content_flags Content markers
|
||||||
* @{
|
* @{
|
||||||
|
@ -239,17 +241,17 @@ function _drupal_theme_initialize($theme, $base_theme = array(), $registry_callb
|
||||||
*
|
*
|
||||||
* @param bool $complete
|
* @param bool $complete
|
||||||
* Optional boolean to indicate whether to return the complete theme registry
|
* Optional boolean to indicate whether to return the complete theme registry
|
||||||
* array or an instance of the ThemeRegistry class. If TRUE, the complete
|
* array or an instance of the Drupal\Core\Utility\ThemeRegistry class.
|
||||||
* theme registry array will be returned. This is useful if you want to
|
* If TRUE, the complete theme registry array will be returned. This is useful
|
||||||
* foreach over the whole registry, use array_* functions or inspect it in a
|
* if you want to foreach over the whole registry, use array_* functions or
|
||||||
* debugger. If FALSE, an instance of the ThemeRegistry class will be
|
* inspect it in a debugger. If FALSE, an instance of the
|
||||||
* returned, this provides an ArrayObject which allows it to be accessed
|
* Drupal\Core\Utility\ThemeRegistry class will be returned, this provides an
|
||||||
* with array syntax and isset(), and should be more lightweight
|
* ArrayObject which allows it to be accessed with array syntax and isset(),
|
||||||
* than the full registry. Defaults to TRUE.
|
* and should be more lightweight than the full registry. Defaults to TRUE.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* The complete theme registry array, or an instance of the ThemeRegistry
|
* The complete theme registry array, or an instance of the
|
||||||
* class.
|
* Drupal\Core\Utility\ThemeRegistry class.
|
||||||
*/
|
*/
|
||||||
function theme_get_registry($complete = TRUE) {
|
function theme_get_registry($complete = TRUE) {
|
||||||
// Use the advanced drupal_static() pattern, since this is called very often.
|
// Use the advanced drupal_static() pattern, since this is called very often.
|
||||||
|
@ -307,10 +309,11 @@ function _theme_registry_callback($callback = NULL, array $arguments = array())
|
||||||
* The name of the theme engine.
|
* The name of the theme engine.
|
||||||
* @param $complete
|
* @param $complete
|
||||||
* Whether to load the complete theme registry or an instance of the
|
* Whether to load the complete theme registry or an instance of the
|
||||||
* ThemeRegistry class.
|
* Drupal\Core\Utility\ThemeRegistry class.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* The theme registry array, or an instance of the ThemeRegistry class.
|
* The theme registry array, or an instance of the
|
||||||
|
* Drupal\Core\Utility\ThemeRegistry class.
|
||||||
*/
|
*/
|
||||||
function _theme_load_registry($theme, $base_theme = NULL, $theme_engine = NULL, $complete = TRUE) {
|
function _theme_load_registry($theme, $base_theme = NULL, $theme_engine = NULL, $complete = TRUE) {
|
||||||
if ($complete) {
|
if ($complete) {
|
||||||
|
@ -352,116 +355,6 @@ function drupal_theme_rebuild() {
|
||||||
cache()->deletePrefix('theme_registry');
|
cache()->deletePrefix('theme_registry');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Builds the run-time theme registry.
|
|
||||||
*
|
|
||||||
* Extends DrupalCacheArray to allow the theme registry to be accessed as a
|
|
||||||
* complete registry, while internally caching only the parts of the registry
|
|
||||||
* that are actually in use on the site. On cache misses the complete
|
|
||||||
* theme registry is loaded and used to update the run-time cache.
|
|
||||||
*/
|
|
||||||
class ThemeRegistry Extends CacheArray {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether the partial registry can be persisted to the cache.
|
|
||||||
*
|
|
||||||
* This is only allowed if all modules and the request method is GET. theme()
|
|
||||||
* should be very rarely called on POST requests and this avoids polluting
|
|
||||||
* the runtime cache.
|
|
||||||
*/
|
|
||||||
protected $persistable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The complete theme registry array.
|
|
||||||
*/
|
|
||||||
protected $completeRegistry;
|
|
||||||
|
|
||||||
function __construct($cid, $bin) {
|
|
||||||
$this->cid = $cid;
|
|
||||||
$this->bin = $bin;
|
|
||||||
$this->persistable = module_load_all(NULL) && $_SERVER['REQUEST_METHOD'] == 'GET';
|
|
||||||
|
|
||||||
$data = array();
|
|
||||||
if ($this->persistable && $cached = cache($this->bin)->get($this->cid)) {
|
|
||||||
$data = $cached->data;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// If there is no runtime cache stored, fetch the full theme registry,
|
|
||||||
// but then initialize each value to NULL. This allows offsetExists()
|
|
||||||
// to function correctly on non-registered theme hooks without triggering
|
|
||||||
// a call to resolveCacheMiss().
|
|
||||||
$data = $this->initializeRegistry();
|
|
||||||
if ($this->persistable) {
|
|
||||||
$this->set($data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->storage = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the full theme registry.
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
* An array with the keys of the full theme registry, but the values
|
|
||||||
* initialized to NULL.
|
|
||||||
*/
|
|
||||||
function initializeRegistry() {
|
|
||||||
$this->completeRegistry = theme_get_registry();
|
|
||||||
|
|
||||||
return array_fill_keys(array_keys($this->completeRegistry), NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetExists($offset) {
|
|
||||||
// Since the theme registry allows for theme hooks to be requested that
|
|
||||||
// are not registered, just check the existence of the key in the registry.
|
|
||||||
// Use array_key_exists() here since a NULL value indicates that the theme
|
|
||||||
// hook exists but has not yet been requested.
|
|
||||||
return array_key_exists($offset, $this->storage);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetGet($offset) {
|
|
||||||
// If the offset is set but empty, it is a registered theme hook that has
|
|
||||||
// not yet been requested. Offsets that do not exist at all were not
|
|
||||||
// registered in hook_theme().
|
|
||||||
if (isset($this->storage[$offset])) {
|
|
||||||
return $this->storage[$offset];
|
|
||||||
}
|
|
||||||
elseif (array_key_exists($offset, $this->storage)) {
|
|
||||||
return $this->resolveCacheMiss($offset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function resolveCacheMiss($offset) {
|
|
||||||
if (!isset($this->completeRegistry)) {
|
|
||||||
$this->completeRegistry = theme_get_registry();
|
|
||||||
}
|
|
||||||
$this->storage[$offset] = $this->completeRegistry[$offset];
|
|
||||||
if ($this->persistable) {
|
|
||||||
$this->persist($offset);
|
|
||||||
}
|
|
||||||
return $this->storage[$offset];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function set($data, $lock = TRUE) {
|
|
||||||
$lock_name = $this->cid . ':' . $this->bin;
|
|
||||||
if (!$lock || lock_acquire($lock_name)) {
|
|
||||||
if ($cached = cache($this->bin)->get($this->cid)) {
|
|
||||||
// Use array merge instead of union so that filled in values in $data
|
|
||||||
// overwrite empty values in the current cache.
|
|
||||||
$data = array_merge($cached->data, $data);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$registry = $this->initializeRegistry();
|
|
||||||
$data = array_merge($registry, $data);
|
|
||||||
}
|
|
||||||
cache($this->bin)->set($this->cid, $data);
|
|
||||||
if ($lock) {
|
|
||||||
lock_release($lock_name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process a single implementation of hook_theme().
|
* Process a single implementation of hook_theme().
|
||||||
*
|
*
|
||||||
|
|
|
@ -0,0 +1,118 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Definition of Drupal\Core\Utility\ThemeRegistry
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\Core\Utility;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the run-time theme registry.
|
||||||
|
*
|
||||||
|
* Extends DrupalCacheArray to allow the theme registry to be accessed as a
|
||||||
|
* complete registry, while internally caching only the parts of the registry
|
||||||
|
* that are actually in use on the site. On cache misses the complete
|
||||||
|
* theme registry is loaded and used to update the run-time cache.
|
||||||
|
*/
|
||||||
|
class ThemeRegistry extends CacheArray {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the partial registry can be persisted to the cache.
|
||||||
|
*
|
||||||
|
* This is only allowed if all modules and the request method is GET. theme()
|
||||||
|
* should be very rarely called on POST requests and this avoids polluting
|
||||||
|
* the runtime cache.
|
||||||
|
*/
|
||||||
|
protected $persistable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The complete theme registry array.
|
||||||
|
*/
|
||||||
|
protected $completeRegistry;
|
||||||
|
|
||||||
|
function __construct($cid, $bin) {
|
||||||
|
$this->cid = $cid;
|
||||||
|
$this->bin = $bin;
|
||||||
|
$this->persistable = module_load_all(NULL) && $_SERVER['REQUEST_METHOD'] == 'GET';
|
||||||
|
|
||||||
|
$data = array();
|
||||||
|
if ($this->persistable && $cached = cache($this->bin)->get($this->cid)) {
|
||||||
|
$data = $cached->data;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// If there is no runtime cache stored, fetch the full theme registry,
|
||||||
|
// but then initialize each value to NULL. This allows offsetExists()
|
||||||
|
// to function correctly on non-registered theme hooks without triggering
|
||||||
|
// a call to resolveCacheMiss().
|
||||||
|
$data = $this->initializeRegistry();
|
||||||
|
if ($this->persistable) {
|
||||||
|
$this->set($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->storage = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the full theme registry.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* An array with the keys of the full theme registry, but the values
|
||||||
|
* initialized to NULL.
|
||||||
|
*/
|
||||||
|
function initializeRegistry() {
|
||||||
|
$this->completeRegistry = theme_get_registry();
|
||||||
|
|
||||||
|
return array_fill_keys(array_keys($this->completeRegistry), NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetExists($offset) {
|
||||||
|
// Since the theme registry allows for theme hooks to be requested that
|
||||||
|
// are not registered, just check the existence of the key in the registry.
|
||||||
|
// Use array_key_exists() here since a NULL value indicates that the theme
|
||||||
|
// hook exists but has not yet been requested.
|
||||||
|
return array_key_exists($offset, $this->storage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetGet($offset) {
|
||||||
|
// If the offset is set but empty, it is a registered theme hook that has
|
||||||
|
// not yet been requested. Offsets that do not exist at all were not
|
||||||
|
// registered in hook_theme().
|
||||||
|
if (isset($this->storage[$offset])) {
|
||||||
|
return $this->storage[$offset];
|
||||||
|
}
|
||||||
|
elseif (array_key_exists($offset, $this->storage)) {
|
||||||
|
return $this->resolveCacheMiss($offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolveCacheMiss($offset) {
|
||||||
|
if (!isset($this->completeRegistry)) {
|
||||||
|
$this->completeRegistry = theme_get_registry();
|
||||||
|
}
|
||||||
|
$this->storage[$offset] = $this->completeRegistry[$offset];
|
||||||
|
if ($this->persistable) {
|
||||||
|
$this->persist($offset);
|
||||||
|
}
|
||||||
|
return $this->storage[$offset];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set($data, $lock = TRUE) {
|
||||||
|
$lock_name = $this->cid . ':' . $this->bin;
|
||||||
|
if (!$lock || lock_acquire($lock_name)) {
|
||||||
|
if ($cached = cache($this->bin)->get($this->cid)) {
|
||||||
|
// Use array merge instead of union so that filled in values in $data
|
||||||
|
// overwrite empty values in the current cache.
|
||||||
|
$data = array_merge($cached->data, $data);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$registry = $this->initializeRegistry();
|
||||||
|
$data = array_merge($registry, $data);
|
||||||
|
}
|
||||||
|
cache($this->bin)->set($this->cid, $data);
|
||||||
|
if ($lock) {
|
||||||
|
lock_release($lock_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,7 +8,7 @@
|
||||||
namespace Drupal\system\Tests\Theme;
|
namespace Drupal\system\Tests\Theme;
|
||||||
|
|
||||||
use Drupal\simpletest\WebTestBase;
|
use Drupal\simpletest\WebTestBase;
|
||||||
use ThemeRegistry;
|
use Drupal\Core\Utility\ThemeRegistry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for the ThemeRegistry class.
|
* Tests for the ThemeRegistry class.
|
||||||
|
|
Loading…
Reference in New Issue