From e35dac85aefe9aa0014d6bf85fe1fe70110bb330 Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 16 Mar 2018 18:18:27 +0300 Subject: [PATCH] Back to basics! Totally Simplify Engine! #5 --- rawilum/Cache.php | 96 +++++++++++++-------------- rawilum/Config.php | 58 ++++++++++++---- rawilum/Events.php | 26 ++++---- rawilum/Filters.php | 36 +++++----- rawilum/I18n.php | 67 ++++++++++++------- rawilum/Markdown.php | 42 ++++++++++++ rawilum/Pages.php | 90 +++++++++++++++---------- rawilum/Plugins.php | 39 ++++++----- rawilum/Rawilum.php | 92 ++++++++++++++++--------- rawilum/RawilumTrait.php | 39 ----------- rawilum/Shortcodes.php | 42 ++++++------ rawilum/{Themes.php => Templates.php} | 22 +++--- 12 files changed, 373 insertions(+), 276 deletions(-) create mode 100644 rawilum/Markdown.php delete mode 100755 rawilum/RawilumTrait.php rename rawilum/{Themes.php => Templates.php} (58%) diff --git a/rawilum/Cache.php b/rawilum/Cache.php index 6234bd9d..c2c6cb24 100755 --- a/rawilum/Cache.php +++ b/rawilum/Cache.php @@ -13,73 +13,69 @@ class Cache { /** - * @var Rawilum + * An instance of the Cache class + * + * @var object */ - protected $rawilum; - + protected static $instance = null; /** * Unique cache key * * @var string Cache key. */ protected static $key; - /** * Lifetime * * @var int Lifetime. */ protected static $lifetime; - /** * Current time * * @var int Current time. */ protected static $now; - /** * Cache Driver * * @var DoctrineCache */ protected static $driver; - + /** + * Protected clone method to enforce singleton behavior. + * + * @access protected + */ + protected function __clone() + { + // Nothing here. + } /** * Constructor. * * @access protected */ - public function __construct(Rawilum $c) + protected function __construct() { - $this->rawilum = $c; - // Set current time static::$now = time(); - // Cache key allows us to invalidate all cache on configuration changes. - static::$key = ($this->rawilum['config']->get('site.cache.prefix') ? $this->rawilum['config']->get('site.cache.prefix') : 'rawilum') . '-' . md5(ROOT_DIR . 'Rawilum::VERSION'); - + static::$key = (Config::get('site.cache.prefix') ? Config::get('site.cache.prefix') : 'fansoro') . '-' . md5(ROOT_DIR . 'Fansoro::VERSION'); // Get Cache Driver - static::$driver = $this->getCacheDriver(); - + static::$driver = static::getCacheDriver(); // Set the cache namespace to our unique key static::$driver->setNamespace(static::$key); - - // Return - return static::$driver; } - /** * Get Cache Driver * * @access public * @return object */ - public function getCacheDriver() + public static function getCacheDriver() { - $driver_name = $this->rawilum['config']->get('site.cache.driver'); - + $driver_name = Config::get('site.cache.driver'); if (!$driver_name || $driver_name == 'auto') { if (extension_loaded('apc')) { $driver_name = 'apc'; @@ -91,7 +87,6 @@ class Cache } else { $driver_name = 'file'; } - switch ($driver_name) { case 'apc': $driver = new \Doctrine\Common\Cache\ApcCache(); @@ -104,29 +99,24 @@ class Cache break; case 'memcache': $memcache = new \Memcache(); - $memcache->connect( - $this->rawilum['config']->get('site.cache.memcache.server', 'localhost'), - $this->rawilum['config']->get('site.cache.memcache.port', 11211) - ); + $memcache->connect(Config::get('site.cache.memcache.server', 'localhost'), + Config::get('site.cache.memcache.port', 11211)); $driver = new \Doctrine\Common\Cache\MemcacheCache(); $driver->setMemcache($memcache); break; case 'redis': $redis = new \Redis(); - $redis->connect( - $this->rawilum['config']->get('site.cache.redis.server', 'localhost'), - $this->rawilum['config']->get('site.cache.redis.port', 6379) - ); + $redis->connect(Config::get('site.cache.redis.server', 'localhost'), + Config::get('site.cache.redis.port', 6379)); $driver = new \Doctrine\Common\Cache\RedisCache(); $driver->setRedis($redis); break; default: // Create doctrine cache directory if its not exists - !$this->rawilum['filesystem']->exists($cache_directory = CACHE_PATH . '/doctrine/') and $this->rawilum['filesystem']->mkdir($cache_directory); + !Rawilum::$filesystem->exists($cache_directory = CACHE_PATH . '/doctrine/') and Rawilum::$filesystem->mkdir($cache_directory); $driver = new \Doctrine\Common\Cache\FilesystemCache($cache_directory); break; } - return $driver; } @@ -136,22 +126,20 @@ class Cache * @access public * @return object */ - public function driver() + public static function driver() { return static::$driver; } - /** * Get cache key. * * @access public * @return string */ - public function getKey() + public static function getKey() { return static::$key; } - /** * Fetches an entry from the cache. * @@ -161,13 +149,12 @@ class Cache */ public function fetch($id) { - if ($this->rawilum['config']->get('site.cache.enabled')) { + if (Config::get('site.cache.enabled')) { return static::$driver->fetch($id); } else { return false; } } - /** * Puts data into the cache. * @@ -180,52 +167,61 @@ class Cache */ public function save($id, $data, $lifetime = null) { - if ($this->rawilum['config']->get('site.cache.enabled')) { + if (Config::get('site.cache.enabled')) { if ($lifetime === null) { $lifetime = static::getLifetime(); } static::$driver->save($id, $data, $lifetime); } } - /** * Clear Cache */ - public function clear() + public static function clear() { - $this->rawilum['filesystem']->remove(CACHE_PATH . '/doctrine/'); + Rawilum::$filesystem->remove(CACHE_PATH . '/doctrine/'); } - /** * Set the cache lifetime. * * @access public * @param int $future timestamp */ - public function setLifetime($future) + public static function setLifetime($future) { if (!$future) { return; } - $interval = $future - $this->now; - if ($interval > 0 && $interval < static::getLifetime()) { static::$lifetime = $interval; } } - /** * Retrieve the cache lifetime (in seconds) * * @access public * @return mixed */ - public function getLifetime() + public static function getLifetime() { if (static::$lifetime === null) { - static::$lifetime = $this->rawilum['config']->get('site.cache.lifetime') ?: 604800; + static::$lifetime = Config::get('site.cache.lifetime') ?: 604800; } return static::$lifetime; } + /** + * Initialize Fansoro Cache + * + * + * Cache::init(); + * + * + * @access public + * @return object + */ + public static function init() + { + return !isset(self::$instance) and self::$instance = new Cache(); + } } diff --git a/rawilum/Config.php b/rawilum/Config.php index 247e674f..e1847ef6 100755 --- a/rawilum/Config.php +++ b/rawilum/Config.php @@ -15,10 +15,14 @@ use Symfony\Component\Yaml\Yaml; class Config { + /** - * @var Rawilum + * An instance of the Config class + * + * @var object + * @access protected */ - protected $rawilum; + protected static $instance = null; /** * Config @@ -26,19 +30,27 @@ class Config * @var array * @access protected */ - protected $config = []; + protected static $config = []; + + /** + * Protected clone method to enforce singleton behavior. + * + * @access protected + */ + protected function __clone() + { + // Nothing here. + } /** * Constructor. * * @access protected */ - public function __construct(Rawilum $c) + protected function __construct() { - $this->rawilum = $c; - - if ($this->rawilum['filesystem']->exists($site_config = CONFIG_PATH . '/' . 'site.yml')) { - $this->config['site'] = Yaml::parse(file_get_contents($site_config)); + if (Rawilum::$filesystem->exists($site_config = CONFIG_PATH . '/' . 'site.yml')) { + static::$config['site'] = Yaml::parse(file_get_contents($site_config)); } else { throw new RuntimeException("Rawilum site config file does not exist."); } @@ -51,9 +63,9 @@ class Config * @param string $key Key * @param mixed $value Value */ - public function set($key, $value) + public static function set($key, $value) { - Arr::set($this->config, $key, $value); + Arr::set(static::$config, $key, $value); } /** @@ -64,19 +76,37 @@ class Config * @param mixed $default Default value * @return mixed */ - public function get($key, $default = null) + public static function get($key, $default = null) { - return Arr::get($this->config, $key, $default); + return Arr::get(static::$config, $key, $default); } /** * Get config array * + * + * $config = Config::getConfig(); + * + * * @access public * @return array */ - public function getConfig() + public static function getConfig() { - return $this->config; + return static::$config; + } + + /** + * Initialize Rawilum Config + * + * + * Config::init(); + * + * + * @access public + */ + public static function init() + { + return !isset(self::$instance) and self::$instance = new Config(); } } diff --git a/rawilum/Events.php b/rawilum/Events.php index 18f06a12..7ad8cdd5 100644 --- a/rawilum/Events.php +++ b/rawilum/Events.php @@ -14,10 +14,6 @@ use Arr; class Events { - /** - * @var Rawilum - */ - protected $rawilum; /** * Events @@ -25,14 +21,16 @@ class Events * @var array * @access protected */ - protected $events = []; + protected static $events = []; /** - * Construct + * Protected constructor since this is a static class. + * + * @access protected */ - public function __construct(Rawilum $c) + protected function __construct() { - $this->rawilum = $c; + // Nothing here } /** @@ -44,11 +42,11 @@ class Events * @param integer $priority Priority. Default is 10 * @param array $args Arguments */ - public function addListener(string $event_name, $added_function, int $priority = 10, array $args = null) + public static function addListener(string $event_name, $added_function, int $priority = 10, array $args = null) { // Hooks a function on to a specific event. - $this->events[] = array( - 'event_name' => $event_name, + static::$events[] = array( + 'event_name' => $event_name, 'function' => $added_function, 'priority' => $priority, 'args' => $args @@ -64,17 +62,17 @@ class Events * @param boolean $return Return data or not. Default is false * @return mixed */ - public function dispatch(string $event_name, array $args = [], bool $return = false) + public static function dispatch(string $event_name, array $args = [], bool $return = false) { // Redefine arguments $event_name = $event_name; $return = $return; // Run event - if (count($this->events) > 0) { + if (count(static::$events) > 0) { // Sort actions by priority - $events = Arr::subvalSort($this->events, 'priority'); + $events = Arr::subvalSort(static::$events, 'priority'); // Loop through $events array foreach ($events as $action) { diff --git a/rawilum/Filters.php b/rawilum/Filters.php index 64907335..561c6016 100755 --- a/rawilum/Filters.php +++ b/rawilum/Filters.php @@ -24,21 +24,24 @@ class Filters * @var array * @access protected */ - protected $filters = []; + protected static $filters = []; + /** - * Construct + * Protected constructor since this is a static class. + * + * @access protected */ - public function __construct(Rawilum $c) + protected function __construct() { - $this->rawilum = $c; + // Nothing here } /** - * Apply filters + * Dispatch filters * * - * Filter::apply('content', $content); + * Filter::dispatch('content', $content); * * * @access public @@ -46,18 +49,15 @@ class Filters * @param mixed $value The value on which the filters hooked. * @return mixed */ - public function dispatch($filter_name, $value) + public static function dispatch(string $filter_name, $value) { - // Redefine arguments - $filter_name = (string) $filter_name; - $args = array_slice(func_get_args(), 2); - if (! isset($this->filters[$filter_name])) { + if (! isset(static::$filters[$filter_name])) { return $value; } - foreach ($this->filters[$filter_name] as $priority => $functions) { + foreach (static::$filters[$filter_name] as $priority => $functions) { if (! is_null($functions)) { foreach ($functions as $function) { $all_args = array_merge(array($value), $args); @@ -98,8 +98,8 @@ class Filters * @param integer $accepted_args The number of arguments the function accept default is 1. * @return boolean */ - public function addListener($filter_name, $function_to_add, $priority = 10, $accepted_args = 1) - { + public static function addListener($filter_name, $function_to_add, $priority = 10, $accepted_args = 1) + { // Redefine arguments $filter_name = (string) $filter_name; $function_to_add = $function_to_add; @@ -107,18 +107,18 @@ class Filters $accepted_args = (int) $accepted_args; // Check that we don't already have the same filter at the same priority. Thanks to WP :) - if (isset($this->filters[$filter_name]["$priority"])) { - foreach ($this->filters[$filter_name]["$priority"] as $filter) { + if (isset(static::$filters[$filter_name]["$priority"])) { + foreach (static::$filters[$filter_name]["$priority"] as $filter) { if ($filter['function'] == $function_to_add) { return true; } } } - $this->filters[$filter_name]["$priority"][] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); + static::$filters[$filter_name]["$priority"][] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); // Sort - ksort($this->filters[$filter_name]["$priority"]); + ksort(static::$filters[$filter_name]["$priority"]); return true; } diff --git a/rawilum/I18n.php b/rawilum/I18n.php index 87b2e852..591ca297 100644 --- a/rawilum/I18n.php +++ b/rawilum/I18n.php @@ -14,12 +14,19 @@ use Symfony\Component\Yaml\Yaml; class I18n { + /** + * An instance of the Cache class + * + * @var object + */ + protected static $instance = null; + /** * Locales array * * @var array */ - public $locales = array( + public static $locales = [ 'ar' => 'العربية', 'bg' => 'Български', 'ca' => 'Català', @@ -52,38 +59,35 @@ class I18n 'tr' => 'Türkçe', 'uk' => 'Українська', 'zh-cn' => '简体中文', - ); - - /** - * @var Rawilum - */ - protected $rawilum; + ]; /** * Dictionary * * @var array */ - public $dictionary = array(); + public static $dictionary = []; + + /** + * Protected clone method to enforce singleton behavior. + * + * @access protected + */ + protected function __clone() + { + // Nothing here. + } /** * Construct */ - public function __construct(Rawilum $c) - { - $this->rawilum = $c; - } - - /** - * Init - */ - public function init() + protected function __construct() { // Get Plugins and Site Locales list - (array) $plugins_list = $this->rawilum['config']->get('site.plugins'); - (array) $locales = $this->rawilum['config']->get('site.locales'); - (array) $dictionary = []; + (array) $plugins_list = Config::get('site.plugins'); + (array) $locales = Config::get('site.locales'); + (array) $dictionary = []; // Create dictionary if (is_array($plugins_list) && count($plugins_list) > 0) { @@ -98,7 +102,7 @@ class I18n } // Save dictionary - $this->dictionary = $dictionary; + static::$dictionary = $dictionary; } /** @@ -110,11 +114,11 @@ class I18n * @param string $locale Locale * @return string */ - public function find(string $string, string $namespace, string $locale, array $values = []) : string + public static function find(string $string, string $namespace, string $locale, array $values = []) : string { // Search current string to translate in the Dictionary - if (isset($this->dictionary[$namespace][$locale][$string])) { - $string = $this->dictionary[$namespace][$locale][$string]; + if (isset(static::$dictionary[$namespace][$locale][$string])) { + $string = static::$dictionary[$namespace][$locale][$string]; $string = empty($values) ? $string : strtr($string, $values); } else { $string = $string; @@ -123,4 +127,19 @@ class I18n // Return translation of a string return $string; } + + /** + * Initialize Rawilum I18n + * + * + * I18n::init(); + * + * + * @access public + * @return object + */ + public static function init() + { + return !isset(self::$instance) and self::$instance = new I18n(); + } } diff --git a/rawilum/Markdown.php b/rawilum/Markdown.php new file mode 100644 index 00000000..7f01a3e4 --- /dev/null +++ b/rawilum/Markdown.php @@ -0,0 +1,42 @@ + + * @link http://rawilum.org + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +class Markdown +{ + /** + * Parsedown Extra Object + * + * @var object + * @access protected + */ + protected static $markdown; + + /** + * Markdown parser + * + * + * $content = Markdown::parse($content); + * + * + * @access public + * @param string $content Content to parse + * @return string Formatted content + */ + public static function parse($content) + { + !static::$markdown and static::$markdown = new ParsedownExtra(); + + return static::$markdown->text($content); + } +} diff --git a/rawilum/Pages.php b/rawilum/Pages.php index 2eec55e7..612941ff 100755 --- a/rawilum/Pages.php +++ b/rawilum/Pages.php @@ -19,29 +19,38 @@ use Symfony\Component\Yaml\Yaml; class Pages { /** - * @var Rawilum + * An instance of the Cache class + * + * @var object */ - protected $rawilum; + protected static $instance = null; /** * @var Page */ - public $page; + public static $page; /** * Constructor * * @param Rawilum $rawilum */ - public function __construct(Rawilum $c) + protected function __construct() { - $this->rawilum = $c; + // The page is not processed and not sent to the display. + Events::dispatch('onPageBeforeRender'); - // Register page shortcodes - $this->pageShortcodes(); + // Get current page + static::$page = static::getPage(Url::getUriString()); + + // Display page for current requested url + static::renderPage(static::$page); + + // The page has been fully processed and sent to the display. + Events::dispatch('onPageAfterRender'); } - protected function pageShortcodes() { + protected static function pageShortcodes() { // {site_url} $this->rawilum['shortcodes']->add('site_url', function() { return Url::getBase(); @@ -51,7 +60,7 @@ class Pages /** * Page finder */ - public function finder($url = '', $url_abs = false) + public static function finder($url = '', $url_abs = false) { // If url is empty that its a homepage @@ -59,18 +68,18 @@ class Pages if ($url) { $file = $url; } else { - $file = PAGES_PATH . '/' . $this->rawilum['config']->get('site.pages.main') . '/' . 'index.md'; + $file = PAGES_PATH . '/' . Config::get('site.pages.main') . '/' . 'index.md'; } } else { if ($url) { $file = PAGES_PATH . '/' . $url . '/index.md'; } else { - $file = PAGES_PATH . '/' . $this->rawilum['config']->get('site.pages.main') . '/' . 'index.md'; + $file = PAGES_PATH . '/' . Config::get('site.pages.main') . '/' . 'index.md'; } } // Get 404 page if file not exists - if ($this->rawilum['filesystem']->exists($file)) { + if (Rawilum::$filesystem->exists($file)) { $file = $file; } else { $file = PAGES_PATH . '/404/index.md'; @@ -83,16 +92,14 @@ class Pages /** * Render page */ - public function renderPage() + public static function renderPage($page) { - $page = $this->page ; - $template_ext = '.php'; $template_name = empty($page['template']) ? 'index' : $page['template']; - $site_theme = $this->rawilum['config']->get('site.theme'); + $site_theme = Config::get('site.theme'); $template_path = THEMES_PATH . '/' . $site_theme . '/' . $template_name . $template_ext; - if ($this->rawilum['filesystem']->exists($template_path)) { + if (Rawilum::$filesystem->exists($template_path)) { include $template_path; } else { throw new RuntimeException("Template {$template_name} does not exist."); @@ -102,12 +109,12 @@ class Pages /** * Page parser */ - public function parse($file) + public static function parse($file) { $page = trim(file_get_contents($file)); $page = explode('---', $page, 3); - $frontmatter = $this->rawilum['shortcodes']->parse($page[1]); + $frontmatter = Shortcodes::parse($page[1]); $result_page = Yaml::parse($frontmatter); $result_page['content'] = $page[2]; @@ -118,28 +125,28 @@ class Pages /** * Get page */ - public function getPage($url = '', $raw = false, $url_abs = false) + public static function getPage($url = '', $raw = false, $url_abs = false) { - $file = $this->finder($url, $url_abs); + $file = static::finder($url, $url_abs); if ($raw) { $page = trim(file_get_contents($file)); - $this->page = $page; - $this->rawilum['events']->dispatch('onPageContentRawAfter'); + static::$page = $page; + Events::dispatch('onPageContentRawAfter'); } else { - $page = $this->parse($file); - $this->page = $page; - $this->page['content'] = $this->rawilum['filters']->dispatch('content', $this->parseContent($this->page['content'])); - $this->rawilum['events']->dispatch('onPageContentAfter'); + $page = static::parse($file); + static::$page = $page; + static::$page['content'] = Filters::dispatch('content', static::parseContent(static::$page['content'])); + Events::dispatch('onPageContentAfter'); } - return $this->page; + return static::$page; } - public function parseContent($content) + public static function parseContent($content) { - $content = $this->rawilum['shortcodes']->parse($content); - $content = $this->rawilum['markdown']->text($content); + $content = Shortcodes::parse($content); + $content = Markdown::parse($content); return $content; } @@ -147,14 +154,14 @@ class Pages /** * getPage */ - public function getPages($url = '', $raw = false, $order_by = 'title', $order_type = 'DESC', $ignore = ['404', 'index'], $limit = null) + public static function getPages($url = '', $raw = false, $order_by = 'title', $order_type = 'DESC', $ignore = ['404', 'index'], $limit = null) { // Get pages list for current $url - $pages_list = $this->rawilum['finder']->files()->name('*.md')->in(PAGES_PATH . '/' . $url); + $pages_list = Rawilum::$finder->files()->name('*.md')->in(PAGES_PATH . '/' . $url); // Go trough pages list foreach ($pages_list as $key => $page) { - $pages[$key] = $this->getPage($page->getPathname(), $raw, true); + $pages[$key] = static::getPage($page->getPathname(), $raw, true); } // Sort and Slice pages if !$raw @@ -168,4 +175,19 @@ class Pages return $pages; } + + /** + * Initialize Rawilum Pages + * + * + * Pages::init(); + * + * + * @access public + * @return object + */ + public static function init() + { + return !isset(self::$instance) and self::$instance = new Pages(); + } } diff --git a/rawilum/Plugins.php b/rawilum/Plugins.php index 9d7013ec..93748948 100755 --- a/rawilum/Plugins.php +++ b/rawilum/Plugins.php @@ -15,17 +15,11 @@ use Symfony\Component\Yaml\Yaml; class Plugins { /** - * @var Rawilum + * An instance of the Cache class + * + * @var object */ - protected $rawilum; - - /** - * __construct - */ - public function __construct(Rawilum $c) - { - $this->rawilum = $c; - } + protected static $instance = null; /** * Init Plugins @@ -33,13 +27,13 @@ class Plugins * @access public * @return mixed */ - public function init() + protected function __construct() { // Plugin manifest $plugin_manifest = []; // Get Plugins List - $plugins_list = $this->rawilum['config']->get('site.plugins'); + $plugins_list = Config::get('site.plugins'); // If Plugins List isnt empty if (is_array($plugins_list) && count($plugins_list) > 0) { @@ -54,12 +48,25 @@ class Plugins } } - $rawilum = $this->rawilum; - - if (is_array($this->rawilum['config']->get('site.plugins')) && count($this->rawilum['config']->get('site.plugins')) > 0) { - foreach ($this->rawilum['config']->get('site.plugins') as $plugin_id => $plugin_name) { + if (is_array(Config::get('site.plugins')) && count(Config::get('site.plugins')) > 0) { + foreach (Config::get('site.plugins') as $plugin_id => $plugin_name) { include_once PLUGINS_PATH .'/'. $plugin_name .'/'. $plugin_name . '.php'; } } } + + /** + * Initialize Rawilum I18n + * + * + * Plugins::init(); + * + * + * @access public + * @return object + */ + public static function init() + { + return !isset(self::$instance) and self::$instance = new Plugins(); + } } diff --git a/rawilum/Rawilum.php b/rawilum/Rawilum.php index 556202cd..9564b9e8 100755 --- a/rawilum/Rawilum.php +++ b/rawilum/Rawilum.php @@ -1,10 +1,9 @@ get('site.errors.display')) { + if (Config::get('site.errors.display')) { define('DEVELOPMENT', true); error_reporting(-1); } else { @@ -137,8 +157,8 @@ class Rawilum extends Container // Set internal encoding function_exists('mb_language') and mb_language('uni'); - function_exists('mb_regex_encoding') and mb_regex_encoding($this['config']->get('site.charset')); - function_exists('mb_internal_encoding') and mb_internal_encoding($this['config']->get('site.charset')); + function_exists('mb_regex_encoding') and mb_regex_encoding(Config::get('site.charset')); + function_exists('mb_internal_encoding') and mb_internal_encoding(Config::get('site.charset')); // Set Error handler set_error_handler('ErrorHandler::error'); @@ -146,33 +166,39 @@ class Rawilum extends Container set_exception_handler('ErrorHandler::exception'); // Set default timezone - date_default_timezone_set($this['config']->get('site.timezone')); + date_default_timezone_set(Config::get('site.timezone')); - // The page is not processed and not sent to the display. - $this['events']->dispatch('onPageBeforeRender'); + // Start the session + Session::start(); + + // Init Cache + Cache::init(); + + // Init I18n + I18n::init(); + + // Init Plugins + Plugins::init(); // Render current page - $this['pages']->renderPage(); - - // The page has been fully processed and sent to the display. - $this['events']->dispatch('onPageAfterRender'); + Pages::init(); // Flush (send) the output buffer and turn off output buffering ob_end_flush(); } /** - * Get Rawilum Application Instance - * - * @access public - * @return object - */ - public static function instance() - { - if (!self::$instance) { - self::$instance = static::init(); - RawilumTrait::setRawilum(self::$instance); - } - return self::$instance; - } + * Initialize Rawilum Application + * + * + * Rawium::init(); + * + * + * @access public + * @return object + */ + public static function init() + { + return !isset(self::$instance) and self::$instance = new Rawilum(); + } } diff --git a/rawilum/RawilumTrait.php b/rawilum/RawilumTrait.php deleted file mode 100755 index dd0a3688..00000000 --- a/rawilum/RawilumTrait.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @link http://rawilum.org - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -trait RawilumTrait -{ - /** - * @var Rawilum - */ - protected static $rawilum; - - /** - * @return Rawilum - */ - public static function getRawilum() - { - if (!self::$rawilum) { - self::$rawilum = Rawilum::instance(); - } - - return self::$rawilum; - } - - /** - * @param Rawilum $rawilum - */ - public static function setRawilum(Rawilum $rawilum) - { - self::$rawilum = $rawilum; - } -} diff --git a/rawilum/Shortcodes.php b/rawilum/Shortcodes.php index de38320d..0898c2b7 100644 --- a/rawilum/Shortcodes.php +++ b/rawilum/Shortcodes.php @@ -12,24 +12,22 @@ class Shortcodes { - /** - * @var Rawilum - */ - protected $rawilum; /** * Shortcode tags array * * @var shortcode_tags */ - protected $shortcode_tags = []; + protected static $shortcode_tags = []; /** - * Construct + * Protected constructor since this is a static class. + * + * @access protected */ - public function __construct(Rawilum $c) + protected function __construct() { - $this->rawilum = $c; + // Nothing here } /** @@ -38,11 +36,11 @@ class Shortcodes * @param string $shortcode Shortcode tag to be searched in content. * @param string $callback_function The callback function to replace the shortcode with. */ - public function add(string $shortcode, $callback_function) + public static function add(string $shortcode, $callback_function) { // Add new shortcode if (is_callable($callback_function)) { - $this->shortcode_tags[$shortcode] = $callback_function; + static::$shortcode_tags[$shortcode] = $callback_function; } } @@ -51,11 +49,11 @@ class Shortcodes * * @param string $shortcode Shortcode tag. */ - public function delete(string $shortcode) + public static function delete(string $shortcode) { // Delete shortcode - if ($this->exists($shortcode)) { - unset($this->shortcode_tags[$shortcode]); + if (static::exists($shortcode)) { + unset(static::$shortcode_tags[$shortcode]); } } @@ -67,9 +65,9 @@ class Shortcodes * * */ - public function clear() + public static function clear() { - $this->shortcode_tags = array(); + static::$shortcode_tags = array(); } /** @@ -77,10 +75,10 @@ class Shortcodes * * @param string $shortcode Shortcode tag. */ - public function exists(string $shortcode) + public static function exists(string $shortcode) { // Check shortcode - return array_key_exists($shortcode, $this->shortcode_tags); + return array_key_exists($shortcode, static::$shortcode_tags); } /** @@ -89,13 +87,13 @@ class Shortcodes * @param string $content Content * @return string */ - public function parse(string $content) + public static function parse(string $content) { - if (! $this->shortcode_tags) { + if (! static::$shortcode_tags) { return $content; } - $shortcodes = implode('|', array_map('preg_quote', array_keys($this->shortcode_tags))); + $shortcodes = implode('|', array_map('preg_quote', array_keys(static::$shortcode_tags))); $pattern = "/(.?)\{([$shortcodes]+)(.*?)(\/)?\}(?(4)|(?:(.+?)\{\/\s*\\2\s*\}))?(.?)/s"; return preg_replace_callback($pattern, array($this, '_handle'), $content); @@ -104,7 +102,7 @@ class Shortcodes /** * _handle() */ - protected function _handle($matches) + protected static function _handle($matches) { $prefix = $matches[1]; $suffix = $matches[6]; @@ -128,6 +126,6 @@ class Shortcodes } // Check if this shortcode realy exists then call user function else return empty string - return (isset($this->shortcode_tags[$shortcode])) ? $prefix . call_user_func($this->shortcode_tags[$shortcode], $attributes, $matches[5], $shortcode) . $suffix : ''; + return (isset(static::$shortcode_tags[$shortcode])) ? $prefix . call_user_func(static::$shortcode_tags[$shortcode], $attributes, $matches[5], $shortcode) . $suffix : ''; } } diff --git a/rawilum/Themes.php b/rawilum/Templates.php similarity index 58% rename from rawilum/Themes.php rename to rawilum/Templates.php index c9aef679..cb8b88a8 100644 --- a/rawilum/Themes.php +++ b/rawilum/Templates.php @@ -10,19 +10,17 @@ * file that was distributed with this source code. */ -class Themes +class Templates { - /** - * @var Rawilum - */ - protected $rawilum; /** - * __construct + * Protected constructor since this is a static class. + * + * @access protected */ - public function __construct(Rawilum $c) + protected function __construct() { - $this->rawilum = $c; + // Nothing here } /** @@ -32,15 +30,15 @@ class Themes * @param string $template_name Template name * @return mixed */ - public function getTemplate($template_name) + public static function display(string $template_name) { $template_ext = '.php'; - $page = $this->rawilum['pages']->page; + $page = Pages::$page; - $template_path = THEMES_PATH . '/' . $this->rawilum['config']->get('site.theme') . '/' . $template_name . $template_ext; + $template_path = THEMES_PATH . '/' . Config::get('site.theme') . '/' . $template_name . $template_ext; - if ($this->rawilum['filesystem']->exists($template_path)) { + if (Rawilum::$filesystem->exists($template_path)) { include $template_path; } else { throw new RuntimeException("Template {$template_name} does not exist.");