autoWireIOL

main
Robert 1 month ago
parent ed81fb5da3
commit 98efec205d
  1. 50
      protected/src/Classes/BaseController.php

@ -10,11 +10,14 @@ declare(strict_types = 1);
namespace Project\Classes; namespace Project\Classes;
use IOcornerstone\Framework\Http\HttpFactory; use IOcornerstone\Framework\Http\HttpFactory;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\{
use Psr\Http\Message\ResponseInterface; ServerRequestInterface,
ResponseInterface,
};
use IOcornerstone\Framework\{ use IOcornerstone\Framework\{
HtmlDocument, HtmlDocument,
View View,
SaferOutput,
}; };
class BaseController class BaseController
@ -24,6 +27,10 @@ class BaseController
private $http; private $http;
public $view; public $view;
public $html; 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 $footer = "";
protected string $authors = ""; protected string $authors = "";
@ -33,15 +40,48 @@ class BaseController
$this->http = new HttpFactory(); $this->http = new HttpFactory();
$this->view = new View(); $this->view = new View();
$this->html = new HtmlDocument(); $this->html = new HtmlDocument();
$this->outputHTML = SaferOutput::class;
// If the child controller has an init() method, call it automatically // If the child controller has an init() method, call it automatically
if (method_exists($this, 'init')) { if (method_exists($this, 'init')) {
$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); return $this->http->createResponse($status, $headers, $body);
} }

Loading…
Cancel
Save