CacheRegistry.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Cache;
  16. use BadMethodCallException;
  17. use Cake\Core\App;
  18. use Cake\Core\ObjectRegistry;
  19. use RuntimeException;
  20. /**
  21. * An object registry for cache engines.
  22. *
  23. * Used by Cake\Cache\Cache to load and manage cache engines.
  24. */
  25. class CacheRegistry extends ObjectRegistry {
  26. /**
  27. * Resolve a cache engine classname.
  28. *
  29. * Part of the template method for Cake\Core\ObjectRegistry::load()
  30. *
  31. * @param string $class Partial classname to resolve.
  32. * @return string|false Either the correct classname or false.
  33. */
  34. protected function _resolveClassName($class) {
  35. if (is_object($class)) {
  36. return $class;
  37. }
  38. return App::className($class, 'Cache/Engine', 'Engine');
  39. }
  40. /**
  41. * Throws an exception when a cache engine is missing.
  42. *
  43. * Part of the template method for Cake\Core\ObjectRegistry::load()
  44. *
  45. * @param string $class The classname that is missing.
  46. * @param string $plugin The plugin the cache is missing in.
  47. * @return void
  48. * @throws \BadMethodCallException
  49. */
  50. protected function _throwMissingClassError($class, $plugin) {
  51. throw new BadMethodCallException(sprintf('Cache engine %s is not available.', $class));
  52. }
  53. /**
  54. * Create the cache engine instance.
  55. *
  56. * Part of the template method for Cake\Core\ObjectRegistry::load()
  57. *
  58. * @param string|CacheEngine $class The classname or object to make.
  59. * @param string $alias The alias of the object.
  60. * @param array $config An array of settings to use for the cache engine.
  61. * @return CacheEngine The constructed CacheEngine class.
  62. * @throws \RuntimeException when an object doesn't implement the correct interface.
  63. */
  64. protected function _create($class, $alias, $config) {
  65. if (is_object($class)) {
  66. $instance = $class;
  67. }
  68. unset($config['className']);
  69. if (!isset($instance)) {
  70. $instance = new $class($config);
  71. }
  72. if (!($instance instanceof CacheEngine)) {
  73. throw new RuntimeException(
  74. 'Cache engines must use Cake\Cache\CacheEngine as a base class.'
  75. );
  76. }
  77. if (!$instance->init($config)) {
  78. throw new RuntimeException(
  79. sprintf('Cache engine %s is not properly configured.', get_class($instance))
  80. );
  81. }
  82. $config = $instance->config();
  83. if ($config['probability'] && time() % $config['probability'] === 0) {
  84. $instance->gc();
  85. }
  86. return $instance;
  87. }
  88. /**
  89. * Remove a single adapter from the registry.
  90. *
  91. * @param string $name The adapter name.
  92. * @return void
  93. */
  94. public function unload($name) {
  95. unset($this->_loaded[$name]);
  96. }
  97. }