Skip to content

Commit

Permalink
Merge pull request #329 from perftools/duplication
Browse files Browse the repository at this point in the history
Drop Saver Factory
  • Loading branch information
glensc committed Sep 16, 2020
2 parents eaa5956 + fedf4c7 commit dd43613
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 66 deletions.
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

0 comments on commit dd43613

Please sign in to comment.