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

Drop Saver Factory #329

Merged
merged 1 commit into from
Sep 16, 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
52 changes: 0 additions & 52 deletions src/Xhgui/Saver.php

This file was deleted.

48 changes: 36 additions & 12 deletions src/Xhgui/ServiceContainer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use MongoDB\Driver\Manager;
use Pimple\Container;
use Slim\Slim;
use Slim\Views\Twig;
Expand Down Expand Up @@ -108,7 +109,7 @@ protected function _services()
);
};

$this['searcher.mongo'] = static function ($c) {
$this['searcher.mongodb'] = static function ($c) {
return new Xhgui_Searcher_Mongo($c['db']);
};

Expand All @@ -117,27 +118,50 @@ protected function _services()
};

$this['searcher'] = static function ($c) {
$config = $c['config'];
$saver = $c['config']['save.handler'];

return $c["searcher.$saver"];
};

switch ($config['save.handler']) {
case 'pdo':
return $c['searcher.pdo'];
$this['saver.mongodb'] = static function ($c) {
$config = $c['config'];

case 'mongodb':
default:
return $c['searcher.mongo'];
if (!class_exists(Manager::class)) {
throw new RuntimeException("Required extension ext-mongodb missing");
}
$mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']);
$collection = $mongo->{$config['db.db']}->results;
$collection->findOne();

return new Xhgui_Saver_Mongo($collection);
};

$this['saver.mongo'] = static function ($c) {
$this['saver.pdo'] = static function ($c) {
$config = $c['config'];
$config['save.handler'] = 'mongodb';

return Xhgui_Saver::factory($config);
if (!class_exists(PDO::class)) {
throw new RuntimeException("Required extension ext-pdo is missing");
}

$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];

return new Xhgui_Saver_Pdo(
new PDO(
$config['pdo']['dsn'],
$config['pdo']['user'],
$config['pdo']['pass'],
$options
),
$config['pdo']['table']
);
};

$this['saver'] = static function ($c) {
return Xhgui_Saver::factory($c['config']);
$saver = $c['config']['save.handler'];

return $c["saver.$saver"];
};
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Searcher/MongoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class MongoTest extends TestCase
public function setUp()
{
$di = Xhgui_ServiceContainer::instance();
$this->mongo = $di['searcher.mongo'];
$this->mongo = $di['searcher.mongodb'];

$di['db']->watches->drop();

loadFixture($di['saver.mongo'], XHGUI_ROOT_DIR . '/tests/fixtures/results.json');
loadFixture($di['saver.mongodb'], XHGUI_ROOT_DIR . '/tests/fixtures/results.json');
}

public function testCustomQuery()
Expand Down