1
0
mirror of https://github.com/moparisthebest/wallabag synced 2024-12-18 21:52:19 -05:00

Move DB queries into a Repository class

This commit is contained in:
Vincent Jousse 2013-12-01 21:43:43 +01:00
parent 67571d47cc
commit 674cf03409
3 changed files with 44 additions and 9 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
use Knp\Provider\ConsoleServiceProvider; use Knp\Provider\ConsoleServiceProvider;
use Poche\Api\EntryApi; use Poche\Api\EntryApi;
use Poche\Repository\EntryRepository;
use Silex\Provider\FormServiceProvider; use Silex\Provider\FormServiceProvider;
@ -13,7 +14,7 @@ $app->register(new Silex\Provider\TwigServiceProvider(), array(
$app->before(function () use ($app) { $app->before(function () use ($app) {
$app['twig']->addGlobal('layout', $app['twig']->loadTemplate('layout.twig')); $app['twig']->addGlobal('layout', $app['twig']->loadTemplate('layout.twig'));
}); });
$app->register(new ConsoleServiceProvider(), [ $app->register(new ConsoleServiceProvider(), [
'console.name' => 'Poche console', 'console.name' => 'Poche console',
'console.version' => '0.1', 'console.version' => '0.1',
@ -33,6 +34,10 @@ $app->register(new Silex\Provider\TranslationServiceProvider(), array(
'translator.messages' => array(), 'translator.messages' => array(),
)); ));
$app['entry_api'] = $app->share(function ($app) { $app['entry_repository'] = $app->share(function ($app) {
return new EntryApi($app['db']); return new EntryRepository($app['db']);
});
$app['entry_api'] = $app->share(function ($app) {
return new EntryApi($app['entry_repository']);
}); });

View File

@ -4,15 +4,24 @@ namespace Poche\Api;
class EntryApi class EntryApi
{ {
private $db; private $entryRepository;
public function __construct($db) { public function __construct($entryRepository) {
$this->db = $db; $this->entryRepository = $entryRepository;
} }
public function getEntries() { public function getEntries() {
$sql = "SELECT * FROM entries"; return $this->entryRepository->getEntries();
$entries = $this->db->fetchAssoc($sql);
return ($entries ? $entries : array());
} }
public function createEntryFromUrl($url) {
//TODO: Fetch all what we need, fill the title, content …
$entry = array(
'url' => $url
);
return $entry;
}
} }

View File

@ -0,0 +1,21 @@
<?php
namespace Poche\Repository;
class EntryRepository
{
private $db;
public function __construct($db) {
$this->db = $db;
}
public function getEntries() {
$sql = "SELECT * FROM entries";
$entries = $this->db->fetchAssoc($sql);
return ($entries ? $entries : array());
}
}