创建适配器

@param  string $adapter
@param  array  $config

@return \One\FileSystem\Contracts\Adapter
@throws \RuntimeException
13223
protected function createAdapter(string $adapter, array $config): Adapter
    {
        if ($adapter === 'Local') {
            if (! isset($config['config']['path'])) {
                throw new RuntimeException('Filesystem local adapter must set `path`');
            }

            $config = [$config['config']['path']];
        }

        return Reflection::newInstance(
            'One\\FileSystem\\Adapters\\' . $adapter,
            $config
        );
    }
888784
public function create($adapterType, array $config)
    {
        if (!array_key_exists($adapterType, $this->adapters)) {
            throw new \InvalidArgumentException(
                sprintf('Cache Adapter\'s key %s does not exist', $adapterType)
            );
        }

        /**
         * @fixme : Active this part & clean cache config
         */
        if (!isset($config[$adapterType])) {
            throw new InvalidConfigurationException(
                sprintf('Config is not set for adapter %s', $adapterType)
            );
        }

        return new $this->adapters[$adapterType]($config[$adapterType]);
    }
588621
public function createRepositoryManager($name, array $adapterConfig, Config $globalConfig)
    {
        $factory = $this->getFactoryObject($name);

        if (!$factory instanceof RepositoryManagerFactory) {
            throw new \LogicException(sprintf('Adapter %s does not support repository-management.', $name));
        }

        return $factory->createRepositoryManager($adapterConfig, $globalConfig);
    }
693162
public function getAdapter()
    {
        if (!$this->adapter) {
            throw new Exception\RuntimeException(sprintf(
                '"%s" unable to load adapter; adapter not found',
                __METHOD__
            ));
        }
        if (method_exists($this->adapter, 'setOptions')) {
            $this->adapter->setOptions($this->getAdapterOptions());
        }

        return $this->adapter;
    }
819238
public function configureFilesystemAdapter(ContainerBuilder $container, array $config)
    {
        // add the default configuration for the local filesystem
        if ($container->hasDefinition('sonata.media.adapter.filesystem.local') && isset($config['filesystem']['local'])) {
            $container->getDefinition('sonata.media.adapter.filesystem.local')
                ->addArgument($config['filesystem']['local']['directory'])
                ->addArgument($config['filesystem']['local']['create'])
            ;
        } else {
            $container->removeDefinition('sonata.media.adapter.filesystem.local');
        }

        // add the default configuration for the FTP filesystem
        if ($container->hasDefinition('sonata.media.adapter.filesystem.ftp') && isset($config['filesystem']['ftp'])) {
            $container->getDefinition('sonata.media.adapter.filesystem.ftp')
                ->addArgument($config['filesystem']['ftp']['directory'])
                ->addArgument($config['filesystem']['ftp']['host'])
                ->addArgument([
                    'port' => $config['filesystem']['ftp']['port'],
                    'username' => $config['filesystem']['ftp']['username'],
                    'password' => $config['filesystem']['ftp']['password'],
                    'passive' => $config['filesystem']['ftp']['passive'],
                    'create' => $config['filesystem']['ftp']['create'],
                    'mode' => $config['filesystem']['ftp']['mode'],
                ])
            ;
        } else {
            $container->removeDefinition('sonata.media.adapter.filesystem.ftp');
            $container->removeDefinition('sonata.media.filesystem.ftp');
        }

        // add the default configuration for the S3 filesystem
        if ($container->hasDefinition('sonata.media.adapter.filesystem.s3') && isset($config['filesystem']['s3'])) {
            $container->getDefinition('sonata.media.adapter.filesystem.s3')
                ->replaceArgument(0, new Reference('sonata.media.adapter.service.s3'))
                ->replaceArgument(1, $config['filesystem']['s3']['bucket'])
                ->replaceArgument(2, ['create' => $config['filesystem']['s3']['create'], 'region' => $config['filesystem']['s3']['region'], 'directory' => $config['filesystem']['s3']['directory'], 'ACL' => $config['filesystem']['s3']['acl']])
            ;

            $container->getDefinition('sonata.media.metadata.amazon')
                ->addArgument([
                        'acl' => $config['filesystem']['s3']['acl'],
                        'storage' => $config['filesystem']['s3']['storage'],
                        'encryption' => $config['filesystem']['s3']['encryption'],
                        'meta' => $config['filesystem']['s3']['meta'],
                        'cache_control' => $config['filesystem']['s3']['cache_control'],
                ])
            ;

            if (3 === $config['filesystem']['s3']['sdk_version']) {
                $arguments = [
                    'region' => $config['filesystem']['s3']['region'],
                    'version' => $config['filesystem']['s3']['version'],
                ];

                if (isset($config['filesystem']['s3']['secretKey'], $config['filesystem']['s3']['accessKey'])) {
                    $arguments['credentials'] = [
                        'secret' => $config['filesystem']['s3']['secretKey'],
                        'key' => $config['filesystem']['s3']['accessKey'],
                    ];
                }

                $container->getDefinition('sonata.media.adapter.service.s3')
                    ->replaceArgument(0, $arguments)
                ;
            } else {
                $container->getDefinition('sonata.media.adapter.service.s3')
                    ->replaceArgument(0, [
                        'secret' => $config['filesystem']['s3']['secretKey'],
                        'key' => $config['filesystem']['s3']['accessKey'],
                    ])
                ;
            }
        } else {
            $container->removeDefinition('sonata.media.adapter.filesystem.s3');
            $container->removeDefinition('sonata.media.filesystem.s3');
        }

        if ($container->hasDefinition('sonata.media.adapter.filesystem.replicate') && isset($config['filesystem']['replicate'])) {
            $container->getDefinition('sonata.media.adapter.filesystem.replicate')
                ->replaceArgument(0, new Reference($config['filesystem']['replicate']['master']))
                ->replaceArgument(1, new Reference($config['filesystem']['replicate']['slave']))
            ;
        } else {
            $container->removeDefinition('sonata.media.adapter.filesystem.replicate');
            $container->removeDefinition('sonata.media.filesystem.replicate');
        }

        if ($container->hasDefinition('sonata.media.adapter.filesystem.mogilefs') && isset($config['filesystem']['mogilefs'])) {
            $container->getDefinition('sonata.media.adapter.filesystem.mogilefs')
                ->replaceArgument(0, $config['filesystem']['mogilefs']['domain'])
                ->replaceArgument(1, $config['filesystem']['mogilefs']['hosts'])
            ;
        } else {
            $container->removeDefinition('sonata.media.adapter.filesystem.mogilefs');
            $container->removeDefinition('sonata.media.filesystem.mogilefs');
        }

        if ($container->hasDefinition('sonata.media.adapter.filesystem.opencloud') &&
            (isset($config['filesystem']['openstack']) || isset($config['filesystem']['rackspace']))) {
            if (isset($config['filesystem']['openstack'])) {
                $container->setParameter('sonata.media.adapter.filesystem.opencloud.class', 'OpenCloud\OpenStack');
                $settings = 'openstack';
            } else {
                $container->setParameter('sonata.media.adapter.filesystem.opencloud.class', 'OpenCloud\Rackspace');
                $settings = 'rackspace';
            }
            $container->getDefinition('sonata.media.adapter.filesystem.opencloud.connection')
                ->replaceArgument(0, $config['filesystem'][$settings]['url'])
                ->replaceArgument(1, $config['filesystem'][$settings]['secret'])
                ;
            $container->getDefinition('sonata.media.adapter.filesystem.opencloud')
                ->replaceArgument(1, $config['filesystem'][$settings]['containerName'])
                ->replaceArgument(2, $config['filesystem'][$settings]['create_container']);
            $container->getDefinition('sonata.media.adapter.filesystem.opencloud.objectstore')
                ->replaceArgument(1, $config['filesystem'][$settings]['region']);
        } else {
            $container->removeDefinition('sonata.media.adapter.filesystem.opencloud');
            $container->removeDefinition('sonata.media.adapter.filesystem.opencloud.connection');
            $container->removeDefinition('sonata.media.adapter.filesystem.opencloud.objectstore');
            $container->removeDefinition('sonata.media.filesystem.opencloud');
        }
    }
551847
public function shareObject($config = array())
    {
        $config = new AnConfig($config);

        if (!$config->object) {
            throw new InvalidArgumentException('Not object specified to share');
        }

        $config->append(array(
             'sharer' => get_viewer(),
        ));

        if ($config->object->isOwnable()) {
            $config->append(array(
                'target' => $config->object->owner,
            ));
        }

        $request = new ComSharesSharerRequest($config);
        $sharers = new ArrayObject();
        dispatch_plugin('connect.onGetShareAdapters', array('adapters' => $sharers, 'request' => $request));

        if ($config->sharers) {
            $sharers = array_filter((array) $sharers, function ($sharer) use ($config) {
               return in_array($sharer->getIdentifier()->name, $config['sharers']);
            });
        }

        foreach ($sharers as $sharer) {
            $sharer->shareRequest($request);
        }
    }
752278
public function setAdapter($adapter)
    {
        if (!is_string($adapter) && !$adapter instanceof ConversionAlgorithmInterface) {
            throw new Exception\InvalidArgumentException(sprintf(
                '"%s" expects a string or an instance of ConversionAlgorithmInterface; received "%s"',
                __METHOD__,
                is_object($adapter) ? get_class($adapter) : gettype($adapter)
            ));
        }
        if (is_string($adapter)) {
            if (!class_exists($adapter)) {
                throw new Exception\RuntimeException(sprintf(
                    '"%s" unable to load adapter; class "%s" not found',
                    __METHOD__,
                    $adapter
                ));
            }
            $adapter = new $adapter();
            if (!$adapter instanceof ConversionAlgorithmInterface) {
                throw new Exception\InvalidArgumentException(sprintf(
                    '"%s" expects a string representing an instance of ConversionAlgorithmInterface; received "%s"',
                    __METHOD__,
                    is_object($adapter) ? get_class($adapter) : gettype($adapter)
                ));
            }
        }
        $this->adapter = $adapter;

        return $this;
    }
50562
protected function _factory($adapter) {
		$_this = static::getInstance();

		if (!isset($adapter['engine'])) {
			$adapter['engine'] = 'Gaufrette';
		}
		if ($adapter['engine'] === static::GAUFRETTE_ENGINE) {
			return $_this->gaufretteFactory($adapter);
		}
		if ($adapter['engine'] === static::FLYSYSTEM_ENGINE) {
			return $_this->flysystemFactory($adapter);
		}

		throw new RuntimeException();
	}
408087
protected function createFlysystem(AdapterInterface $adapter, array $config)
    {
        $cache = ArrayHelper::pull($config, 'cache');
        $config = ArrayHelper::only($config, ['visibility', 'disable_asserts', 'url']);
        if ($cache) {
            $adapter = new CachedAdapter($adapter, $this->createCacheStore($cache));
        }
        return new Flysystem($adapter, count($config) > 0 ? $config : null);
    }
324170
protected function _createConnection( $name, $adapter )
	{
		$host = $this->_config->get( 'resource/' . $name . '/host' );
		$port = $this->_config->get( 'resource/' . $name . '/port' );
		$user = $this->_config->get( 'resource/' . $name . '/username' );
		$pass = $this->_config->get( 'resource/' . $name . '/password' );
		$sock = $this->_config->get( 'resource/' . $name . '/socket' );
		$dbase = $this->_config->get( 'resource/' . $name . '/database' );

		$dsn = $adapter . ':dbname=' . $dbase;
		if( $sock == null )
		{
			$dsn .= isset( $host ) ? ';host=' . $host : '';
			$dsn .= isset( $port ) ? ';port=' . $port : '';
		}
		else
		{
			$dsn .= ';unix_socket=' . $sock;
		}

		$attr = array(
			PDO::ATTR_PERSISTENT => $this->_config->get( 'resource/' . $name . '/opt-persistent', false ),
		);

		$pdo = new PDO( $dsn, $user, $pass, $attr );
		$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
		$dbc = new MW_DB_Connection_PDO( $pdo );

		foreach( $this->_config->get( 'resource/' . $name . '/stmt', array() ) as $stmt ) {
			$dbc->create( $stmt )->execute()->finish();
		}

		return $dbc;
	}
460052
private function createConfigurationDefinition(array $adapter, array $configs)
    {
        $definition = new DefinitionDecorator(self::createServiceName('configuration'));

        foreach (array_merge($configs, $adapter['configs']) as $property => $value) {
            $definition->addMethodCall($this->getMethod($property), [$value]);
        }

        return $definition;
    }
603525
protected function createFlysystem(AdapterInterface $adapter, array $config)
    {
        if (class_exists('League\Flysystem\EventableFilesystem\EventableFilesystem')) {
            $fsClass = \League\Flysystem\EventableFilesystem\EventableFilesystem::class;
        } else {
            $fsClass = \League\Flysystem\Filesystem::class;
        }

        if (class_exists('League\Flysystem\Cached\CachedAdapter')) {
            $adapter = $this->decorateWithCachedAdapter($adapter, Arr::get($config, 'cache', []));
        }

        $config = Arr::only($config, ['visibility']);

        return new $fsClass($adapter, count($config) > 0 ? $config : null);
    }
398239
protected function createDisk(AdapterInterface $adapter, $config)
    {
//        $config = Arr::only($config, ['visibility', 'disable_asserts', 'url']);
        return new FileDisk($adapter, count($config) > 0 ? $config : null);
    }
787160
private function configureDatabase(array $config) {
        if (!isset($config['database']) || !isset($config['database']['adapter'])) {
            throw new InvalidArgumentException('Missing database adapter configuration for the image variations event listener', 500);
        }

        $config = $config['database'];

        if (is_callable($config['adapter'])) {
            $databaseAdapter = $config['adapter']();
        } else if (is_string($config['adapter'])) {
            $databaseAdapter = new $config['adapter'](isset($config['params']) ? $config['params'] : null);
        } else {
            $databaseAdapter = $config['adapter'];
        }

        if (!($databaseAdapter instanceof DatabaseInterface)) {
            throw new InvalidArgumentException('Invalid database adapter for the image variations event listener', 500);
        }

        $this->database = $databaseAdapter;
    }
597643
protected function getAdapter(AdapterInterface $client, array $config)
    {
        $file = array_get($config, 'file', 'flysystem.json');
        $ttl = array_get($config, 'ttl');

        return new Adapter($client, $file, $ttl);
    }
104763
public function createOssAdapter(array $config)
    {
        $root = $config['root'] ?? null;
        $oss = new OssClient($config['access_id'], $config['access_secret'], $config['endpoint'],
            $config['isCName'] ?? false,
            $config['securityToken'] ?? null,
            $config['proxy'] ?? null
        );
        $oss->setTimeout($config['timeout'] ?? 3600);
        $oss->setConnectTimeout($config['connectTimeout'] ?? 10);
        $oss->setUseSSL($config['useSSL'] ?? false);
        return $this->adapt($this->createFlysystem(
            new OssAdapter($oss, $config['bucket'], $root), $config
        ));
    }
204268
protected static function _class($config, $paths = []) {
		if (!$config['adapter']) {
			$config['adapter'] = $config['type'];
		} else {
			$paths = array_merge(["adapter.data.source.{$config['type']}"], (array) $paths);
		}
		return parent::_class($config, $paths);
	}