aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/functions.php90
-rw-r--r--core/namespace/Application.php10
-rw-r--r--core/namespace/Parsers/FunctionParser.php19
3 files changed, 66 insertions, 53 deletions
diff --git a/core/functions.php b/core/functions.php
index 15a5318..2155d96 100644
--- a/core/functions.php
+++ b/core/functions.php
@@ -101,10 +101,6 @@ function generateItemTemplateData(EntityInterface $Entity): array {
$FunctionParser = new FunctionParser;
$MarkdownParser = new MarkdownParser;
- $FunctionParser->registerFromArray(
- Application::getContentFunctions()
- );
-
$attribute = $Entity->getAll(['password']);
$attribute = array_change_key_case($attribute, CASE_UPPER);
@@ -163,11 +159,7 @@ function generateCategoryDataTree(array $category_data, $root = 0): array {
function parseEntityContent(EntityInterface $Entity): string {
$text = $Entity->get('body');
- $FunctionParser = new FunctionParser;
- $FunctionParser->registerFromArray(
- Application::getContentFunctions()
- );
-
+ $FunctionParser = new FunctionParser();
$text = $FunctionParser->transform($text);
if(Application::get('WRAP_EMOTICONS')) {
@@ -379,33 +371,71 @@ function USER(int $id): array {
}
#===========================================================================
-# Register (BAS|FIL)E_URL content functions
+# Get base URL (optionally extended by $extend)
#===========================================================================
-Application::addContentFunction('BASE_URL',
-function($extend = '') {
+FunctionParser::register('BASE_URL', function($extend = '') {
return Application::getURL($extend);
});
-Application::addContentFunction('FILE_URL',
-function($extend = '') {
+#===========================================================================
+# Get file URL (optionally extended by $extend)
+#===========================================================================
+FunctionParser::register('FILE_URL', function($extend = '') {
return Application::getFileURL($extend);
});
#===========================================================================
-# Register (CATEGORY|PAGE|POST|USER)(_URL)? content functions
+# Get Markdown formatted *category* link
#===========================================================================
-foreach(['CATEGORY', 'PAGE', 'POST', 'USER'] as $function) {
- $entity = ucfirst(strtolower($function));
-
- // Get Markdown formatted entity link
- Application::addContentFunction($function,
- function($id, $text = NULL, $title = NULL) use($entity) {
- return getEntityMarkdownLink($entity, $id, $text, $title);
- });
-
- // Get URL to entity
- Application::addContentFunction(sprintf('%s_URL', $function),
- function($id) use($entity) {
- return getEntityURL($entity, $id);
- });
-}
+FunctionParser::register('CATEGORY', function($id, $text = NULL, $title = NULL) {
+ return getEntityMarkdownLink('Category', $id, $text, $title);
+});
+
+#===========================================================================
+# Get Markdown formatted *page* link
+#===========================================================================
+FunctionParser::register('PAGE', function($id, $text = NULL, $title = NULL) {
+ return getEntityMarkdownLink('Page', $id, $text, $title);
+});
+
+#===========================================================================
+# Get Markdown formatted *post* link
+#===========================================================================
+FunctionParser::register('POST', function($id, $text = NULL, $title = NULL) {
+ return getEntityMarkdownLink('Post', $id, $text, $title);
+});
+
+#===========================================================================
+# Get Markdown formatted *user* link
+#===========================================================================
+FunctionParser::register('USER', function($id, $text = NULL, $title = NULL) {
+ return getEntityMarkdownLink('User', $id, $text, $title);
+});
+
+#===========================================================================
+# Get URL to a category entity
+#===========================================================================
+FunctionParser::register('CATEGORY_URL', function($id) {
+ return getEntityURL('Category', $id);
+});
+
+#===========================================================================
+# Get URL to a page entity
+#===========================================================================
+FunctionParser::register('PAGE_URL', function($id) {
+ return getEntityURL('Page', $id);
+});
+
+#===========================================================================
+# Get URL to a post entity
+#===========================================================================
+FunctionParser::register('POST_URL', function($id) {
+ return getEntityURL('Post', $id);
+});
+
+#===========================================================================
+# Get URL to a user entity
+#===========================================================================
+FunctionParser::register('USER_URL', function($id) {
+ return getEntityURL('User', $id);
+});
diff --git a/core/namespace/Application.php b/core/namespace/Application.php
index 1f1e6f6..8346248 100644
--- a/core/namespace/Application.php
+++ b/core/namespace/Application.php
@@ -11,7 +11,6 @@ class Application {
private static $Language;
private static $Migrator;
private static $repositories = [];
- private static $contentFunctions = [];
#===============================================================================
# Configuration array
@@ -224,14 +223,7 @@ class Application {
contain only numbers, uppercase letters and underscores!');
}
- self::$contentFunctions[$name] = $callback;
- }
-
- #===============================================================================
- # Return all known content functions
- #===============================================================================
- public static function getContentFunctions(): array {
- return self::$contentFunctions;
+ FunctionParser::register($name, $callback);
}
#===============================================================================
diff --git a/core/namespace/Parsers/FunctionParser.php b/core/namespace/Parsers/FunctionParser.php
index 94a4c92..02570a0 100644
--- a/core/namespace/Parsers/FunctionParser.php
+++ b/core/namespace/Parsers/FunctionParser.php
@@ -3,7 +3,7 @@ namespace Parsers;
use ReflectionFunction;
class FunctionParser implements ParserInterface {
- private $functions = [];
+ private static $functions = [];
#===============================================================================
# Main regex for matching the whole function call
@@ -39,28 +39,19 @@ class FunctionParser implements ParserInterface {
#===============================================================================
# Register function
#===============================================================================
- public function register(string $name, callable $callback): void {
+ public static function register(string $name, callable $callback): void {
$Function = new ReflectionFunction($callback);
- $this->functions[$name] = [
+ self::$functions[$name] = [
'callback' => $callback,
'required' => $Function->getNumberOfRequiredParameters()
];
}
#===============================================================================
- # Register multiple functions from array
- #===============================================================================
- public function registerFromArray(array $functions): void {
- foreach($functions as $name => $callback) {
- $this->register($name, $callback);
- }
- }
-
- #===============================================================================
# Parse functions
#===============================================================================
public function parse(string $text): array {
- $functionNames = array_keys($this->functions);
+ $functionNames = array_keys(self::$functions);
$functionNames = implode('|', $functionNames);
$pattern = self::FUNCTION_PATTERN;
@@ -81,7 +72,7 @@ class FunctionParser implements ParserInterface {
# Transform functions
#===============================================================================
public function transform(string $text): string {
- $functionData = $this->functions;
+ $functionData = self::$functions;
$functionNames = array_keys($functionData);
$functionNames = implode('|', $functionNames);