PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php

https://gitlab.com/naufal/overtime-service-final
PHP | 330 lines | 146 code | 46 blank | 138 comment | 6 complexity | 019a8ba8e8c56ff4de592a760adb395a MD5 | raw file
  1. <?php
  2. namespace Illuminate\Filesystem;
  3. use Closure;
  4. use Aws\S3\S3Client;
  5. use OpenCloud\Rackspace;
  6. use Illuminate\Support\Arr;
  7. use InvalidArgumentException;
  8. use League\Flysystem\AdapterInterface;
  9. use League\Flysystem\FilesystemInterface;
  10. use League\Flysystem\Filesystem as Flysystem;
  11. use League\Flysystem\Adapter\Ftp as FtpAdapter;
  12. use League\Flysystem\Rackspace\RackspaceAdapter;
  13. use League\Flysystem\Adapter\Local as LocalAdapter;
  14. use League\Flysystem\AwsS3v3\AwsS3Adapter as S3Adapter;
  15. use Illuminate\Contracts\Filesystem\Factory as FactoryContract;
  16. class FilesystemManager implements FactoryContract
  17. {
  18. /**
  19. * The application instance.
  20. *
  21. * @var \Illuminate\Contracts\Foundation\Application
  22. */
  23. protected $app;
  24. /**
  25. * The array of resolved filesystem drivers.
  26. *
  27. * @var array
  28. */
  29. protected $disks = [];
  30. /**
  31. * The registered custom driver creators.
  32. *
  33. * @var array
  34. */
  35. protected $customCreators = [];
  36. /**
  37. * Create a new filesystem manager instance.
  38. *
  39. * @param \Illuminate\Contracts\Foundation\Application $app
  40. * @return void
  41. */
  42. public function __construct($app)
  43. {
  44. $this->app = $app;
  45. }
  46. /**
  47. * Get a filesystem instance.
  48. *
  49. * @param string $name
  50. * @return \Illuminate\Contracts\Filesystem\Filesystem
  51. */
  52. public function drive($name = null)
  53. {
  54. return $this->disk($name);
  55. }
  56. /**
  57. * Get a filesystem instance.
  58. *
  59. * @param string $name
  60. * @return \Illuminate\Contracts\Filesystem\Filesystem
  61. */
  62. public function disk($name = null)
  63. {
  64. $name = $name ?: $this->getDefaultDriver();
  65. return $this->disks[$name] = $this->get($name);
  66. }
  67. /**
  68. * Get a default cloud filesystem instance.
  69. *
  70. * @return \Illuminate\Contracts\Filesystem\Filesystem
  71. */
  72. public function cloud()
  73. {
  74. $name = $this->getDefaultCloudDriver();
  75. return $this->disks[$name] = $this->get($name);
  76. }
  77. /**
  78. * Attempt to get the disk from the local cache.
  79. *
  80. * @param string $name
  81. * @return \Illuminate\Contracts\Filesystem\Filesystem
  82. */
  83. protected function get($name)
  84. {
  85. return isset($this->disks[$name]) ? $this->disks[$name] : $this->resolve($name);
  86. }
  87. /**
  88. * Resolve the given disk.
  89. *
  90. * @param string $name
  91. * @return \Illuminate\Contracts\Filesystem\Filesystem
  92. *
  93. * @throws \InvalidArgumentException
  94. */
  95. protected function resolve($name)
  96. {
  97. $config = $this->getConfig($name);
  98. if (isset($this->customCreators[$config['driver']])) {
  99. return $this->callCustomCreator($config);
  100. }
  101. $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
  102. if (method_exists($this, $driverMethod)) {
  103. return $this->{$driverMethod}($config);
  104. } else {
  105. throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
  106. }
  107. }
  108. /**
  109. * Call a custom driver creator.
  110. *
  111. * @param array $config
  112. * @return \Illuminate\Contracts\Filesystem\Filesystem
  113. */
  114. protected function callCustomCreator(array $config)
  115. {
  116. $driver = $this->customCreators[$config['driver']]($this->app, $config);
  117. if ($driver instanceof FilesystemInterface) {
  118. return $this->adapt($driver);
  119. }
  120. return $driver;
  121. }
  122. /**
  123. * Create an instance of the local driver.
  124. *
  125. * @param array $config
  126. * @return \Illuminate\Contracts\Filesystem\Filesystem
  127. */
  128. public function createLocalDriver(array $config)
  129. {
  130. $permissions = isset($config['permissions']) ? $config['permissions'] : [];
  131. $links = Arr::get($config, 'links') === 'skip'
  132. ? LocalAdapter::SKIP_LINKS
  133. : LocalAdapter::DISALLOW_LINKS;
  134. return $this->adapt($this->createFlysystem(new LocalAdapter(
  135. $config['root'], LOCK_EX, $links, $permissions
  136. ), $config));
  137. }
  138. /**
  139. * Create an instance of the ftp driver.
  140. *
  141. * @param array $config
  142. * @return \Illuminate\Contracts\Filesystem\Filesystem
  143. */
  144. public function createFtpDriver(array $config)
  145. {
  146. $ftpConfig = Arr::only($config, [
  147. 'host', 'username', 'password', 'port', 'root', 'passive', 'ssl', 'timeout',
  148. ]);
  149. return $this->adapt($this->createFlysystem(
  150. new FtpAdapter($ftpConfig), $config
  151. ));
  152. }
  153. /**
  154. * Create an instance of the Amazon S3 driver.
  155. *
  156. * @param array $config
  157. * @return \Illuminate\Contracts\Filesystem\Cloud
  158. */
  159. public function createS3Driver(array $config)
  160. {
  161. $s3Config = $this->formatS3Config($config);
  162. $root = isset($s3Config['root']) ? $s3Config['root'] : null;
  163. $options = isset($config['options']) ? $config['options'] : [];
  164. return $this->adapt($this->createFlysystem(
  165. new S3Adapter(new S3Client($s3Config), $s3Config['bucket'], $root, $options), $config
  166. ));
  167. }
  168. /**
  169. * Format the given S3 configuration with the default options.
  170. *
  171. * @param array $config
  172. * @return array
  173. */
  174. protected function formatS3Config(array $config)
  175. {
  176. $config += ['version' => 'latest'];
  177. if ($config['key'] && $config['secret']) {
  178. $config['credentials'] = Arr::only($config, ['key', 'secret']);
  179. }
  180. return $config;
  181. }
  182. /**
  183. * Create an instance of the Rackspace driver.
  184. *
  185. * @param array $config
  186. * @return \Illuminate\Contracts\Filesystem\Cloud
  187. */
  188. public function createRackspaceDriver(array $config)
  189. {
  190. $client = new Rackspace($config['endpoint'], [
  191. 'username' => $config['username'], 'apiKey' => $config['key'],
  192. ]);
  193. $root = isset($config['root']) ? $config['root'] : null;
  194. return $this->adapt($this->createFlysystem(
  195. new RackspaceAdapter($this->getRackspaceContainer($client, $config), $root), $config
  196. ));
  197. }
  198. /**
  199. * Get the Rackspace Cloud Files container.
  200. *
  201. * @param \OpenCloud\Rackspace $client
  202. * @param array $config
  203. * @return \OpenCloud\ObjectStore\Resource\Container
  204. */
  205. protected function getRackspaceContainer(Rackspace $client, array $config)
  206. {
  207. $urlType = Arr::get($config, 'url_type');
  208. $store = $client->objectStoreService('cloudFiles', $config['region'], $urlType);
  209. return $store->getContainer($config['container']);
  210. }
  211. /**
  212. * Create a Flysystem instance with the given adapter.
  213. *
  214. * @param \League\Flysystem\AdapterInterface $adapter
  215. * @param array $config
  216. * @return \League\Flysystem\FlysystemInterface
  217. */
  218. protected function createFlysystem(AdapterInterface $adapter, array $config)
  219. {
  220. $config = Arr::only($config, ['visibility']);
  221. return new Flysystem($adapter, count($config) > 0 ? $config : null);
  222. }
  223. /**
  224. * Adapt the filesystem implementation.
  225. *
  226. * @param \League\Flysystem\FilesystemInterface $filesystem
  227. * @return \Illuminate\Contracts\Filesystem\Filesystem
  228. */
  229. protected function adapt(FilesystemInterface $filesystem)
  230. {
  231. return new FilesystemAdapter($filesystem);
  232. }
  233. /**
  234. * Get the filesystem connection configuration.
  235. *
  236. * @param string $name
  237. * @return array
  238. */
  239. protected function getConfig($name)
  240. {
  241. return $this->app['config']["filesystems.disks.{$name}"];
  242. }
  243. /**
  244. * Get the default driver name.
  245. *
  246. * @return string
  247. */
  248. public function getDefaultDriver()
  249. {
  250. return $this->app['config']['filesystems.default'];
  251. }
  252. /**
  253. * Get the default cloud driver name.
  254. *
  255. * @return string
  256. */
  257. public function getDefaultCloudDriver()
  258. {
  259. return $this->app['config']['filesystems.cloud'];
  260. }
  261. /**
  262. * Register a custom driver creator Closure.
  263. *
  264. * @param string $driver
  265. * @param \Closure $callback
  266. * @return $this
  267. */
  268. public function extend($driver, Closure $callback)
  269. {
  270. $this->customCreators[$driver] = $callback;
  271. return $this;
  272. }
  273. /**
  274. * Dynamically call the default driver instance.
  275. *
  276. * @param string $method
  277. * @param array $parameters
  278. * @return mixed
  279. */
  280. public function __call($method, $parameters)
  281. {
  282. return call_user_func_array([$this->disk(), $method], $parameters);
  283. }
  284. }