Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Move StorageProviders to own classes #410

Merged
merged 7 commits into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions external/import.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use XHGui\Saver\SaverInterface;
use XHGui\ServiceContainer;
use XHGui\Application;

require __DIR__ . '/../vendor/autoload.php';

Expand All @@ -21,9 +20,8 @@
throw new RuntimeException('Can\'t open ' . $file);
}

$container = ServiceContainer::instance();
/** @var SaverInterface $saver */
$saver = $container['saver'];
$app = new Application();
$saver = $app->getSaver();

while (!feof($fp)) {
$line = fgets($fp);
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="./tests/bootstrap.php">
<phpunit colors="true" bootstrap="./vendor/autoload.php">
<php>
<server name="SYMFONY_PHPUNIT_VERSION" value="6.5" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
Expand Down
48 changes: 48 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace XHGui;

use Pimple\Container;
use Slim\Slim as App;
use XHGui\Saver\SaverInterface;

class Application extends Container
{
/** @var bool */
private $booted = false;

public function __construct()
{
parent::__construct();
$this->register(new ServiceProvider\ServiceProvider());
$this->register(new ServiceProvider\ConfigProvider());
$this->register(new ServiceProvider\PdoStorageProvider());
$this->register(new ServiceProvider\MongoStorageProvider());
$this->register(new ServiceProvider\SlimProvider());
}

public function run(): void
{
$this->boot()->getSlim()->run();
}

public function boot(): self
{
if (!$this->booted) {
$this->register(new ServiceProvider\RouteProvider());
$this->booted = true;
}

return $this;
}

public function getSlim(): App
{
return $this['app'];
}

public function getSaver(): SaverInterface
{
return $this['saver'];
}
}
249 changes: 0 additions & 249 deletions src/ServiceContainer.php

This file was deleted.

64 changes: 64 additions & 0 deletions src/ServiceProvider/MongoStorageProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace XHGui\ServiceProvider;

use MongoClient;
use MongoCollection;
use MongoDB;
use MongoDB\Driver\Manager;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use RuntimeException;
use XHGui\Saver\MongoSaver;
use XHGui\Searcher\MongoSearcher;

class MongoStorageProvider implements ServiceProviderInterface
{
public function register(Container $app): void
{
// NOTE: db.host, db.options, db.driverOptions, db.db are @deprecated and will be removed in the future
$app['mongodb.database'] = static function ($app) {
$config = $app['config'];
$mongodb = $config['mongodb'] ?? [];

return $config['db.db'] ?? $mongodb['database'] ?? 'xhgui';
};

$app[MongoDB::class] = static function ($app) {
$database = $app['mongodb.database'];
/** @var MongoClient $client */
$client = $app[MongoClient::class];
$mongoDB = $client->selectDb($database);
$mongoDB->results->findOne();

return $mongoDB;
};

$app[MongoClient::class] = static function ($app) {
if (!class_exists(Manager::class)) {
throw new RuntimeException('Required extension ext-mongodb missing');
}

$config = $app['config'];
$mongodb = $config['mongodb'] ?? [];
$options = $config['db.options'] ?? $mongodb['options'] ?? [];
$driverOptions = $config['db.driverOptions'] ?? $mongodb['driverOptions'] ?? [];
$server = $config['db.host'] ?? sprintf('mongodb://%s:%s', $mongodb['hostname'], $mongodb['port']);

return new MongoClient($server, $options, $driverOptions);
};

$app['searcher.mongodb'] = static function ($app) {
return new MongoSearcher($app[MongoDB::class]);
};

$app['saver.mongodb'] = static function ($app) {
/** @var MongoDB $mongoDB */
$mongoDB = $app[MongoDB::class];
/** @var MongoCollection $collection */
$collection = $mongoDB->results;

return new MongoSaver($collection);
};
}
}
Loading