Skip to content

Commit

Permalink
Enable singleton to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-factorin committed Jul 23, 2018
1 parent 686d139 commit 5645d30
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 42 deletions.
47 changes: 38 additions & 9 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class Controller
/** @var View $view */
protected $view;

/** @var Request $request */
protected $request;

/** @var Server $server */
protected $server;

/** @var Session $session */
protected $session;

/**
* Controller constructor.
* @param string $name
Expand All @@ -26,6 +35,10 @@ public function __construct($name)

$view = static::VIEW;
$this->view = new $view($this);

$this->request = Request::getInstance();
$this->session = Session::getInstance();
$this->server = Server::getInstance();
}

/**
Expand Down Expand Up @@ -63,17 +76,19 @@ public function dispatchMethod($methodName, $args = array())
throw new Exception(sprintf('Invalid method "%s::%s"', $class, $method));
}

if (method_exists($this, $method) === false) {
throw new Exception(sprintf('Method "%s::%s" not found', $class, $method));
}
if (method_exists($this, '__call') === false) {
if (method_exists($this, $method) === false) {
throw new Exception(sprintf('Method "%s::%s" not found', $class, $method));
}

$reflectionMethod = new \ReflectionMethod($this, $method);
if ($reflectionMethod->isPublic() === false) {
throw new Exception(sprintf('Method "%s::%s" should be public', $class, $method));
}
$reflectionMethod = new \ReflectionMethod($this, $method);
if ($reflectionMethod->isPublic() === false) {
throw new Exception(sprintf('Method "%s::%s" should be public', $class, $method));
}

if ($reflectionMethod->isStatic()) {
throw new Exception(sprintf('Method "%s::%s" should not be static', $class, $method));
if ($reflectionMethod->isStatic()) {
throw new Exception(sprintf('Method "%s::%s" should not be static', $class, $method));
}
}

$this->view->render($method);
Expand All @@ -87,4 +102,18 @@ public function buildResponse()
{
echo($this->view->build());
}

/**
* @param string $path
* @param array $query
*/
public function redirect($path, $query = array())
{
if (empty($query) === false) {
$path = sprintf('%s?%s', http_build_query($query));
}

header(sprintf('Location: %s', $path));
exit(0);
}
}
61 changes: 46 additions & 15 deletions src/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Class DataSource
* @package Slimcake\Core
*/
class DataSource
class DataSource extends Singleton
{
/** @var \PDO $pdo */
protected $pdo;
Expand All @@ -21,7 +21,13 @@ protected function __construct()
\PDO::ATTR_EMULATE_PREPARES => false
);

$dsn = Config::get('DATABASE_DSN', null);
$dsn = sprintf(
'mysql:host=%s;port=%s;dbname=%s',
Config::get('DATABASE_HOST', '127.0.0.1'),
Config::get('DATABASE_PORT', 3306),
Config::get('DATABASE_NAME', null)
);

$user = Config::get('DATABASE_USER', 'root');
$pass = Config::get('DATABASE_PASS', null);
$options = array_merge($options, Config::get('DATABASE_OPTIONS', array()));
Expand All @@ -30,16 +36,35 @@ protected function __construct()
}

/**
* @return DataSource
* @param string $table
* @param array $where
* @param array $order
* @return array
*/
public static function getInstance()
protected function createQuery($table, $where = array(), $order = array())
{
static $instance;
if (empty($instance)) {
$instance = new static();
$data = array();
$query = 'SELECT *'
. ' FROM `%s`';

if (empty($where) === false) {
$filters = array_map(function ($k) {
return sprintf('`%s` = ?', Inflector::underscore($k));
}, array_keys($where));

$data = array_values($where);
$query = sprintf('%s WHERE %s', $query, implode(' AND ', $filters));
}

if (empty($order) === false) {
$orders = array_map(function ($k, $v) {
return sprintf('`%s` %s', Inflector::underscore($k), strtoupper($v));
}, array_keys($order), $order);

$query = sprintf('%s ORDER BY %s', $query, implode(', ', $orders));
}

return $instance;
return array(sprintf($query, $table), $data);
}

/** Begin transaction */
Expand Down Expand Up @@ -147,22 +172,28 @@ public function delete($table, $where = array())
}

/**
* @param string $query
* @param array $data
* @return mixed
* @param string $table
* @param array $where
* @param array $order
* @return array
*/
public function find($query, $data = array())
public function find($table, $where = array(), $order = array())
{
list($query, $data) = $this->createQuery($table, $where, $order);

return $this->execute($query, $data)->fetch(\PDO::FETCH_ASSOC);
}

/**
* @param string $query
* @param array $data
* @param string $table
* @param array $where
* @param array $order
* @return array
*/
public function findAll($query, $data = array())
public function findAll($table, $where = array(), $order = array())
{
list($query, $data) = $this->createQuery($table, $where, $order);

return $this->execute($query, $data)->fetchAll(\PDO::FETCH_ASSOC);
}
}
2 changes: 1 addition & 1 deletion src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Dispatcher
*/
public static function parseURI()
{
$path = Server::get('PATH_INFO');
$path = Server::getInstance()->get('PATH_INFO');
$routes = explode('/', trim($path, '/'));

$args = array();
Expand Down
52 changes: 44 additions & 8 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,56 @@ public function __construct($data = array())
* Get validation constraints
* @return array
*/
protected function constraints()
protected static function validations()
{
return array();
}

/**
* @return Validator
* @param array $data
* @return static[]
*/
protected function validator()
protected static function populate(array $data)
{
static $validator;
if (empty($validator)) {
$validator = new Validator($this);
$obj = array();
foreach ($data as $v) {
$obj[] = new static($v);
}

return $validator;
return $obj;
}

/**
* @param int $id
* @return array
*/
public static function find($id)
{
return static::findBy(array('id' => $id));
}

/**
* @param array $where
* @param array $order
* @return null|Model|static
*/
public static function findBy($where = array(), $order = array())
{
$data = DataSource::getInstance()->find(static::TABLE_NAME, $where, $order);

return empty($data) ? null : new static($data);
}

/**
* @param array $where
* @param array $order
* @return array|Model[]|static[]
*/
public static function findAll($where = array(), $order = array())
{
$data = DataSource::getInstance()->findAll(static::TABLE_NAME, $where, $order);

return empty($data) ? array() : static::populate($data);
}

/**
Expand All @@ -56,7 +90,9 @@ protected function validator()
*/
public function validate()
{
return $this->validator()->validate($this->constraints());
$validator = new Validator($this);

return $validator->validate(static::validations());
}

/**
Expand Down
26 changes: 23 additions & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,43 @@
* Class Request
* @package Slimcake\Core
*/
class Request
class Request extends Singleton
{
/**
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($key, $default = null)
public function get($key, $default = null)
{
return isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default;
}

/**
* @param string $key
* @param mixed $data
*/
public function set($key, $data = null)
{
$_REQUEST[$key] = $data;
}

/**
* @return array
*/
public static function all()
public function all()
{
return isset($_REQUEST) ? $_REQUEST : array();
}

/**
* @param string $method
*
* @return bool
*/
public function isMethod($method)
{
$server = Server::getInstance();
return strtoupper($method) === strtoupper($server->get('REQUEST_METHOD'));
}
}
8 changes: 4 additions & 4 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Class Server
* @package Slimcake\Core
*/
class Server
class Server extends Singleton
{
/**
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($key, $default = null)
public function get($key, $default = null)
{
return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
}
Expand All @@ -22,15 +22,15 @@ public static function get($key, $default = null)
* @param string $key
* @param mixed $data
*/
public static function set($key, $data = null)
public function set($key, $data = null)
{
$_SERVER[$key] = $data;
}

/**
* @return array
*/
public static function all()
public function all()
{
return isset($_SERVER) ? $_SERVER : array();
}
Expand Down
Loading

0 comments on commit 5645d30

Please sign in to comment.