StaticConfigTrait.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v3.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Core;
  18. use Cake\Error;
  19. /**
  20. * A trait that provides a set of static methods to manage configuration
  21. * for classes that provide an adapter facade or need to have sets of
  22. * configuration data registered and manipulated.
  23. *
  24. * Implementing objects are expected to declare a static `$_config` property.
  25. */
  26. trait StaticConfigTrait {
  27. /**
  28. * This method can be used to define confguration adapters for an application
  29. * or read existing configuration.
  30. *
  31. * To change an adapter's configuration at runtime, first drop the adapter and then
  32. * reconfigure it.
  33. *
  34. * Adapters will not be constructed until the first operation is done.
  35. *
  36. * ### Usage
  37. *
  38. * Assuming that the class' name is `Cache` the following scenarios
  39. * are supported:
  40. *
  41. * Reading config data back:
  42. *
  43. * `Cache::config('default');`
  44. *
  45. * Setting a cache engine up.
  46. *
  47. * `Cache::config('default', $settings);`
  48. *
  49. * Injecting a constructed adapter in:
  50. *
  51. * `Cache::config('default', $instance);`
  52. *
  53. * Configure multiple adapters at once:
  54. *
  55. * `Cache::config($arrayOfConfig);`
  56. *
  57. * @param string|array $key The name of the configuration, or an array of multiple configs.
  58. * @param array $config An array of name => configuration data for adapter.
  59. * @return mixed null when adding configuration and an array of configuration data when reading.
  60. * @throws Cake\Error\Exception When trying to modify an existing config.
  61. */
  62. public static function config($key, $config = null) {
  63. // Read config.
  64. if ($config === null && is_string($key)) {
  65. return isset(static::$_config[$key]) ? static::$_config[$key] : null;
  66. }
  67. if ($config === null && is_array($key)) {
  68. foreach ($key as $name => $settings) {
  69. static::config($name, $settings);
  70. }
  71. return;
  72. }
  73. if (isset(static::$_config[$key])) {
  74. throw new Error\Exception(sprintf('Cannot reconfigure existing key "%s"', $key));
  75. }
  76. if (is_object($config)) {
  77. $config = ['className' => $config];
  78. }
  79. if (isset($config['engine']) && empty($config['className'])) {
  80. $config['className'] = $config['engine'];
  81. unset($config['engine']);
  82. }
  83. static::$_config[$key] = $config;
  84. }
  85. /**
  86. * Drops a constructed adapter.
  87. *
  88. * If you wish to modify an existing configuration, you should drop it,
  89. * change configuration and then re-add it.
  90. *
  91. * If the implementing objects supports a `$_registry` object the named configuration
  92. * will also be unloaded from the registry.
  93. *
  94. * @param string $config An existing configuation you wish to remove.
  95. * @return boolean success of the removal, returns false when the config does not exist.
  96. */
  97. public static function drop($config) {
  98. if (!isset(static::$_config[$config])) {
  99. return false;
  100. }
  101. if (isset(static::$_registry) && isset(static::$_registry->{$config})) {
  102. static::$_registry->unload($config);
  103. }
  104. unset(static::$_config[$config]);
  105. return true;
  106. }
  107. /**
  108. * Returns an array containing the named configurations
  109. *
  110. * @return array Array of configurations.
  111. */
  112. public static function configured() {
  113. return array_keys(static::$_config);
  114. }
  115. }