2019-11-19 11:32:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use App\Database\DB;
|
|
|
|
use App\Web\Lang;
|
|
|
|
use Aws\S3\S3Client;
|
2022-02-08 21:24:16 +00:00
|
|
|
use League\Flysystem\Cached\CachedAdapter;
|
|
|
|
use League\Flysystem\Cached\Storage\Adapter;
|
2019-11-20 17:49:31 +00:00
|
|
|
use function DI\factory;
|
|
|
|
use function DI\get;
|
2019-11-19 11:32:58 +00:00
|
|
|
use Google\Cloud\Storage\StorageClient;
|
|
|
|
use League\Flysystem\Adapter\Ftp as FtpAdapter;
|
|
|
|
use League\Flysystem\Adapter\Local;
|
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter;
|
2020-05-19 14:55:30 +00:00
|
|
|
use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter;
|
|
|
|
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
|
2019-11-19 11:32:58 +00:00
|
|
|
use League\Flysystem\Filesystem;
|
|
|
|
use Monolog\Formatter\LineFormatter;
|
|
|
|
use Monolog\Handler\RotatingFileHandler;
|
|
|
|
use Monolog\Logger;
|
|
|
|
use Psr\Container\ContainerInterface as Container;
|
|
|
|
use Spatie\Dropbox\Client as DropboxClient;
|
|
|
|
use Spatie\FlysystemDropbox\DropboxAdapter;
|
|
|
|
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;
|
|
|
|
|
2019-11-21 17:00:47 +00:00
|
|
|
return [
|
2019-11-19 11:32:58 +00:00
|
|
|
Logger::class => factory(function () {
|
|
|
|
$logger = new Logger('app');
|
|
|
|
|
|
|
|
$streamHandler = new RotatingFileHandler(BASE_DIR.'logs/log.txt', 10, Logger::DEBUG);
|
|
|
|
|
2019-11-20 17:49:31 +00:00
|
|
|
$lineFormatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s');
|
2019-11-19 11:32:58 +00:00
|
|
|
$lineFormatter->includeStacktraces(true);
|
|
|
|
|
|
|
|
$streamHandler->setFormatter($lineFormatter);
|
|
|
|
|
|
|
|
$logger->pushHandler($streamHandler);
|
|
|
|
|
|
|
|
return $logger;
|
|
|
|
}),
|
|
|
|
'logger' => get(Logger::class),
|
|
|
|
|
|
|
|
DB::class => factory(function (Container $container) {
|
|
|
|
$config = $container->get('config');
|
2019-11-20 17:49:31 +00:00
|
|
|
|
2019-11-19 11:32:58 +00:00
|
|
|
return new DB(dsnFromConfig($config), $config['db']['username'], $config['db']['password']);
|
|
|
|
}),
|
|
|
|
'database' => get(DB::class),
|
|
|
|
|
|
|
|
Filesystem::class => factory(function (Container $container) {
|
|
|
|
$config = $container->get('config');
|
2022-02-08 21:24:16 +00:00
|
|
|
$driver = $config['storage']['driver'];
|
|
|
|
if ($driver === 'local') {
|
|
|
|
return new Filesystem(new Local($config['storage']['path']));
|
|
|
|
} elseif ($driver === 's3') {
|
|
|
|
$client = new S3Client([
|
|
|
|
'credentials' => [
|
|
|
|
'key' => $config['storage']['key'],
|
|
|
|
'secret' => $config['storage']['secret'],
|
|
|
|
],
|
|
|
|
'region' => $config['storage']['region'],
|
|
|
|
'endpoint' => $config['storage']['endpoint'],
|
|
|
|
'version' => 'latest',
|
|
|
|
'use_path_style_endpoint' => $config['storage']['use_path_style_endpoint'] ?? false,
|
|
|
|
'@http' => ['stream' => true],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$adapter = new AwsS3Adapter($client, $config['storage']['bucket'], $config['storage']['path']);
|
|
|
|
} elseif ($driver === 'dropbox') {
|
|
|
|
$client = new DropboxClient($config['storage']['token']);
|
|
|
|
|
|
|
|
$adapter = new DropboxAdapter($client);
|
|
|
|
} elseif ($driver === 'ftp') {
|
|
|
|
$adapter = new FtpAdapter([
|
|
|
|
'host' => $config['storage']['host'],
|
|
|
|
'username' => $config['storage']['username'],
|
|
|
|
'password' => $config['storage']['password'],
|
|
|
|
'port' => $config['storage']['port'],
|
|
|
|
'root' => $config['storage']['path'],
|
|
|
|
'passive' => $config['storage']['passive'],
|
|
|
|
'ssl' => $config['storage']['ssl'],
|
|
|
|
'timeout' => 30,
|
|
|
|
]);
|
|
|
|
} elseif ($driver === 'google-cloud') {
|
|
|
|
$client = new StorageClient([
|
|
|
|
'projectId' => $config['storage']['project_id'],
|
|
|
|
'keyFilePath' => $config['storage']['key_path'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$adapter = new GoogleStorageAdapter($client, $client->bucket($config['storage']['bucket']));
|
|
|
|
} elseif ($driver === 'azure') {
|
|
|
|
$client = BlobRestProxy::createBlobService(
|
|
|
|
sprintf(
|
|
|
|
'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;',
|
|
|
|
$config['storage']['account_name'],
|
|
|
|
$config['storage']['account_key']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$adapter = new AzureBlobStorageAdapter($client, $config['storage']['container_name']);
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException('The driver specified is not supported.');
|
2019-11-19 11:32:58 +00:00
|
|
|
}
|
2022-02-08 21:24:16 +00:00
|
|
|
|
|
|
|
$cache = new Adapter(new Local(BASE_DIR.'resources/cache/fs'), 'file', 300); // 5min
|
|
|
|
return new Filesystem(new CachedAdapter($adapter, $cache));
|
2019-11-19 11:32:58 +00:00
|
|
|
}),
|
|
|
|
'storage' => get(Filesystem::class),
|
|
|
|
|
2019-11-19 12:59:17 +00:00
|
|
|
Lang::class => factory(function () {
|
2019-11-19 11:32:58 +00:00
|
|
|
return Lang::build(Lang::recognize(), BASE_DIR.'resources/lang/');
|
|
|
|
}),
|
|
|
|
'lang' => get(Lang::class),
|
2019-11-21 17:00:47 +00:00
|
|
|
];
|