Вход Регистрация
Файл: gapps/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php
Строк: 352
<?php

namespace IlluminateFilesystem;

use 
Closure;
use 
AwsS3S3Client;
use 
OpenCloudRackspace;
use 
IlluminateSupportArr;
use 
InvalidArgumentException;
use 
LeagueFlysystemAdapterInterface;
use 
LeagueFlysystemFilesystemInterface;
use 
LeagueFlysystemFilesystem as Flysystem;
use 
LeagueFlysystemAdapterFtp as FtpAdapter;
use 
LeagueFlysystemRackspaceRackspaceAdapter;
use 
LeagueFlysystemAdapterLocal as LocalAdapter;
use 
LeagueFlysystemAwsS3v3AwsS3Adapter as S3Adapter;
use 
IlluminateContractsFilesystemFactory as FactoryContract;

class 
FilesystemManager implements FactoryContract
{
    
/**
     * The application instance.
     *
     * @var IlluminateContractsFoundationApplication
     */
    
protected $app;

    
/**
     * The array of resolved filesystem drivers.
     *
     * @var array
     */
    
protected $disks = [];

    
/**
     * The registered custom driver creators.
     *
     * @var array
     */
    
protected $customCreators = [];

    
/**
     * Create a new filesystem manager instance.
     *
     * @param  IlluminateContractsFoundationApplication  $app
     * @return void
     */
    
public function __construct($app)
    {
        
$this->app $app;
    }

    
/**
     * Get a filesystem instance.
     *
     * @param  string  $name
     * @return IlluminateContractsFilesystemFilesystem
     */
    
public function drive($name null)
    {
        return 
$this->disk($name);
    }

    
/**
     * Get a filesystem instance.
     *
     * @param  string  $name
     * @return IlluminateContractsFilesystemFilesystem
     */
    
public function disk($name null)
    {
        
$name $name ?: $this->getDefaultDriver();

        return 
$this->disks[$name] = $this->get($name);
    }

    
/**
     * Get a default cloud filesystem instance.
     *
     * @return IlluminateContractsFilesystemFilesystem
     */
    
public function cloud()
    {
        
$name $this->getDefaultCloudDriver();

        return 
$this->disks[$name] = $this->get($name);
    }

    
/**
     * Attempt to get the disk from the local cache.
     *
     * @param  string  $name
     * @return IlluminateContractsFilesystemFilesystem
     */
    
protected function get($name)
    {
        return isset(
$this->disks[$name]) ? $this->disks[$name] : $this->resolve($name);
    }

    
/**
     * Resolve the given disk.
     *
     * @param  string  $name
     * @return IlluminateContractsFilesystemFilesystem
     *
     * @throws InvalidArgumentException
     */
    
protected function resolve($name)
    {
        
$config $this->getConfig($name);

        if (isset(
$this->customCreators[$config['driver']])) {
            return 
$this->callCustomCreator($config);
        }

        
$driverMethod 'create'.ucfirst($config['driver']).'Driver';

        if (
method_exists($this$driverMethod)) {
            return 
$this->{$driverMethod}($config);
        } else {
            throw new 
InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
        }
    }

    
/**
     * Call a custom driver creator.
     *
     * @param  array  $config
     * @return IlluminateContractsFilesystemFilesystem
     */
    
protected function callCustomCreator(array $config)
    {
        
$driver $this->customCreators[$config['driver']]($this->app$config);

        if (
$driver instanceof FilesystemInterface) {
            return 
$this->adapt($driver);
        }

        return 
$driver;
    }

    
/**
     * Create an instance of the local driver.
     *
     * @param  array  $config
     * @return IlluminateContractsFilesystemFilesystem
     */
    
public function createLocalDriver(array $config)
    {
        
$permissions = isset($config['permissions']) ? $config['permissions'] : [];

        
$links Arr::get($config'links') === 'skip'
            
LocalAdapter::SKIP_LINKS
            
LocalAdapter::DISALLOW_LINKS;

        return 
$this->adapt($this->createFlysystem(new LocalAdapter(
            
$config['root'], LOCK_EX$links$permissions
        
), $config));
    }

    
/**
     * Create an instance of the ftp driver.
     *
     * @param  array  $config
     * @return IlluminateContractsFilesystemFilesystem
     */
    
public function createFtpDriver(array $config)
    {
        
$ftpConfig Arr::only($config, [
            
'host''username''password''port''root''passive''ssl''timeout',
        ]);

        return 
$this->adapt($this->createFlysystem(
            new 
FtpAdapter($ftpConfig), $config
        
));
    }

    
/**
     * Create an instance of the Amazon S3 driver.
     *
     * @param  array  $config
     * @return IlluminateContractsFilesystemCloud
     */
    
public function createS3Driver(array $config)
    {
        
$s3Config $this->formatS3Config($config);

        
$root = isset($s3Config['root']) ? $s3Config['root'] : null;

        
$options = isset($config['options']) ? $config['options'] : [];

        return 
$this->adapt($this->createFlysystem(
            new 
S3Adapter(new S3Client($s3Config), $s3Config['bucket'], $root$options), $config
        
));
    }

    
/**
     * Format the given S3 configuration with the default options.
     *
     * @param  array  $config
     * @return array
     */
    
protected function formatS3Config(array $config)
    {
        
$config += ['version' => 'latest'];

        if (
$config['key'] && $config['secret']) {
            
$config['credentials'] = Arr::only($config, ['key''secret']);
        }

        return 
$config;
    }

    
/**
     * Create an instance of the Rackspace driver.
     *
     * @param  array  $config
     * @return IlluminateContractsFilesystemCloud
     */
    
public function createRackspaceDriver(array $config)
    {
        
$client = new Rackspace($config['endpoint'], [
            
'username' => $config['username'], 'apiKey' => $config['key'],
        ]);

        
$root = isset($config['root']) ? $config['root'] : null;

        return 
$this->adapt($this->createFlysystem(
            new 
RackspaceAdapter($this->getRackspaceContainer($client$config), $root), $config
        
));
    }

    
/**
     * Get the Rackspace Cloud Files container.
     *
     * @param  OpenCloudRackspace  $client
     * @param  array  $config
     * @return OpenCloudObjectStoreResourceContainer
     */
    
protected function getRackspaceContainer(Rackspace $client, array $config)
    {
        
$urlType Arr::get($config'url_type');

        
$store $client->objectStoreService('cloudFiles'$config['region'], $urlType);

        return 
$store->getContainer($config['container']);
    }

    
/**
     * Create a Flysystem instance with the given adapter.
     *
     * @param  LeagueFlysystemAdapterInterface  $adapter
     * @param  array  $config
     * @return LeagueFlysystemFlysystemInterface
     */
    
protected function createFlysystem(AdapterInterface $adapter, array $config)
    {
        
$config Arr::only($config, ['visibility''disable_asserts']);

        return new 
Flysystem($adaptercount($config) > $config null);
    }

    
/**
     * Adapt the filesystem implementation.
     *
     * @param  LeagueFlysystemFilesystemInterface  $filesystem
     * @return IlluminateContractsFilesystemFilesystem
     */
    
protected function adapt(FilesystemInterface $filesystem)
    {
        return new 
FilesystemAdapter($filesystem);
    }

    
/**
     * Get the filesystem connection configuration.
     *
     * @param  string  $name
     * @return array
     */
    
protected function getConfig($name)
    {
        return 
$this->app['config']["filesystems.disks.{$name}"];
    }

    
/**
     * Get the default driver name.
     *
     * @return string
     */
    
public function getDefaultDriver()
    {
        return 
$this->app['config']['filesystems.default'];
    }

    
/**
     * Get the default cloud driver name.
     *
     * @return string
     */
    
public function getDefaultCloudDriver()
    {
        return 
$this->app['config']['filesystems.cloud'];
    }

    
/**
     * Register a custom driver creator Closure.
     *
     * @param  string    $driver
     * @param  Closure  $callback
     * @return $this
     */
    
public function extend($driverClosure $callback)
    {
        
$this->customCreators[$driver] = $callback;

        return 
$this;
    }

    
/**
     * Dynamically call the default driver instance.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return mixed
     */
    
public function __call($method$parameters)
    {
        return 
call_user_func_array([$this->disk(), $method], $parameters);
    }
}
Онлайн: 0
Реклама