StickingToGoals.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
mygoals/protected/src/Classes/BaseController.php

105 lines
3.1 KiB

<?php
declare(strict_types = 1);
/**
* @author Robert Strutts
* @copyright (c) 2026, Robert Strutts
* @license MIT
*/
namespace Project\Classes;
use IOcornerstone\Framework\Http\HttpFactory;
use Psr\Http\Message\{
ServerRequestInterface,
ResponseInterface,
};
use IOcornerstone\Framework\{
HtmlDocument,
View,
SaferOutput,
};
class BaseController
{
public string $pageOutput = ''; // To keep views working...without Dynamic Variable Error!
public static $params; // To keep Routes working...
public $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 = "";
public function __construct(
public ServerRequestInterface $request
) {
$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 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);
}
public function setFooterAndAuthors()
{
if (defined('\Project\MyPage::MyFooter')) {
$this->footer = \Project\MyPage::MyFooter;
} else {
$this->footer = "";
}
if (defined('\Project\MyPage::MyAuthors')) {
$this->authors = \Project\MyPage::MyAuthors;
} else {
$this->authors = "";
}
$this->html->setFooter($this->footer);
$this->html->setAuthor($this->authors);
}
}