Skip to content
Snippets Groups Projects
Commit 910b0ec9 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

page class: php8 only; added variable types; short array syntax

parent 43b5d42e
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
<?php
/**
* PIMPED APACHE-STATUS
* Page class
* Render output page by replacing placeholders
*
* @package pimped_apache_status
* origin is from PIMPED APACHE-STATUS
*
* @author Axel Hahn
*
* 2024-08-23 v1.1 Axel Hahn php8 only; added variable types; short array syntax
*/
class Page {
class Page
{
/**
* output type of content
* @var array
* Output type of content
* @var string
*/
private $sType = 'html';
private string $sType = 'html';
/**
* array of strings for http response header
* Array of strings for http response header
* @var array
*/
private $aResponseHeader = array();
private array $aResponseHeader = [];
/**
* Replacements in the template
* @var array
*/
private $aReplace = array(
private array $aReplace = [
'{{HEADER}}' => '',
'{{CONTENT}}' => '',
'{{FOOTER}}' => '',
'{{JSONREADY}}' => '',
);
];
/**
* constructor (it does nothing)
* @return boolean (true)
* Constructor
*/
public function __construct() {
public function __construct()
{
$this->setOutputtype();
return true;
}
/**
* wrap on document ready instructions in jQuery style
* @return type
* Wrap on document ready instructions in jQuery style
* @return string
*/
private function _finalizeJsOnReady() {
private function _finalizeJsOnReady(): string
{
return $this->aReplace["{{JSONREADY}}"] = '
<script>
$(document).ready(function() {
......@@ -60,43 +63,48 @@ class Page {
// ----------------------------------------------------------------------
/**
* get current page content
* @return string
* Get current page content
* @return string HTML code
*/
public function getContent() {
public function getContent(): string
{
return $this->aReplace['{{CONTENT}}'];
}
/**
* get current footer
* @return type
* Get current footer
* @return string
*/
public function getFooter() {
public function getFooter(): string
{
return $this->aReplace['{{FOOTER}}'];
}
/**
* get current header in response body
* @return type
* Get current header in response body
* @return string
*/
public function getHeader() {
public function getHeader(): string
{
return $this->aReplace['{{HEADER}}'];
}
/**
* get on ready javascript instructions
* @return type¨
* Get on ready javascript instructions
* @return string
*/
public function getJsOnReady() {
public function getJsOnReady(): string
{
return $this->aReplace['{{JSONREADY}}'];
}
/**
* get output type
* Get output type
* @return string
*/
public function getOutputtype() {
public function getOutputtype()
{
return $this->sType;
}
......@@ -105,12 +113,14 @@ class Page {
// ----------------------------------------------------------------------
/**
* add javascript for on ready execution
* Add javascript for on ready execution
* @param string $s javascript code
* @return boolean
* @return bool
*/
public function addJsOnReady($s) {
return $this->aReplace['{{JSONREADY}}'] .= $s;
public function addJsOnReady(string $s): string
{
$this->aReplace['{{JSONREADY}}'] .= $s;
return true;
}
/**
......@@ -118,67 +128,78 @@ class Page {
* @param string $s
* @return boolean
*/
public function addResponseHeader($s) {
return $this->aResponseHeader[] = $s;
public function addResponseHeader(string $s)
{
$this->aResponseHeader[] = $s;
return true;
}
/**
* set html body; it replaces old content
* Set html body; it replaces old content
* @param string $s html code
* @return boolean
*/
public function setContent($s) {
return $this->aReplace['{{CONTENT}}'] = $s;
public function setContent(string $s): bool
{
$this->aReplace['{{CONTENT}}'] = $s;
return true;
}
/**
* set footer in html body; it replaces old content
* Set footer in html body; it replaces old content
* @param string $s html code
* @return boolean
*/
public function setFooter($s) {
return $this->aReplace['{{FOOTER}}'] = $s;
public function setFooter(string $s): bool
{
$this->aReplace['{{FOOTER}}'] = $s;
return true;
}
/**
* set html header; it replaces old content
* Set html header; it replaces old content
* @param string $s html code
* @return boolean
*/
public function setHeader($s) {
return $this->aReplace['{{HEADER}}'] = $s;
public function setHeader(string $s): bool
{
$this->aReplace['{{HEADER}}'] = $s;
return true;
}
/**
* set javascript code on ready; it replaces old content
* Set javascript code on ready; it replaces old content
* @param string $s javascript code
* @return boolean
*/
public function setJsOnReady($s) {
return $this->aReplace['{{JSONREADY}}'] = $s;
public function setJsOnReady(string $s): bool
{
$this->aReplace['{{JSONREADY}}'] = $s;
return true;
}
/**
* set output type of response
* Set output type of response
* @param string $sOutputType
* @return boolean
*/
public function setOutputtype($sOutputType = 'html') {
return $this->sType = $sOutputType;
public function setOutputtype(string $sOutputType = 'html'): bool
{
$this->sType = $sOutputType;
return true;
}
// ----------------------------------------------------------------------
// OUTPUT
// ----------------------------------------------------------------------
/**
* send http reponse headers and built the response body
* @return type
* Send http reponse headers and built the response body
* @return string
*/
public function render() {
$aS = array(); // search
$aR = array(); // replace
public function render(): string
{
$aS = []; // search
$aR = []; // replace
$this->_finalizeJsOnReady();
......@@ -190,12 +211,12 @@ class Page {
$sTemplate = false;
$sTplFile = dirname(__FILE__) . "/" . $this->sType . ".tpl.php";
if (!file_exists($sTplFile)) {
die("ERROR: template for type " . $this->sType . " was not found: $sTplFile");
throw new Exception("ERROR: template for type " . $this->sType . " was not found: $sTplFile");
}
$sTemplate = file_get_contents($sTplFile);
if (!$sTemplate) {
die("ERROR: template file $sTplFile is empty or could not be read.");
throw new Exception("ERROR: template file $sTplFile is empty or could not be read.");
}
foreach ($this->aResponseHeader as $sHeader) {
......@@ -205,5 +226,3 @@ class Page {
}
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment