PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 304 lines | 135 code | 41 blank | 128 comment | 6 complexity | e2ecd227b1ef8a7377ad80a7005b831d 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. * Attempt to get the disk from the local cache.
  69. *
  70. * @param string $name
  71. * @return \Illuminate\Contracts\Filesystem\Filesystem
  72. */
  73. protected function get($name)
  74. {
  75. return isset($this->disks[$name]) ? $this->disks[$name] : $this->resolve($name);
  76. }
  77. /**
  78. * Resolve the given disk.
  79. *
  80. * @param string $name
  81. * @return \Illuminate\Contracts\Filesystem\Filesystem
  82. *
  83. * @throws \InvalidArgumentException
  84. */
  85. protected function resolve($name)
  86. {
  87. $config = $this->getConfig($name);
  88. if (isset($this->customCreators[$config['driver']])) {
  89. return $this->callCustomCreator($config);
  90. }
  91. $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
  92. if (method_exists($this, $driverMethod)) {
  93. return $this->{$driverMethod}($config);
  94. } else {
  95. throw new InvalidArgumentException("Driver [{$config['driver']}] not supported.");
  96. }
  97. }
  98. /**
  99. * Call a custom driver creator.
  100. *
  101. * @param array $config
  102. * @return \Illuminate\Contracts\Filesystem\Filesystem
  103. */
  104. protected function callCustomCreator(array $config)
  105. {
  106. $driver = $this->customCreators[$config['driver']]($this->app, $config);
  107. if ($driver instanceof FilesystemInterface) {
  108. return $this->adapt($driver);
  109. }
  110. return $driver;
  111. }
  112. /**
  113. * Create an instance of the local driver.
  114. *
  115. * @param array $config
  116. * @return \Illuminate\Contracts\Filesystem\Filesystem
  117. */
  118. public function createLocalDriver(array $config)
  119. {
  120. $permissions = isset($config['permissions']) ? $config['permissions'] : [];
  121. $links = Arr::get($config, 'links') === 'skip'
  122. ? LocalAdapter::SKIP_LINKS
  123. : LocalAdapter::DISALLOW_LINKS;
  124. return $this->adapt($this->createFlysystem(new LocalAdapter(
  125. $config['root'], LOCK_EX, $links, $permissions
  126. ), $config));
  127. }
  128. /**
  129. * Create an instance of the ftp driver.
  130. *
  131. * @param array $config
  132. * @return \Illuminate\Contracts\Filesystem\Filesystem
  133. */
  134. public function createFtpDriver(array $config)
  135. {
  136. $ftpConfig = Arr::only($config, [
  137. 'host', 'username', 'password', 'port', 'root', 'passive', 'ssl', 'timeout',
  138. ]);
  139. return $this->adapt($this->createFlysystem(
  140. new FtpAdapter($ftpConfig), $config
  141. ));
  142. }
  143. /**
  144. * Create an instance of the Amazon S3 driver.
  145. *
  146. * @param array $config
  147. * @return \Illuminate\Contracts\Filesystem\Cloud
  148. */
  149. public function createS3Driver(array $config)
  150. {
  151. $s3Config = $this->formatS3Config($config);
  152. $root = isset($s3Config['root']) ? $s3Config['root'] : null;
  153. return $this->adapt($this->createFlysystem(
  154. new S3Adapter(new S3Client($s3Config), $s3Config['bucket'], $root), $config
  155. ));
  156. }
  157. /**
  158. * Format the given S3 configuration with the default options.
  159. *
  160. * @param array $config
  161. * @return array
  162. */
  163. protected function formatS3Config(array $config)
  164. {
  165. $config += ['version' => 'latest'];
  166. if ($config['key'] && $config['secret']) {
  167. $config['credentials'] = Arr::only($config, ['key', 'secret']);
  168. }
  169. return $config;
  170. }
  171. /**
  172. * Create an instance of the Rackspace driver.
  173. *
  174. * @param array $config
  175. * @return \Illuminate\Contracts\Filesystem\Cloud
  176. */
  177. public function createRackspaceDriver(array $config)
  178. {
  179. $client = new Rackspace($config['endpoint'], [
  180. 'username' => $config['username'], 'apiKey' => $config['key'],
  181. ]);
  182. return $this->adapt($this->createFlysystem(
  183. new RackspaceAdapter($this->getRackspaceContainer($client, $config)), $config
  184. ));
  185. }
  186. /**
  187. * Get the Rackspace Cloud Files container.
  188. *
  189. * @param \OpenCloud\Rackspace $client
  190. * @param array $config
  191. * @return \OpenCloud\ObjectStore\Resource\Container
  192. */
  193. protected function getRackspaceContainer(Rackspace $client, array $config)
  194. {
  195. $urlType = Arr::get($config, 'url_type');
  196. $store = $client->objectStoreService('cloudFiles', $config['region'], $urlType);
  197. return $store->getContainer($config['container']);
  198. }
  199. /**
  200. * Create a Flysystem instance with the given adapter.
  201. *
  202. * @param \League\Flysystem\AdapterInterface $adapter
  203. * @param array $config
  204. * @return \League\Flysystem\FlysystemInterface
  205. */
  206. protected function createFlysystem(AdapterInterface $adapter, array $config)
  207. {
  208. $config = Arr::only($config, ['visibility']);
  209. return new Flysystem($adapter, count($config) > 0 ? $config : null);
  210. }
  211. /**
  212. * Adapt the filesystem implementation.
  213. *
  214. * @param \League\Flysystem\FilesystemInterface $filesystem
  215. * @return \Illuminate\Contracts\Filesystem\Filesystem
  216. */
  217. protected function adapt(FilesystemInterface $filesystem)
  218. {
  219. return new FilesystemAdapter($filesystem);
  220. }
  221. /**
  222. * Get the filesystem connection configuration.
  223. *
  224. * @param string $name
  225. * @return array
  226. */
  227. protected function getConfig($name)
  228. {
  229. return $this->app['config']["filesystems.disks.{$name}"];
  230. }
  231. /**
  232. * Get the default driver name.
  233. *
  234. * @return string
  235. */
  236. public function getDefaultDriver()
  237. {
  238. return $this->app['config']['filesystems.default'];
  239. }
  240. /**
  241. * Register a custom driver creator Closure.
  242. *
  243. * @param string $driver
  244. * @param \Closure $callback
  245. * @return $this
  246. */
  247. public function extend($driver, Closure $callback)
  248. {
  249. $this->customCreators[$driver] = $callback;
  250. return $this;
  251. }
  252. /**
  253. * Dynamically call the default driver instance.
  254. *
  255. * @param string $method
  256. * @param array $parameters
  257. * @return mixed
  258. */
  259. public function __call($method, $parameters)
  260. {
  261. return call_user_func_array([$this->disk(), $method], $parameters);
  262. }
  263. }