Overhaul init of Pico

This may break BC if you're using one of the now deprecated constants (e.g. ROOT_DIR)
This commit is contained in:
Daniel Rudolf 2015-10-01 15:05:50 +02:00
parent 5e77d862bf
commit fc7632b0ac
4 changed files with 127 additions and 28 deletions

View file

@ -1,10 +0,0 @@
<?php
define('ROOT_DIR', __DIR__ . '/');
define('LIB_DIR', ROOT_DIR . 'lib/');
define('VENDOR_DIR', ROOT_DIR . 'vendor/');
define('PLUGINS_DIR', ROOT_DIR . 'plugins/');
define('THEMES_DIR', ROOT_DIR . 'themes/');
define('CONFIG_DIR', ROOT_DIR . 'config/');
require_once(VENDOR_DIR . 'autoload.php');

View file

@ -1,3 +1,9 @@
<?php
require_once(__DIR__ . '/global.php');
$pico = new Pico();
require_once(__DIR__ . '/vendor/autoload.php');
$pico = new Pico(
__DIR__,
__DIR__ . '/config/',
__DIR__ . '/plugins/',
__DIR__ . '/themes/'
);
echo $pico->run();

View file

@ -26,6 +26,34 @@
*/
class Pico
{
/**
* Root directory of this Pico instance
*
* @var string
*/
protected $rootDir;
/**
* Config directory of this Pico instance
*
* @var string
*/
protected $configDir;
/**
* Plugins directory of this Pico instance
*
* @var string
*/
protected $pluginsDir;
/**
* Themes directory of this Pico instance
*
* @var string
*/
protected $themesDir;
/**
* List of loaded plugins
*
@ -133,10 +161,66 @@ class Pico
/**
* Constructs a new Pico instance
*
* The constructor carries out all the processing in Pico.
* Does URL routing, Markdown processing and Twig processing.
* To carry out all the processing in Pico, call the run() method.
*/
public function __construct()
public function __construct($rootDir, $configDir, $pluginsDir, $themesDir)
{
$this->rootDir = rtrim($rootDir, '/') . '/';
$this->configDir = rtrim($configDir, '/') . '/';
$this->pluginsDir = rtrim($pluginsDir, '/') . '/';
$this->themesDir = rtrim($themesDir, '/') . '/';
}
/**
* Returns the root directory of this Pico instance
*
* @return string root directory path
*/
public function getRootDir()
{
return $this->rootDir;
}
/**
* Returns the config directory of this Pico instance
*
* @return string config directory path
*/
public function getConfigDir()
{
return $this->configDir;
}
/**
* Returns the plugins directory of this Pico instance
*
* @return string plugins directory path
*/
public function getPluginsDir()
{
return $this->pluginsDir;
}
/**
* Returns the themes directory of this Pico instance
*
* @return string themes directory path
*/
public function getThemesDir()
{
return $this->themesDir;
}
/**
* Runs this Pico instance
*
* Loads plugins, evaluates the config file, does URL routing, parses
* meta headers, processes Markdown, does Twig processing and returns
* the rendered contents.
*
* @return string rendered Pico contents
*/
public function run()
{
// load plugins
$this->loadPlugins();
@ -210,7 +294,7 @@ class Pico
} else {
$templateName = 'index';
}
if (file_exists(THEMES_DIR . $this->getConfig('theme') . '/' . $templateName . '.twig')) {
if (file_exists($this->getThemesDir() . $this->getConfig('theme') . '/' . $templateName . '.twig')) {
$templateName .= '.twig';
} else {
$templateName .= '.html';
@ -221,7 +305,7 @@ class Pico
$output = $this->twig->render($templateName, $this->twigVariables);
$this->triggerEvent('onPageRendered', array(&$output));
echo $output;
return $output;
}
/**
@ -237,7 +321,7 @@ class Pico
protected function loadPlugins()
{
$this->plugins = array();
$pluginFiles = $this->getFiles(PLUGINS_DIR, '.php');
$pluginFiles = $this->getFiles($this->getPluginsDir(), '.php');
foreach ($pluginFiles as $pluginFile) {
require_once($pluginFile);
@ -302,12 +386,13 @@ class Pico
'twig_config' => array('cache' => false, 'autoescape' => false, 'debug' => false),
'pages_order_by' => 'alpha',
'pages_order' => 'asc',
'content_dir' => ROOT_DIR . 'content-sample/',
'content_dir' => $this->getRootDir() . 'content-sample/',
'content_ext' => '.md',
'timezone' => ''
);
$config = file_exists(CONFIG_DIR . 'config.php') ? require(CONFIG_DIR . 'config.php') : null;
$configFile = $this->getConfigDir() . 'config.php';
$config = file_exists($configFile) ? require($configFile) : null;
$this->config = is_array($config) ? $config + $defaultConfig : $defaultConfig;
if (empty($this->config['base_url'])) {
@ -579,7 +664,7 @@ class Pico
$content = str_replace('%base_url%', rtrim($this->getBaseUrl(), '/'), $content);
// replace %theme_url%
$themeUrl = $this->getBaseUrl() . basename(THEMES_DIR) . '/' . $this->getConfig('theme');
$themeUrl = $this->getBaseUrl() . basename($this->getThemesDir()) . '/' . $this->getConfig('theme');
$content = str_replace('%theme_url%', $themeUrl, $content);
// replace %meta.*%
@ -783,7 +868,7 @@ class Pico
*/
protected function registerTwig()
{
$twigLoader = new Twig_Loader_Filesystem(THEMES_DIR . $this->getConfig('theme'));
$twigLoader = new Twig_Loader_Filesystem($this->getThemesDir() . $this->getConfig('theme'));
$this->twig = new Twig_Environment($twigLoader, $this->getConfig('twig_config'));
$this->twig->addExtension(new Twig_Extension_Debug());
$this->twig->addFilter(new Twig_SimpleFilter('link', array($this, 'getPageUrl')));
@ -812,10 +897,10 @@ class Pico
$frontPage = $this->getConfig('content_dir') . 'index' . $this->getConfig('content_ext');
return array(
'config' => $this->getConfig(),
'base_dir' => rtrim(ROOT_DIR, '/'),
'base_dir' => rtrim($this->getRootDir(), '/'),
'base_url' => rtrim($this->getBaseUrl(), '/'),
'theme_dir' => THEMES_DIR . $this->getConfig('theme'),
'theme_url' => $this->getBaseUrl() . basename(THEMES_DIR) . '/' . $this->getConfig('theme'),
'theme_dir' => $this->getThemesDir() . $this->getConfig('theme'),
'theme_url' => $this->getBaseUrl() . basename($this->getThemesDir()) . '/' . $this->getConfig('theme'),
'rewrite_url' => $this->isUrlRewritingEnabled(),
'site_title' => $this->getConfig('site_title'),
'meta' => $this->meta,

View file

@ -93,9 +93,9 @@ class PicoDeprecated extends AbstractPicoPlugin
*/
public function onConfigLoaded(&$config)
{
if (file_exists(ROOT_DIR . 'config.php')) {
if (file_exists($this->getRootDir() . 'config.php')) {
// config.php in ROOT_DIR is deprecated; use CONFIG_DIR instead
$newConfig = require(ROOT_DIR . 'config.php');
$newConfig = require($this->getRootDir() . 'config.php');
if (is_array($newConfig)) {
$config = $newConfig + $config;
}
@ -119,7 +119,25 @@ class PicoDeprecated extends AbstractPicoPlugin
}
// CONTENT_DIR constant is deprecated since v0.9,
// CONTENT_EXT constant since v1.0
// ROOT_DIR, LIB_DIR, PLUGINS_DIR, THEMES_DIR and CONTENT_EXT constants since v1.0,
// CONFIG_DIR constant existed just for a short time between v0.9 and v1.0,
// CACHE_DIR constant was dropped with v1.0 without a replacement
if (!defined('ROOT_DIR')) {
define('ROOT_DIR', $this->getRootDir());
}
if (!defined('CONFIG_DIR')) {
define('CONFIG_DIR', $this->getConfigDir());
}
if (!defined('LIB_DIR')) {
$picoReflector = new ReflectionClass('Pico');
define('LIB_DIR', dirname($picoReflector->getFileName() . '/'));
}
if (!defined('PLUGINS_DIR')) {
define('PLUGINS_DIR', $this->getPluginsDir());
}
if (!defined('THEMES_DIR')) {
define('THEMES_DIR', $this->getThemesDir());
}
if (!defined('CONTENT_DIR')) {
define('CONTENT_DIR', $config['content_dir']);
}