1
0
mirror of https://github.com/moparisthebest/wallabag synced 2024-08-13 16:54:00 -04: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
use Knp\Provider\ConsoleServiceProvider;
use Poche\Api\EntryApi;
use Poche\Repository\EntryRepository;
use Silex\Provider\FormServiceProvider;
@ -13,7 +14,7 @@ $app->register(new Silex\Provider\TwigServiceProvider(), array(
$app->before(function () use ($app) {
$app['twig']->addGlobal('layout', $app['twig']->loadTemplate('layout.twig'));
});
$app->register(new ConsoleServiceProvider(), [
'console.name' => 'Poche console',
'console.version' => '0.1',
@ -33,6 +34,10 @@ $app->register(new Silex\Provider\TranslationServiceProvider(), array(
'translator.messages' => array(),
));
$app['entry_api'] = $app->share(function ($app) {
return new EntryApi($app['db']);
$app['entry_repository'] = $app->share(function ($app) {
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
{
private $db;
private $entryRepository;
public function __construct($db) {
$this->db = $db;
public function __construct($entryRepository) {
$this->entryRepository = $entryRepository;
}
public function getEntries() {
$sql = "SELECT * FROM entries";
$entries = $this->db->fetchAssoc($sql);
return ($entries ? $entries : array());
return $this->entryRepository->getEntries();
}
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());
}
}