Added proper DI and autoloading to make things simpler for the actual logic'
This commit is contained in:
parent
ecbd2fd94a
commit
2ef67de718
18
api/DependencyManager.php
Normal file
18
api/DependencyManager.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
// Responsible for loading in all necessary classes. AKA a poor man's DI solution.
|
||||
use BusinessLogic\Category\CategoryRetriever;
|
||||
use DataAccess\CategoryGateway;
|
||||
|
||||
class DependencyManager {
|
||||
public $get;
|
||||
|
||||
function __construct() {
|
||||
$this->get = array();
|
||||
|
||||
$this->get['CategoryGateway'] = new CategoryGateway();
|
||||
$this->get['CategoryRetriever'] = new CategoryRetriever($this->get['CategoryGateway']);
|
||||
}
|
||||
}
|
25
api/autoload.php
Normal file
25
api/autoload.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
// Responsible for loading in all necessary scripts and kicking off the DependencyManager
|
||||
|
||||
define('IN_SCRIPT', 1);
|
||||
define('HESK_PATH', '../');
|
||||
require_once(__DIR__ . '/core/common.php');
|
||||
require_once(__DIR__ . '/Link.php');
|
||||
require_once(__DIR__ . '/../hesk_settings.inc.php');
|
||||
|
||||
// FILES
|
||||
require_once(__DIR__ . '/http_response_code.php');
|
||||
require_once(__DIR__ . '/dao/category/CategoryGateway.php');
|
||||
require_once(__DIR__ . '/businesslogic/category/CategoryRetriever.php');
|
||||
require_once(__DIR__ . '/businesslogic/category/Category.php');
|
||||
require_once(__DIR__ . '/controllers/CategoryController.php');
|
||||
|
||||
hesk_load_api_database_functions();
|
||||
|
||||
// HESK files that require database access
|
||||
require_once(__DIR__ . '/../inc/custom_fields.inc.php');
|
||||
|
||||
require_once(__DIR__ . '/DependencyManager.php');
|
||||
|
||||
$applicationContext = new \Core\DependencyManager();
|
@ -9,7 +9,7 @@ class ValidationException extends Exception {
|
||||
|
||||
/**
|
||||
* ValidationException constructor.
|
||||
* @param ValidationModel $validationModel The validatio model
|
||||
* @param ValidationModel $validationModel The validation model
|
||||
* @throws Exception If the validationModel's errorKeys is empty
|
||||
*/
|
||||
function __construct($validationModel) {
|
||||
|
@ -11,9 +11,16 @@ namespace BusinessLogic\Category;
|
||||
use DataAccess\CategoryGateway;
|
||||
|
||||
class CategoryRetriever {
|
||||
static function get_all_categories($hesk_settings) {
|
||||
require_once(__DIR__ . '/../../dao/category/CategoryGateway.php');
|
||||
/**
|
||||
* @var CategoryGateway
|
||||
*/
|
||||
private $categoryGateway;
|
||||
|
||||
return CategoryGateway::getAllCategories($hesk_settings);
|
||||
function __construct($categoryGateway) {
|
||||
$this->categoryGateway = $categoryGateway;
|
||||
}
|
||||
|
||||
function getAllCategories($hesk_settings) {
|
||||
return $this->categoryGateway->getAllCategories($hesk_settings);
|
||||
}
|
||||
}
|
@ -1,10 +1,4 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: user
|
||||
* Date: 1/16/17
|
||||
* Time: 10:12 PM
|
||||
*/
|
||||
|
||||
namespace Controllers\Category;
|
||||
|
||||
@ -21,9 +15,11 @@ class CategoryController {
|
||||
}
|
||||
|
||||
private static function getAllCategories() {
|
||||
global $hesk_settings;
|
||||
require_once(__DIR__ . '/../businesslogic/category/CategoryRetriever.php');
|
||||
global $hesk_settings, $applicationContext;
|
||||
|
||||
return CategoryRetriever::get_all_categories($hesk_settings);
|
||||
/* @var $categoryRetriever CategoryRetriever */
|
||||
$categoryRetriever = $applicationContext->get['CategoryRetriever'];
|
||||
|
||||
return $categoryRetriever->getAllCategories($hesk_settings);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
function print_error($title, $message) {
|
||||
function print_error($title, $message, $response_code = 500) {
|
||||
require_once(__DIR__ . '/output.php');
|
||||
|
||||
$error = array();
|
||||
@ -8,6 +8,6 @@ function print_error($title, $message) {
|
||||
$error['title'] = $title;
|
||||
$error['message'] = $message;
|
||||
|
||||
print output($error);
|
||||
print output($error, $response_code);
|
||||
return;
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
function output($data, $status_code = 200) {
|
||||
http_response_code($status_code);
|
||||
header('Content-Type: application/json');
|
||||
print json_encode($data);
|
||||
return http_response_code($status_code);
|
||||
}
|
@ -12,9 +12,7 @@ use BusinessObjects\Category;
|
||||
use Exception;
|
||||
|
||||
class CategoryGateway {
|
||||
static function getAllCategories($hesk_settings) {
|
||||
require_once(__DIR__ . '/../../businesslogic/category/Category.php');
|
||||
|
||||
function getAllCategories($hesk_settings) {
|
||||
if (!function_exists('hesk_dbConnect')) {
|
||||
throw new Exception('Database not loaded!');
|
||||
}
|
||||
|
15
api/http_response_code.php
Normal file
15
api/http_response_code.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
// For 4.3.0 <= PHP <= 5.4.0
|
||||
if (function_exists('http_response_code') === false) {
|
||||
function http_response_code($newcode = NULL) {
|
||||
static $code = 200;
|
||||
if ($newcode !== NULL) {
|
||||
header('X-PHP-Response-Code: ' . $newcode, true, $newcode);
|
||||
if (!headers_sent()) {
|
||||
$code = $newcode;
|
||||
}
|
||||
}
|
||||
return $code;
|
||||
}
|
||||
}
|
@ -1,17 +1,6 @@
|
||||
<?php
|
||||
// Router: handles all REST requests to go to their proper place. Common dependency loading also happens here
|
||||
define('IN_SCRIPT', 1);
|
||||
define('HESK_PATH', '../');
|
||||
require_once(__DIR__ . '/core/common.php');
|
||||
require_once(__DIR__ . '/Link.php');
|
||||
require_once(__DIR__ . '/../hesk_settings.inc.php');
|
||||
|
||||
// Controllers
|
||||
require_once(__DIR__ . '/controllers/CategoryController.php');
|
||||
hesk_load_api_database_functions();
|
||||
require_once(__DIR__ . '/../inc/custom_fields.inc.php');
|
||||
|
||||
// Properly handle error logging, as well as a fatal error workaround
|
||||
require_once(__DIR__ . '/autoload.php');
|
||||
error_reporting(0);
|
||||
set_error_handler('errorHandler');
|
||||
register_shutdown_function('fatalErrorShutdownHandler');
|
||||
@ -45,5 +34,7 @@ Link::all(array(
|
||||
// Categories
|
||||
'/v1/categories' => '\Controllers\Category\CategoryController::printAllCategories',
|
||||
'/v1/categories/{i}' => '\Controllers\Category\CategoryController',
|
||||
|
||||
// Any URL that doesn't match goes to the 404 handler
|
||||
'404' => 'handle404'
|
||||
));
|
Loading…
x
Reference in New Issue
Block a user