Improve themes dir guessing; add $config['theme_url'] config

This commit is contained in:
Daniel Rudolf 2016-04-24 01:22:43 +02:00
parent 1b3ef7516d
commit d19621a908
No known key found for this signature in database
GPG key ID: A061F02CD8DE4538
2 changed files with 48 additions and 7 deletions

View file

@ -10,23 +10,23 @@
* {@path "config/config.php"}, uncomment the line, then make and
* save your changes.
*
* @author Gilbert Pellegrom
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT
* @version 1.0
* @version 1.1
*/
/*
* BASIC
*/
// $config['site_title'] = 'Pico'; // Site title
// $config['base_url'] = ''; // Override base URL (e.g. http://example.com)
// $config['base_url'] = ''; // Override base URL (e.g. http://example.com/pico/)
// $config['rewrite_url'] = null; // A boolean indicating forced URL rewriting
/*
* THEME
*/
// $config['theme'] = 'default'; // Set the theme (defaults to "default")
// $config['theme_url'] = ''; // Override the base URL of the themes folder (e.g. http://example.com/pico/themes/)
// $config['twig_config'] = array( // Twig settings
// 'cache' => false, // To enable Twig caching change this to a path to a writable directory
// 'autoescape' => false, // Auto-escape Twig vars

View file

@ -22,7 +22,7 @@
* @author Daniel Rudolf
* @link <http://picocms.org>
* @license The MIT License <http://opensource.org/licenses/MIT>
* @version 1.0
* @version 1.1
*/
class Pico
{
@ -590,6 +590,14 @@ class Pico
$this->config['content_dir'] = $this->getAbsolutePath($this->config['content_dir']);
}
if (empty($this->config['theme_url'])) {
$this->config['theme_url'] = $this->getBaseThemeUrl();
} elseif (preg_match('#^[A-Za-z][A-Za-z0-9+\-.]*://#', $this->config['theme_url'])) {
$this->config['theme_url'] = rtrim($this->config['theme_url'], '/') . '/';
} else {
$this->config['theme_url'] = $this->getBaseUrl() . rtrim($this->config['theme_url'], '/') . '/';
}
if (empty($this->config['timezone'])) {
// explicitly set a default timezone to prevent a E_NOTICE
// when no timezone is set; the `date_default_timezone_get()`
@ -973,8 +981,7 @@ class Pico
$variables['%base_url%'] = rtrim($this->getBaseUrl(), '/');
// replace %theme_url%
$themeUrl = $this->getBaseUrl() . basename($this->getThemesDir()) . '/' . $this->getConfig('theme');
$variables['%theme_url%'] = $themeUrl;
$variables['%theme_url%'] = $this->getBaseThemeUrl() . $this->getConfig('theme');
// replace %meta.*%
if (!empty($meta)) {
@ -1311,7 +1318,7 @@ class Pico
'base_dir' => rtrim($this->getRootDir(), '/'),
'base_url' => rtrim($this->getBaseUrl(), '/'),
'theme_dir' => $this->getThemesDir() . $this->getConfig('theme'),
'theme_url' => $this->getBaseUrl() . basename($this->getThemesDir()) . '/' . $this->getConfig('theme'),
'theme_url' => $this->getBaseThemeUrl() . $this->getConfig('theme'),
'rewrite_url' => $this->isUrlRewritingEnabled(),
'site_title' => $this->getConfig('site_title'),
'meta' => $this->meta,
@ -1410,6 +1417,40 @@ class Pico
}
}
/**
* Returns the URL of the themes folder of this Pico instance
*
* We assume that the themes folder is a arbitrary deep sub folder of the
* script's base path (i.e. the directory {@path "index.php"} is in resp.
* the `httpdocs` directory). Usually the script's base path is identical
* to {@link Pico::$rootDir}, but this may aberrate when Pico got installed
* as a composer dependency. However, ultimately it allows us to use
* {@link Pico::getBaseUrl()} as origin of the theme URL. Otherwise Pico
* falls back to the basename of {@link Pico::$themesDir} (i.e. assuming
* that `Pico::$themesDir` is `foo/bar/baz`, the base URL of the themes
* folder will be `baz/`; this ensures BC to Pico < 1.1). Pico's base URL
* always gets prepended appropriately.
*
* @return string the URL of the themes folder
*/
public function getBaseThemeUrl()
{
$themeUrl = $this->getConfig('theme_url');
if (!empty($themeUrl)) {
return $themeUrl;
}
$basePath = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
$basePathLength = strlen($basePath);
if (substr($this->getThemesDir(), 0, $basePathLength) === $basePath) {
$this->config['theme_url'] = $this->getBaseUrl() . substr($this->getThemesDir(), $basePathLength);
} else {
$this->config['theme_url'] = $this->getBaseUrl() . basename($this->getThemesDir()) . '/';
}
return $this->config['theme_url'];
}
/**
* Recursively walks through a directory and returns all containing files
* matching the specified file extension