Commit 4144837f authored by Michael Ochmann's avatar Michael Ochmann
Browse files

implemented plugin system and first example plugin, which now provides

the "title" functionality
parent 092833cb
/*!
* Font Awesome Free 5.8.1 by @fontawesome - https://fontawesome.com
* License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)
*/
@import '../../Teamprojekt SLCMS/fontawesome-free-5.8.1-web/scss/variables';
@font-face {
font-family: 'Font Awesome 5 Free';
font-style: normal;
font-weight: 900;
font-display: $fa-font-display;
src: url('#{$fa-font-path}/fa-solid-900.eot');
src: url('#{$fa-font-path}/fa-solid-900.eot?#iefix') format('embedded-opentype'),
url('#{$fa-font-path}/fa-solid-900.woff2') format('woff2'),
url('#{$fa-font-path}/fa-solid-900.woff') format('woff'),
url('#{$fa-font-path}/fa-solid-900.ttf') format('truetype'),
url('#{$fa-font-path}/fa-solid-900.svg#fontawesome') format('svg');
}
.fa,
.fas {
font-family: 'Font Awesome 5 Free';
font-weight: 900;
}
<?php
namespace DynamicTitle;
use slcms\plugins\Plugin;
class DynamicTitle extends Plugin {
const DEFAULT_TITLE = "Suckless CMS";
protected function init() {
$this->utils->registerSimplePlaceholder("title", $this->title());
$this->utils->registerComplexPlaceholder("subtitle", [&$this, "subtitle"]);
}
private function title() : string {
$titleFile = __DIR__."/../webcontent/.title";
if (file_exists($titleFile))
return (trim(strip_tags(file_get_contents($titleFile))));
return self::DEFAULT_TITLE;
}
public function subtitle($model) {
return $model->path === "" ? "Startseite" : \slcms\Std::getToplevelFromPath($model->path);
}
}
......@@ -13,6 +13,8 @@ use slcms\models\Page;
* @autor Michael Ochmann
*/
class Application {
private $utils;
private $pluginManager;
private $dirTree;
private $renderer;
private $page;
......@@ -20,10 +22,12 @@ class Application {
// builds the frame and puts out the time which was needed for
public function __construct() {
$this->startTime = microtime(true); // sets startTime on current Time
$this->dirTree = new models\Tree("/".$_GET["path"]);
$this->page = new Page($_GET["path"], $this->dirTree);
$this->renderer = new Renderer($this->page);
$this->utils = new plugins\Utils();
$this->pluginManager = new plugins\PluginManager($this->utils);
$this->startTime = microtime(true); // sets startTime on current Time
$this->dirTree = new models\Tree("/".$_GET["path"]);
$this->page = new Page($_GET["path"], $this->dirTree, $this->utils);
$this->renderer = new Renderer($this->page);
$this->renderer->render();
$microseconds = microtime(true) - $this->startTime; // counts microseconds until rendering is finished
......
......@@ -27,11 +27,12 @@ class Page implements Model {
public $texts;
public $others;
public $subtree;
private $path;
public $path;
private $stats;
private $subtitle;
private $utils;
public function __construct(string $path, Tree &$pageTree) {
public function __construct(string $path, Tree &$pageTree, \slcms\plugins\Utils &$utils) {
$this->utils = $utils;
$this->images = new SegmentedList();
$this->texts = new SegmentedList();
$this->others = new SegmentedList();
......@@ -41,6 +42,14 @@ class Page implements Model {
$this->subtree = $pageTree->subtree();
$this->stats = new PageStats();
$this->curateContents();
foreach ($utils->simplePlaceholders() as $key => $value) {
$this->$key = $value;
}
foreach ($utils->placeholders() as $key => $callback) {
$this->$key = $callback($this);
}
}
private function curateContents() : void {
......@@ -94,18 +103,6 @@ class Page implements Model {
return count($this->texts);
}
public function title() : string {
$titleFile = __DIR__."/../../webcontent/.title";
if (file_exists($titleFile))
return (trim(strip_tags(file_get_contents($titleFile))));
// default name if there's no .title file
return "Suckless CMS";
}
public function subtitle() {
return $this->path === "" ? "Startseite" : \slcms\Std::getToplevelFromPath($this->path);
}
// getter for PageStats
public function maxImageWidth() : int {
......
<?php
namespace slcms\plugins;
abstract class Plugin {
protected $utils;
public function __construct(\slcms\plugins\Utils &$utils) {
$this->utils = $utils;
$this->init();
}
abstract protected function init();
}
<?php declare(strict_types=1);
namespace slcms\plugins;
class PluginManager {
private $utils;
private $pluginsLoaded;
private $plugins;
public function __construct(Utils &$utils) {
$this->utils = $utils;
$this->pluginsLoaded = [];
$this->plugins = [];
spl_autoload_register([&$this, "autoload"]);
$this->init();
}
private function init() {
$dir = dirname(__FILE__)."/../../plugins";
foreach (scandir($dir) as $plugin) {
if (!is_dir($dir."/".$plugin) || $plugin === "." ||$plugin === "..")
continue;
$class = "\\$plugin\\$plugin";
array_push($this->plugins, new $class($this->utils));
}
}
public function autoload(string $className) : void {
$pluginID = $className;
$className = dirname(__FILE__)."/../../plugins/".str_replace("\\", "/", $className).".php";
if (file_exists($className)) {
require_once($className);
array_push($this->pluginsLoaded, $pluginID);
}
}
public function pluginsLoaded() : array {
return $this->pluginsLoaded;
}
}
<?php declare(strict_types=1);
namespace slcms\plugins {
class Utils {
private $simplePlaceholders;
private $placeholders;
public function __construct() {
$this->placeholders = [];
$this->simplePlaceholders = [];
}
public function simplePlaceholders() : array {
return $this->simplePlaceholders;
}
public function placeholders() : array {
return $this->placeholders;
}
public function registerSimplePlaceholder(string $identifier, $value) : bool {
if (array_key_exists($identifier, $this->simplePlaceholders) || array_key_exists($identifier, $this->placeholders))
return false;
$this->simplePlaceholders[$identifier] = $value;
return true;
}
public function registerComplexPlaceholder(string $identifier, callable $callback) : bool {
if (array_key_exists($identifier, $this->simplePlaceholders) || array_key_exists($identifier, $this->placeholders))
return false;
$this->placeholders[$identifier] = $callback;
return true;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment