From ec07dddb4c38698494ddf7ea611c0d35da02e585 Mon Sep 17 00:00:00 2001 From: alext Date: Sat, 10 Nov 2018 09:44:27 +0100 Subject: [PATCH] Implementation of core engine components. --- src/engine/Core/Controller.php | 23 +++++ src/engine/Core/Framework.php | 43 ++++++++ src/engine/Core/Input.php | 145 +++++++++++++++++++++++++++ src/engine/Core/Migration.php | 23 +++++ src/engine/Core/Model.php | 23 +++++ src/engine/Core/Output.php | 82 +++++++++++++++ src/engine/Core/QueryBuilder.php | 166 +++++++++++++++++++++++++++++++ src/engine/Core/Session.php | 77 ++++++++++++++ 8 files changed, 582 insertions(+) create mode 100644 src/engine/Core/Controller.php create mode 100644 src/engine/Core/Framework.php create mode 100644 src/engine/Core/Input.php create mode 100644 src/engine/Core/Migration.php create mode 100644 src/engine/Core/Model.php create mode 100644 src/engine/Core/Output.php create mode 100644 src/engine/Core/QueryBuilder.php create mode 100644 src/engine/Core/Session.php diff --git a/src/engine/Core/Controller.php b/src/engine/Core/Controller.php new file mode 100644 index 00000000..c999695c --- /dev/null +++ b/src/engine/Core/Controller.php @@ -0,0 +1,23 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Core; + +/** + * Class Controller + * + * @package EA\Engine\Core + */ +class Controller extends \CI_Controller { + +} diff --git a/src/engine/Core/Framework.php b/src/engine/Core/Framework.php new file mode 100644 index 00000000..2be399b2 --- /dev/null +++ b/src/engine/Core/Framework.php @@ -0,0 +1,43 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Core; + +/** + * Class Framework + * + * @package EA\Engine\Core + */ +class Framework { + /** + * Get the framework instance. + * + * @return \CI_Controller + */ + public static function getInstance() + { + return get_instance(); + } + + /** + * Magit get method that gives access to the internal members of the framework intance. + * + * @param string $key + * + * @return mixed + */ + public function __get($key) + { + return self::getInstance()->$key; + } +} diff --git a/src/engine/Core/Input.php b/src/engine/Core/Input.php new file mode 100644 index 00000000..c4cb2995 --- /dev/null +++ b/src/engine/Core/Input.php @@ -0,0 +1,145 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Core; + +/** + * Class Input + * + * @package EA\Engine\Core + */ +class Input { + /** + * @var \EA\Engine\Core\Framework + */ + private $framework; + + /** + * Input constructor. + * + * @param Framework $framework + */ + public function __construct(Framework $framework) + { + $this->framework = $framework; + } + + + /** + * Get request (either GET or POST) parameter (not filtered/escaped). + * + * @param string $key + * + * @return string + */ + public function request($key) + { + return isset($_REQUEST[$key]) ? $_REQUEST[$key] : NULL; + } + + /** + * Get GET parameter value. + * + * @param string $key + */ + public function get($key) + { + $this->framework->input->get($key); + } + + /** + * Get POST parameter value. + * + * @param string $key + */ + public function post($key) + { + $this->framework->input->post($key); + } + + /** + * Get COOKIE parameter value. + * + * @param string $key + * + * @return string + */ + public function cookie($key) + { + return $this->framework->input->cookie($key); + } + + /** + * Get SERVER parameter value. + * + * @param string $key + * + * @return string + */ + public function server($key) + { + return $this->framework->input->server($key); + } + + /** + * Get raw request input. + * + * @return string + */ + public function raw() + { + return $this->framework->input->raw_input_stream; + } + + /** + * Get json decoded raw request input. + * + * @return string + */ + public function json() + { + return json_decode($this->framework->input->raw_input_stream); + } + + /** + * Get request headers as an array. + * + * @return array + */ + public function headers() + { + return $this->framework->input->request_headers(); + } + + /** + * Get request header value. + * + * @param string $key + * + * @return string + */ + public function header($key) + { + return $this->framework->input->get_request_header($key); + } + + /** + * Determine if this is a CLI request or not. + * + * @return bool + */ + public function isCliRequest() + { + return $this->framework->is_cli_request(); + } +} diff --git a/src/engine/Core/Migration.php b/src/engine/Core/Migration.php new file mode 100644 index 00000000..951b095e --- /dev/null +++ b/src/engine/Core/Migration.php @@ -0,0 +1,23 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Core; + +/** + * Class Migration + * + * @package EA\Engine\Core + */ +class Migration extends \CI_Migration { + +} diff --git a/src/engine/Core/Model.php b/src/engine/Core/Model.php new file mode 100644 index 00000000..dca7ae17 --- /dev/null +++ b/src/engine/Core/Model.php @@ -0,0 +1,23 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Core; + +/** + * Class Model + * + * @package EA\Engine\Core + */ +class Model extends \CI_Model { + +} diff --git a/src/engine/Core/Output.php b/src/engine/Core/Output.php new file mode 100644 index 00000000..3b9e6a56 --- /dev/null +++ b/src/engine/Core/Output.php @@ -0,0 +1,82 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Core; + +/** + * Class Output + * + * @package EA\Engine\Core + */ +class Output { + /** + * @var \EA\Engine\Core\Framework + */ + private $framework; + + /** + * Output constructor. + * + * @param Framework $framework + */ + public function __construct(Framework $framework) + { + $this->framework = $framework; + } + + /** + * Output HTML markup content. + * + * @param string $content + * @param int $statusCode + * @param array $headers + */ + public function html($content, $statusCode = 200, array $headers = []) + { + $headers[] = 'Content-Type: text/html'; + $this->write($content, $statusCode, $headers); + } + + /** + * Output JSON encoded content. + * + * @param mixed $content + * @param int $statusCode + * @param array $headers + */ + public function json($content, $statusCode = 200, array $headers = []) + { + $headers[] = 'Content-Type: application/json; charset=utf-8'; + $content = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + $this->write($content, $statusCode, $headers); + } + + /** + * Output arbitrary content with custom status and headers. + * + * @param string $content + * @param int $statusCode + * @param array $headers + */ + public function write($content, $statusCode = 200, array $headers = []) + { + $this->framework->output->set_status_header($statusCode); + + foreach ($headers as $header) + { + $this->framework->output->set_header($header); + } + + $this->framework->output->set_output($content); + } +} diff --git a/src/engine/Core/QueryBuilder.php b/src/engine/Core/QueryBuilder.php new file mode 100644 index 00000000..3d1ba681 --- /dev/null +++ b/src/engine/Core/QueryBuilder.php @@ -0,0 +1,166 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Core; + +/** + * Class QueryBuilder + * + * @package EA\Engine\Core + */ +class QueryBuilder { + /** + * @var \EA\Engine\Core\Framework + */ + private $framework; + + /** + * QueryBuilder constructor. + * + * @param \EA\Engine\Core\Framework $framework + */ + public function __construct(Framework $framework) + { + $this->framework = $framework; + } + + + /** + * Count matching query rows. + * + * @param string $table + * @param mixed $conditions + * + * @return int + */ + public function count($table, $conditions) + { + return $this->framework->db->get_where($table, $conditions)->num_rows(); + } + + /** + * Perform an arbitrary query to the databbase. + * + * @param $query + * + * @return mixed + */ + public function query($query) + { + return $this->framework->db->query($query); + } + + /** + * Get a single database row, based on the provided conditions. + * + * @param string $table + * @param mixed $conditions + * + * @return mixed + */ + public function row($table, $conditions) + { + return $this->framework->db->get_where($table, $conditions)->row_array(); + } + + /** + * Get a collection of rows, based on the provided parameters. + * + * @param string $table + * @param mixed|null $conditions + * @param int|null $limit + * @param int|null $offset + * @param mixed|null $order + * + * @return mixed + */ + public function collection($table, $conditions = NULL, $limit = NULL, $offset = NULL, $order = NULL) + { + if ($limit) + { + $this->framework->db->limit($limit); + } + + if ($limit && $offset) + { + $this->framework->db->limit($limit, $offset); + } + + if ($order) + { + $this->framework->db->order_by($order); + } + + if ($conditions) + { + $this->framework->db->where($conditions); + } + + return $this->framework->db->get($table)->result_array(); + } + + /** + * Insert a new database row. + * + * @param string $table + * @param array $row + * + * @return int Returns the inserted ID. + */ + public function insert($table, $row) + { + $this->framework->db->insert($table, $row); + + return $this->framework->db->insert_id(); + } + + /** + * Update an existing database row. + * + * @param string $table + * @param array $row + * @param mixed $conditions + * + * @return mixed Returns the operation result. + */ + public function update($table, $row, $conditions) + { + return $this->framework->db->update($table, $row, $conditions); + } + + /** + * Delete an existing database row. + * + * @param string $table + * @param array $row + * @param mixed $conditions + * + * @return mixed + */ + public function delete($table, $row, $conditions) + { + return $this->framework->db->delete($table, $row, $conditions); + } + + /** + * Escape a raw value before using it in a query. + * + * @param string $value + * + * @return string + */ + public function escape($value) + { + return $this->framework->db->escape($value); + } +} diff --git a/src/engine/Core/Session.php b/src/engine/Core/Session.php new file mode 100644 index 00000000..93451ff2 --- /dev/null +++ b/src/engine/Core/Session.php @@ -0,0 +1,77 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Core; + +/** + * Class Session + * + * @package EA\Engine\Core + */ +class Session { + /** + * @var \EA\Engine\Core\Framework + */ + private $framework; + + /** + * Session constructor. + * + * @param Framework $framework + */ + public function __construct(Framework $framework) + { + $this->framework = $framework; + } + + + /** + * Create a new session for the current user. + */ + public function create() + { + $this->framework->load->library('session'); + } + + /** + * Destroy the session of the current user. + */ + public function destroy() + { + $this->framework->session->sess_destroy(); + } + + /** + * Get a session value. + * + * @param string $key + * + * @return mixed + */ + public function get($key) + { + return $this->framework->session->userdata($key); + } + + /** + * Set a session value. + * + * @param string $key + * + * @param mixed $value + */ + public function set($key, $value) + { + $this->framework->session->userdata($key, $value); + } +}