PageRenderTime 100ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php

https://gitlab.com/dzakiafif/cokelatklasik
PHP | 311 lines | 126 code | 39 blank | 146 comment | 4 complexity | 32e8e776e6ac8df1a3bc7bab341d8aaa MD5 | raw file
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Closure;
  4. use InvalidArgumentException;
  5. use Illuminate\Contracts\Cache\Store;
  6. use Illuminate\Contracts\Cache\Factory as FactoryContract;
  7. class CacheManager implements FactoryContract
  8. {
  9. /**
  10. * The application instance.
  11. *
  12. * @var \Illuminate\Foundation\Application
  13. */
  14. protected $app;
  15. /**
  16. * The array of resolved cache stores.
  17. *
  18. * @var array
  19. */
  20. protected $stores = [];
  21. /**
  22. * The registered custom driver creators.
  23. *
  24. * @var array
  25. */
  26. protected $customCreators = [];
  27. /**
  28. * Create a new Cache manager instance.
  29. *
  30. * @param \Illuminate\Foundation\Application $app
  31. * @return void
  32. */
  33. public function __construct($app)
  34. {
  35. $this->app = $app;
  36. }
  37. /**
  38. * Get a cache store instance by name.
  39. *
  40. * @param string|null $name
  41. * @return mixed
  42. */
  43. public function store($name = null)
  44. {
  45. $name = $name ?: $this->getDefaultDriver();
  46. return $this->stores[$name] = $this->get($name);
  47. }
  48. /**
  49. * Get a cache driver instance.
  50. *
  51. * @param string $driver
  52. * @return mixed
  53. */
  54. public function driver($driver = null)
  55. {
  56. return $this->store($driver);
  57. }
  58. /**
  59. * Attempt to get the store from the local cache.
  60. *
  61. * @param string $name
  62. * @return \Illuminate\Contracts\Cache\Repository
  63. */
  64. protected function get($name)
  65. {
  66. return isset($this->stores[$name]) ? $this->stores[$name] : $this->resolve($name);
  67. }
  68. /**
  69. * Resolve the given store.
  70. *
  71. * @param string $name
  72. * @return \Illuminate\Contracts\Cache\Repository
  73. */
  74. protected function resolve($name)
  75. {
  76. $config = $this->getConfig($name);
  77. if (is_null($config)) {
  78. throw new InvalidArgumentException("Cache store [{$name}] is not defined.");
  79. }
  80. if (isset($this->customCreators[$config['driver']])) {
  81. return $this->callCustomCreator($config);
  82. } else {
  83. return $this->{'create'.ucfirst($config['driver']).'Driver'}($config);
  84. }
  85. }
  86. /**
  87. * Call a custom driver creator.
  88. *
  89. * @param array $config
  90. * @return mixed
  91. */
  92. protected function callCustomCreator(array $config)
  93. {
  94. return $this->customCreators[$config['driver']]($this->app, $config);
  95. }
  96. /**
  97. * Create an instance of the APC cache driver.
  98. *
  99. * @param array $config
  100. * @return \Illuminate\Cache\ApcStore
  101. */
  102. protected function createApcDriver(array $config)
  103. {
  104. $prefix = $this->getPrefix($config);
  105. return $this->repository(new ApcStore(new ApcWrapper, $prefix));
  106. }
  107. /**
  108. * Create an instance of the array cache driver.
  109. *
  110. * @return \Illuminate\Cache\ArrayStore
  111. */
  112. protected function createArrayDriver()
  113. {
  114. return $this->repository(new ArrayStore);
  115. }
  116. /**
  117. * Create an instance of the file cache driver.
  118. *
  119. * @param array $config
  120. * @return \Illuminate\Cache\FileStore
  121. */
  122. protected function createFileDriver(array $config)
  123. {
  124. return $this->repository(new FileStore($this->app['files'], $config['path']));
  125. }
  126. /**
  127. * Create an instance of the Memcached cache driver.
  128. *
  129. * @param array $config
  130. * @return \Illuminate\Cache\MemcachedStore
  131. */
  132. protected function createMemcachedDriver(array $config)
  133. {
  134. $prefix = $this->getPrefix($config);
  135. $memcached = $this->app['memcached.connector']->connect($config['servers']);
  136. return $this->repository(new MemcachedStore($memcached, $prefix));
  137. }
  138. /**
  139. * Create an instance of the Null cache driver.
  140. *
  141. * @return \Illuminate\Cache\NullStore
  142. */
  143. protected function createNullDriver()
  144. {
  145. return $this->repository(new NullStore);
  146. }
  147. /**
  148. * Create an instance of the WinCache cache driver.
  149. *
  150. * @param array $config
  151. * @return \Illuminate\Cache\WinCacheStore
  152. */
  153. protected function createWincacheDriver(array $config)
  154. {
  155. return $this->repository(new WinCacheStore($this->getPrefix($config)));
  156. }
  157. /**
  158. * Create an instance of the XCache cache driver.
  159. *
  160. * @param array $config
  161. * @return \Illuminate\Cache\WinCacheStore
  162. */
  163. protected function createXcacheDriver(array $config)
  164. {
  165. return $this->repository(new XCacheStore($this->getPrefix($config)));
  166. }
  167. /**
  168. * Create an instance of the Redis cache driver.
  169. *
  170. * @param array $config
  171. * @return \Illuminate\Cache\RedisStore
  172. */
  173. protected function createRedisDriver(array $config)
  174. {
  175. $redis = $this->app['redis'];
  176. $connection = array_get($config, 'connection', 'default') ?: 'default';
  177. return $this->repository(new RedisStore($redis, $this->getPrefix($config), $connection));
  178. }
  179. /**
  180. * Create an instance of the database cache driver.
  181. *
  182. * @param array $config
  183. * @return \Illuminate\Cache\DatabaseStore
  184. */
  185. protected function createDatabaseDriver(array $config)
  186. {
  187. $connection = $this->app['db']->connection(array_get($config, 'connection'));
  188. return $this->repository(
  189. new DatabaseStore(
  190. $connection, $this->app['encrypter'], $config['table'], $this->getPrefix($config)
  191. )
  192. );
  193. }
  194. /**
  195. * Create a new cache repository with the given implementation.
  196. *
  197. * @param \Illuminate\Contracts\Cache\Store $store
  198. * @return \Illuminate\Cache\Repository
  199. */
  200. public function repository(Store $store)
  201. {
  202. $repository = new Repository($store);
  203. if ($this->app->bound('Illuminate\Contracts\Events\Dispatcher')) {
  204. $repository->setEventDispatcher(
  205. $this->app['Illuminate\Contracts\Events\Dispatcher']
  206. );
  207. }
  208. return $repository;
  209. }
  210. /**
  211. * Get the cache prefix.
  212. *
  213. * @param array $config
  214. * @return string
  215. */
  216. protected function getPrefix(array $config)
  217. {
  218. return array_get($config, 'prefix') ?: $this->app['config']['cache.prefix'];
  219. }
  220. /**
  221. * Get the cache connection configuration.
  222. *
  223. * @param string $name
  224. * @return array
  225. */
  226. protected function getConfig($name)
  227. {
  228. return $this->app['config']["cache.stores.{$name}"];
  229. }
  230. /**
  231. * Get the default cache driver name.
  232. *
  233. * @return string
  234. */
  235. public function getDefaultDriver()
  236. {
  237. return $this->app['config']['cache.default'];
  238. }
  239. /**
  240. * Set the default cache driver name.
  241. *
  242. * @param string $name
  243. * @return void
  244. */
  245. public function setDefaultDriver($name)
  246. {
  247. $this->app['config']['cache.default'] = $name;
  248. }
  249. /**
  250. * Register a custom driver creator Closure.
  251. *
  252. * @param string $driver
  253. * @param \Closure $callback
  254. * @return $this
  255. */
  256. public function extend($driver, Closure $callback)
  257. {
  258. $this->customCreators[$driver] = $callback;
  259. return $this;
  260. }
  261. /**
  262. * Dynamically call the default driver instance.
  263. *
  264. * @param string $method
  265. * @param array $parameters
  266. * @return mixed
  267. */
  268. public function __call($method, $parameters)
  269. {
  270. return call_user_func_array([$this->store(), $method], $parameters);
  271. }
  272. }