XBackBone/bootstrap/container.php

107 lines
4.2 KiB
PHP
Raw Normal View History

2019-11-19 11:32:58 +00:00
<?php
use App\Database\DB;
use App\Web\Lang;
use Aws\S3\S3Client;
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;
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');
switch ($config['storage']['driver']) {
case 'local':
return new Filesystem(new Local($config['storage']['path']));
case 's3':
2019-11-21 17:00:47 +00:00
$client = new S3Client([
'credentials' => [
2020-05-12 16:02:46 +00:00
'key' => $config['storage']['key'],
2019-11-19 11:32:58 +00:00
'secret' => $config['storage']['secret'],
2019-11-21 17:00:47 +00:00
],
2020-05-12 16:02:46 +00:00
'region' => $config['storage']['region'],
'endpoint' => $config['storage']['endpoint'],
2019-11-19 11:32:58 +00:00
'version' => 'latest',
2020-05-12 16:02:46 +00:00
'@http' => ['stream' => true],
2019-11-21 17:00:47 +00:00
]);
2019-11-19 11:32:58 +00:00
return new Filesystem(new AwsS3Adapter($client, $config['storage']['bucket'], $config['storage']['path']));
case 'dropbox':
$client = new DropboxClient($config['storage']['token']);
2019-11-20 17:49:31 +00:00
2019-11-21 17:00:47 +00:00
return new Filesystem(new DropboxAdapter($client), ['case_sensitive' => false]);
2019-11-19 11:32:58 +00:00
case 'ftp':
2019-11-21 17:00:47 +00:00
return new Filesystem(new FtpAdapter([
2020-05-12 16:02:46 +00:00
'host' => $config['storage']['host'],
2019-11-19 11:32:58 +00:00
'username' => $config['storage']['username'],
'password' => $config['storage']['password'],
2020-05-12 16:02:46 +00:00
'port' => $config['storage']['port'],
'root' => $config['storage']['path'],
'passive' => $config['storage']['passive'],
'ssl' => $config['storage']['ssl'],
'timeout' => 30,
2019-11-21 17:00:47 +00:00
]));
2019-11-19 11:32:58 +00:00
case 'google-cloud':
2019-11-21 17:00:47 +00:00
$client = new StorageClient([
2020-05-12 16:02:46 +00:00
'projectId' => $config['storage']['project_id'],
2019-11-19 11:32:58 +00:00
'keyFilePath' => $config['storage']['key_path'],
2019-11-21 17:00:47 +00:00
]);
2019-11-20 17:49:31 +00:00
2019-11-19 11:32:58 +00:00
return new Filesystem(new GoogleStorageAdapter($client, $client->bucket($config['storage']['bucket'])));
case 'azure':
$client = BlobRestProxy::createBlobService(
sprintf(
'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;',
$config['storage']['account_name'],
$config['storage']['account_key'])
);
return new Filesystem(new AzureBlobStorageAdapter($client, $config['storage']['container_name']));
2019-11-19 11:32:58 +00:00
default:
throw new InvalidArgumentException('The driver specified is not supported.');
}
}),
'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
];