StaticConfigTrait.php 3.6 KB

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