diff --git a/protected/src/Classes/BaseController.php b/protected/src/Classes/BaseController.php index 2e084c9..4ab2a48 100644 --- a/protected/src/Classes/BaseController.php +++ b/protected/src/Classes/BaseController.php @@ -10,11 +10,14 @@ declare(strict_types = 1); namespace Project\Classes; use IOcornerstone\Framework\Http\HttpFactory; -use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\{ + ServerRequestInterface, + ResponseInterface, +}; use IOcornerstone\Framework\{ HtmlDocument, - View + View, + SaferOutput, }; class BaseController @@ -24,6 +27,10 @@ class BaseController private $http; public $view; public $html; + public $outputHTML; + public $pdo; + public $inputs; // Set by Inputs and used in Logic/Outputs + public $logic; // Used in Logic for Outputs protected string $footer = ""; protected string $authors = ""; @@ -33,15 +40,48 @@ class BaseController $this->http = new HttpFactory(); $this->view = new View(); $this->html = new HtmlDocument(); + $this->outputHTML = SaferOutput::class; // If the child controller has an init() method, call it automatically if (method_exists($this, 'init')) { $this->init(); } } - - public function returnResponse(string $body, int $status = 200, array $headers = []): ResponseInterface + + public function autoWireIOL( + string $root_folder, + string $file, + string $method = 'index', + ): ResponseInterface { + $saferRootFolder = Common::getAlphaNumbric($root_folder, allow_dashes: true); + $saferFile = Common::getAlphaNumbric($file, allow_dashes: true); + $saferMethod = Common::getAlphaNumbric($method, allow_dashes: true); + unset($root_folder); + unset($file); + unset($method); + + $classNameIn = "\\Project\\Classes\\Inputs\\{$saferRootFolder}\\{$saferFile}"; + $this->inputs = $classNameIn::$saferMethod($this->request); + + // Logic might not always exist, so check. + $classNameLogic = "\\Project\\Classes\\Logic\\{$saferRootFolder}\\{$saferFile}"; + if (method_exists($classNameLogic, $saferMethod)) { + $classNameLogic::$saferMethod($this); + } + + $classNameOut= "\\Project\\Classes\\Outputs\\{$saferRootFolder}\\{$saferFile}"; + return $classNameOut::$saferMethod($this); + } + + public function returnResponse(string|array $body, int $status = 200, array $headers = []): ResponseInterface { + if (is_array($body)) { + if (!in_array("Content-Type", $headers)) { + $headers = ['Content-Type' => 'application/json']; + } + + return $this->http->createResponse($status, $headers, json_encode($body)); + } return $this->http->createResponse($status, $headers, $body); }