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

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

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