瀏覽代碼

Overhaul init of Pico

This may break BC if you're using one of the now deprecated constants (e.g. ROOT_DIR)
Daniel Rudolf 9 年之前
父節點
當前提交
fc7632b0ac
共有 4 個文件被更改,包括 127 次插入28 次删除
  1. 0 10
      global.php
  2. 8 2
      index.php
  3. 98 13
      lib/Pico.php
  4. 21 3
      plugins/00-PicoDeprecated.php

+ 0 - 10
global.php

@@ -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');
-

+ 8 - 2
index.php

@@ -1,3 +1,9 @@
 <?php
 <?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();

+ 98 - 13
lib/Pico.php

@@ -26,6 +26,34 @@
  */
  */
 class Pico
 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
      * List of loaded plugins
      *
      *
@@ -133,10 +161,66 @@ class Pico
     /**
     /**
      * Constructs a new Pico instance
      * 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($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 __construct()
+    public function run()
     {
     {
         // load plugins
         // load plugins
         $this->loadPlugins();
         $this->loadPlugins();
@@ -210,7 +294,7 @@ class Pico
         } else {
         } else {
             $templateName = 'index';
             $templateName = 'index';
         }
         }
-        if (file_exists(THEMES_DIR . $this->getConfig('theme') . '/' . $templateName . '.twig')) {
+        if (file_exists($this->getThemesDir() . $this->getConfig('theme') . '/' . $templateName . '.twig')) {
             $templateName .= '.twig';
             $templateName .= '.twig';
         } else {
         } else {
             $templateName .= '.html';
             $templateName .= '.html';
@@ -221,7 +305,7 @@ class Pico
         $output = $this->twig->render($templateName, $this->twigVariables);
         $output = $this->twig->render($templateName, $this->twigVariables);
         $this->triggerEvent('onPageRendered', array(&$output));
         $this->triggerEvent('onPageRendered', array(&$output));
 
 
-        echo $output;
+        return $output;
     }
     }
 
 
     /**
     /**
@@ -237,7 +321,7 @@ class Pico
     protected function loadPlugins()
     protected function loadPlugins()
     {
     {
         $this->plugins = array();
         $this->plugins = array();
-        $pluginFiles = $this->getFiles(PLUGINS_DIR, '.php');
+        $pluginFiles = $this->getFiles($this->getPluginsDir(), '.php');
         foreach ($pluginFiles as $pluginFile) {
         foreach ($pluginFiles as $pluginFile) {
             require_once($pluginFile);
             require_once($pluginFile);
 
 
@@ -302,12 +386,13 @@ class Pico
             'twig_config' => array('cache' => false, 'autoescape' => false, 'debug' => false),
             'twig_config' => array('cache' => false, 'autoescape' => false, 'debug' => false),
             'pages_order_by' => 'alpha',
             'pages_order_by' => 'alpha',
             'pages_order' => 'asc',
             'pages_order' => 'asc',
-            'content_dir' => ROOT_DIR . 'content-sample/',
+            'content_dir' => $this->getRootDir() . 'content-sample/',
             'content_ext' => '.md',
             'content_ext' => '.md',
             'timezone' => ''
             '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;
         $this->config = is_array($config) ? $config + $defaultConfig : $defaultConfig;
 
 
         if (empty($this->config['base_url'])) {
         if (empty($this->config['base_url'])) {
@@ -579,7 +664,7 @@ class Pico
         $content = str_replace('%base_url%', rtrim($this->getBaseUrl(), '/'), $content);
         $content = str_replace('%base_url%', rtrim($this->getBaseUrl(), '/'), $content);
 
 
         // replace %theme_url%
         // 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);
         $content = str_replace('%theme_url%', $themeUrl, $content);
 
 
         // replace %meta.*%
         // replace %meta.*%
@@ -783,7 +868,7 @@ class Pico
      */
      */
     protected function registerTwig()
     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 = new Twig_Environment($twigLoader, $this->getConfig('twig_config'));
         $this->twig->addExtension(new Twig_Extension_Debug());
         $this->twig->addExtension(new Twig_Extension_Debug());
         $this->twig->addFilter(new Twig_SimpleFilter('link', array($this, 'getPageUrl')));
         $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');
         $frontPage = $this->getConfig('content_dir') . 'index' . $this->getConfig('content_ext');
         return array(
         return array(
             'config' => $this->getConfig(),
             'config' => $this->getConfig(),
-            'base_dir' => rtrim(ROOT_DIR, '/'),
+            'base_dir' => rtrim($this->getRootDir(), '/'),
             'base_url' => rtrim($this->getBaseUrl(), '/'),
             '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(),
             'rewrite_url' => $this->isUrlRewritingEnabled(),
             'site_title' => $this->getConfig('site_title'),
             'site_title' => $this->getConfig('site_title'),
             'meta' => $this->meta,
             'meta' => $this->meta,

+ 21 - 3
plugins/00-PicoDeprecated.php

@@ -93,9 +93,9 @@ class PicoDeprecated extends AbstractPicoPlugin
      */
      */
     public function onConfigLoaded(&$config)
     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
             // 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)) {
             if (is_array($newConfig)) {
                 $config = $newConfig + $config;
                 $config = $newConfig + $config;
             }
             }
@@ -119,7 +119,25 @@ class PicoDeprecated extends AbstractPicoPlugin
         }
         }
 
 
         // CONTENT_DIR constant is deprecated since v0.9,
         // 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')) {
         if (!defined('CONTENT_DIR')) {
             define('CONTENT_DIR', $config['content_dir']);
             define('CONTENT_DIR', $config['content_dir']);
         }
         }