1
0
mirror of https://github.com/monstra-cms/monstra.git synced 2025-08-03 19:57:57 +02:00

#431 general structure changes

This commit is contained in:
Awilum
2018-03-05 01:41:26 +03:00
parent d69615b8ad
commit 0057b27a88
4 changed files with 37 additions and 43 deletions

View File

@@ -30,7 +30,6 @@
"force/shortcode" : "*", "force/shortcode" : "*",
"force/arr" : "*", "force/arr" : "*",
"force/http" : "*", "force/http" : "*",
"force/filesystem" : "*",
"force/token" : "*", "force/token" : "*",
"force/url" : "*" "force/url" : "*"
}, },

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace Monstra;
/** /**
* This file is part of the Monstra. * This file is part of the Monstra.
@@ -12,11 +13,9 @@
class Cache class Cache
{ {
/** /**
* An instance of the Cache class * @var Monstra
*
* @var object
*/ */
protected static $instance = null; protected $monstra;
/** /**
* Unique cache key * Unique cache key
@@ -46,34 +45,29 @@ class Cache
*/ */
protected static $driver; protected static $driver;
/**
* Protected clone method to enforce singleton behavior.
*
* @access protected
*/
protected function __clone()
{
// Nothing here.
}
/** /**
* Constructor. * Constructor.
* *
* @access protected * @access protected
*/ */
protected function __construct() public function __construct(Monstra $c)
{ {
$this->monstra = $c;
// Set current time // Set current time
static::$now = time(); static::$now = time();
// Cache key allows us to invalidate all cache on configuration changes. // Cache key allows us to invalidate all cache on configuration changes.
static::$key = (Config::get('site.cache.prefix') ? Config::get('site.cache.prefix') : 'monstra') . '-' . md5(ROOT_DIR . Monstra::VERSION); static::$key = ($this->monstra['config']->get('site.cache.prefix') ? $this->monstra['config']->get('site.cache.prefix') : 'monstra') . '-' . md5(ROOT_DIR . 'Monstra::VERSION');
// Get Cache Driver // Get Cache Driver
static::$driver = static::getCacheDriver(); static::$driver = $this->getCacheDriver();
// Set the cache namespace to our unique key // Set the cache namespace to our unique key
static::$driver->setNamespace(static::$key); static::$driver->setNamespace(static::$key);
// Return
return static::$driver;
} }
/** /**
@@ -82,9 +76,9 @@ class Cache
* @access public * @access public
* @return object * @return object
*/ */
public static function getCacheDriver() public function getCacheDriver()
{ {
$driver_name = Config::get('site.cache.driver'); $driver_name = $this->monstra['config']->get('site.cache.driver');
if (!$driver_name || $driver_name == 'auto') { if (!$driver_name || $driver_name == 'auto') {
if (extension_loaded('apc')) { if (extension_loaded('apc')) {
@@ -110,21 +104,21 @@ class Cache
break; break;
case 'memcache': case 'memcache':
$memcache = new \Memcache(); $memcache = new \Memcache();
$memcache->connect(Config::get('site.cache.memcache.server', 'localhost'), $memcache->connect($this->monstra['config']->get('site.cache.memcache.server', 'localhost'),
Config::get('site.cache.memcache.port', 11211)); $this->monstra['config']->get('site.cache.memcache.port', 11211));
$driver = new \Doctrine\Common\Cache\MemcacheCache(); $driver = new \Doctrine\Common\Cache\MemcacheCache();
$driver->setMemcache($memcache); $driver->setMemcache($memcache);
break; break;
case 'redis': case 'redis':
$redis = new \Redis(); $redis = new \Redis();
$redis->connect(Config::get('site.cache.redis.server', 'localhost'), $redis->connect($this->monstra['config']->get('site.cache.redis.server', 'localhost'),
Config::get('site.cache.redis.port', 6379)); $this->monstra['config']->get('site.cache.redis.port', 6379));
$driver = new \Doctrine\Common\Cache\RedisCache(); $driver = new \Doctrine\Common\Cache\RedisCache();
$driver->setRedis($redis); $driver->setRedis($redis);
break; break;
default: default:
// Create doctrine cache directory if its not exists // Create doctrine cache directory if its not exists
!Dir::exists($cache_directory = CACHE_PATH . '/doctrine/') and Dir::create($cache_directory); !$this->monstra['filesystem']->exists($cache_directory = CACHE_PATH . '/doctrine/') and $this->monstra['filesystem']->mkdir($cache_directory);
$driver = new \Doctrine\Common\Cache\FilesystemCache($cache_directory); $driver = new \Doctrine\Common\Cache\FilesystemCache($cache_directory);
break; break;
} }
@@ -138,7 +132,7 @@ class Cache
* @access public * @access public
* @return object * @return object
*/ */
public static function driver() public function driver()
{ {
return static::$driver; return static::$driver;
} }
@@ -149,7 +143,7 @@ class Cache
* @access public * @access public
* @return string * @return string
*/ */
public static function getKey() public function getKey()
{ {
return static::$key; return static::$key;
} }
@@ -163,7 +157,7 @@ class Cache
*/ */
public function fetch($id) public function fetch($id)
{ {
if (Config::get('site.cache.enabled')) { if ($this->monstra['config']->get('site.cache.enabled')) {
return static::$driver->fetch($id); return static::$driver->fetch($id);
} else { } else {
return false; return false;
@@ -182,7 +176,7 @@ class Cache
*/ */
public function save($id, $data, $lifetime = null) public function save($id, $data, $lifetime = null)
{ {
if (Config::get('site.cache.enabled')) { if ($this->monstra['config']->get('site.cache.enabled')) {
if ($lifetime === null) { if ($lifetime === null) {
$lifetime = static::getLifetime(); $lifetime = static::getLifetime();
} }
@@ -193,10 +187,9 @@ class Cache
/** /**
* Clear Cache * Clear Cache
*/ */
public static function clear() public function clear()
{ {
Dir::delete(CACHE_PATH . '/doctrine/'); $this->monstra['filesystem']->remove(CACHE_PATH . '/doctrine/');
Dir::delete(CACHE_PATH . '/fenom/');
} }
/** /**
@@ -205,7 +198,7 @@ class Cache
* @access public * @access public
* @param int $future timestamp * @param int $future timestamp
*/ */
public static function setLifetime($future) public function setLifetime($future)
{ {
if (!$future) { if (!$future) {
return; return;
@@ -224,10 +217,10 @@ class Cache
* @access public * @access public
* @return mixed * @return mixed
*/ */
public static function getLifetime() public function getLifetime()
{ {
if (static::$lifetime === null) { if (static::$lifetime === null) {
static::$lifetime = Config::get('site.cache.lifetime') ?: 604800; static::$lifetime = $this->monstra['config']->get('site.cache.lifetime') ?: 604800;
} }
return static::$lifetime; return static::$lifetime;
} }
@@ -242,8 +235,8 @@ class Cache
* @access public * @access public
* @return object * @return object
*/ */
public static function init() public function init()
{ {
return !isset(self::$instance) and self::$instance = new Cache();
} }
} }

View File

@@ -51,10 +51,17 @@ class Monstra extends Container
return new Finder(); return new Finder();
}; };
$container['cache'] = function ($c) {
return new Cache($c);
};
$container['config'] = function ($c) { $container['config'] = function ($c) {
return new Config($c); return new Config($c);
}; };
// Start the session
//\Session::start();
$container['markdown'] = function ($c) { $container['markdown'] = function ($c) {
return new ParsedownExtra(); return new ParsedownExtra();
}; };

View File

@@ -103,13 +103,8 @@ class Page
*/ */
public function renderPage($page) public function renderPage($page)
{ {
if (empty($page['template'])) {
$template_name = 'index';
} else {
$template_name = $page['template'];
}
$template_ext = '.php'; $template_ext = '.php';
$template_name = empty($page['template']) ? 'index' : $page['template'];
include THEMES_PATH . '/' . $this->monstra['config']->get('site.theme') . '/' . $template_name . $template_ext; include THEMES_PATH . '/' . $this->monstra['config']->get('site.theme') . '/' . $template_name . $template_ext;
} }