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 <?php
/** /**
* PIMPED APACHE-STATUS
* Page class * Page class
* Render output page by replacing placeholders * Render output page by replacing placeholders
* *
* @package pimped_apache_status * origin is from PIMPED APACHE-STATUS
*
* @author Axel Hahn * @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 * Output type of content
* @var array * @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 * @var array
*/ */
private $aResponseHeader = array(); private array $aResponseHeader = [];
/** /**
* Replacements in the template * Replacements in the template
* @var array * @var array
*/ */
private $aReplace = array( private array $aReplace = [
'{{HEADER}}' => '', '{{HEADER}}' => '',
'{{CONTENT}}' => '', '{{CONTENT}}' => '',
'{{FOOTER}}' => '', '{{FOOTER}}' => '',
'{{JSONREADY}}' => '', '{{JSONREADY}}' => '',
); ];
/** /**
* constructor (it does nothing) * Constructor
* @return boolean (true)
*/ */
public function __construct() { public function __construct()
{
$this->setOutputtype(); $this->setOutputtype();
return true;
} }
/** /**
* wrap on document ready instructions in jQuery style * Wrap on document ready instructions in jQuery style
* @return type * @return string
*/ */
private function _finalizeJsOnReady() { private function _finalizeJsOnReady(): string
{
return $this->aReplace["{{JSONREADY}}"] = ' return $this->aReplace["{{JSONREADY}}"] = '
<script> <script>
$(document).ready(function() { $(document).ready(function() {
...@@ -60,43 +63,48 @@ class Page { ...@@ -60,43 +63,48 @@ class Page {
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* get current page content * Get current page content
* @return string * @return string HTML code
*/ */
public function getContent() { public function getContent(): string
{
return $this->aReplace['{{CONTENT}}']; return $this->aReplace['{{CONTENT}}'];
} }
/** /**
* get current footer * Get current footer
* @return type * @return string
*/ */
public function getFooter() { public function getFooter(): string
{
return $this->aReplace['{{FOOTER}}']; return $this->aReplace['{{FOOTER}}'];
} }
/** /**
* get current header in response body * Get current header in response body
* @return type * @return string
*/ */
public function getHeader() { public function getHeader(): string
{
return $this->aReplace['{{HEADER}}']; return $this->aReplace['{{HEADER}}'];
} }
/** /**
* get on ready javascript instructions * Get on ready javascript instructions
* @return type¨ * @return string
*/ */
public function getJsOnReady() { public function getJsOnReady(): string
{
return $this->aReplace['{{JSONREADY}}']; return $this->aReplace['{{JSONREADY}}'];
} }
/** /**
* get output type * Get output type
* @return string * @return string
*/ */
public function getOutputtype() { public function getOutputtype()
{
return $this->sType; return $this->sType;
} }
...@@ -105,12 +113,14 @@ class Page { ...@@ -105,12 +113,14 @@ class Page {
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* add javascript for on ready execution * Add javascript for on ready execution
* @param string $s javascript code * @param string $s javascript code
* @return boolean * @return bool
*/ */
public function addJsOnReady($s) { public function addJsOnReady(string $s): string
return $this->aReplace['{{JSONREADY}}'] .= $s; {
$this->aReplace['{{JSONREADY}}'] .= $s;
return true;
} }
/** /**
...@@ -118,67 +128,78 @@ class Page { ...@@ -118,67 +128,78 @@ class Page {
* @param string $s * @param string $s
* @return boolean * @return boolean
*/ */
public function addResponseHeader($s) { public function addResponseHeader(string $s)
return $this->aResponseHeader[] = $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 * @param string $s html code
* @return boolean * @return boolean
*/ */
public function setContent($s) { public function setContent(string $s): bool
return $this->aReplace['{{CONTENT}}'] = $s; {
$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 * @param string $s html code
* @return boolean * @return boolean
*/ */
public function setFooter($s) { public function setFooter(string $s): bool
return $this->aReplace['{{FOOTER}}'] = $s; {
$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 * @param string $s html code
* @return boolean * @return boolean
*/ */
public function setHeader($s) { public function setHeader(string $s): bool
return $this->aReplace['{{HEADER}}'] = $s; {
$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 * @param string $s javascript code
* @return boolean * @return boolean
*/ */
public function setJsOnReady($s) { public function setJsOnReady(string $s): bool
return $this->aReplace['{{JSONREADY}}'] = $s; {
$this->aReplace['{{JSONREADY}}'] = $s;
return true;
} }
/** /**
* set output type of response * Set output type of response
* @param string $sOutputType * @param string $sOutputType
* @return boolean * @return boolean
*/ */
public function setOutputtype($sOutputType = 'html') { public function setOutputtype(string $sOutputType = 'html'): bool
return $this->sType = $sOutputType; {
$this->sType = $sOutputType;
return true;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// OUTPUT // OUTPUT
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* send http reponse headers and built the response body * Send http reponse headers and built the response body
* @return type * @return string
*/ */
public function render() { public function render(): string
$aS = array(); // search {
$aR = array(); // replace $aS = []; // search
$aR = []; // replace
$this->_finalizeJsOnReady(); $this->_finalizeJsOnReady();
...@@ -190,12 +211,12 @@ class Page { ...@@ -190,12 +211,12 @@ class Page {
$sTemplate = false; $sTemplate = false;
$sTplFile = dirname(__FILE__) . "/" . $this->sType . ".tpl.php"; $sTplFile = dirname(__FILE__) . "/" . $this->sType . ".tpl.php";
if (!file_exists($sTplFile)) { 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); $sTemplate = file_get_contents($sTplFile);
if (!$sTemplate) { 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) { foreach ($this->aResponseHeader as $sHeader) {
...@@ -205,5 +226,3 @@ class Page { ...@@ -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