GIF89a;
| Direktori : /home/serb/www/api/src/Services/ |
| Current File : /home/serb/www/api/src/Services/AbstractService.php |
<?php
namespace Mar\Services;
use \Slim\Slim,
Mar\Collections\JsonCollection;
/**
* Abstract Service
*/
abstract class AbstractService
{
protected $_app;
protected $_config;
private static $_resource;
public function __construct(Slim $app)
{
if (!isset($this->_configSection)) {
throw new \Exception('There\'s no configuration section configured.');
}
if (!isset($app->settings[$this->_configSection])) {
throw new \Exception('There\'s no configuration for the selected section.');
}
$this->_app = $app;
$this->_config = $this->_app->settings[$this->_configSection];
if (!isset($this->_config['fields'])) {
$this->_config['fields'] = [];
}
}
protected function resource() {
if (!is_object(self::$_resource)) {
$db = $this->_app->settings['database'];
self::$_resource = new \PDO("mysql:host={$db['hostname']};dbname={$db['database']};charset=utf8", $db['username'], $db['password']);
}
return self::$_resource;
}
protected function query($sql, $params = array()) {
try {
$stmt = $this->resource()->query($sql);
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$data = new JsonCollection($data, $this->_app, $this->_config['fields']);
} catch(\PDOException $ex) {
var_dump($ex->getMessage());
}
return $data;
}
public function findAll() {
return $this->query($this->_config['queries']['index']);
}
}