Browse Source

Explicitly treat relative paths to be relative to Picos root dir

This tempers the BC break, we can now recommend to simply remove the ROOT_DIR part
Daniel Rudolf 9 years ago
parent
commit
cdef7a6324
2 changed files with 23 additions and 7 deletions
  1. 3 3
      index.php
  2. 20 4
      lib/Pico.php

+ 3 - 3
index.php

@@ -2,8 +2,8 @@
 require_once(__DIR__ . '/vendor/autoload.php');
 $pico = new Pico(
     __DIR__,
-    __DIR__ . '/config/',
-    __DIR__ . '/plugins/',
-    __DIR__ . '/themes/'
+    'config/',
+    'plugins/',
+    'themes/'
 );
 echo $pico->run();

+ 20 - 4
lib/Pico.php

@@ -166,9 +166,9 @@ class Pico
     public function __construct($rootDir, $configDir, $pluginsDir, $themesDir)
     {
         $this->rootDir = rtrim($rootDir, '/') . '/';
-        $this->configDir = rtrim($configDir, '/') . '/';
-        $this->pluginsDir = rtrim($pluginsDir, '/') . '/';
-        $this->themesDir = rtrim($themesDir, '/') . '/';
+        $this->configDir = $this->getAbsolutePath($configDir);
+        $this->pluginsDir = $this->getAbsolutePath($pluginsDir);
+        $this->themesDir = $this->getAbsolutePath($themesDir);
     }
 
     /**
@@ -399,7 +399,7 @@ class Pico
             $this->config['base_url'] = $this->getBaseUrl();
         }
         if (!empty($this->config['content_dir'])) {
-            $this->config['content_dir'] = rtrim($this->config['content_dir'], '/') . '/';
+            $this->config['content_dir'] = $this->getAbsolutePath($this->config['content_dir']);
         }
         if (!empty($this->config['timezone'])) {
             date_default_timezone_set($this->config['timezone']);
@@ -1006,6 +1006,22 @@ class Pico
         return $result;
     }
 
+    /**
+     * Makes a relative path absolute to Picos root dir
+     *
+     * This method also guarantees a trailing slash.
+     *
+     * @param  string $path relative or absolute path
+     * @return string       absolute path
+     */
+    protected function getAbsolutePath($path)
+    {
+        if (substr($path, 0, 1) !== '/') {
+            $path = $this->getRootDir() . $path;
+        }
+        return rtrim($path, '/') . '/';
+    }
+
     /**
      * Triggers events on plugins which implement {@link PicoPluginInterface}
      *