2016-07-13 14:36:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\View;
|
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
use InvalidArgumentException;
|
2017-02-22 04:14:53 +01:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
2018-01-12 04:25:02 +01:00
|
|
|
use SilverStripe\Core\Path;
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2016-07-13 14:36:52 +02:00
|
|
|
/**
|
|
|
|
* Handles finding templates from a stack of template manifest objects.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class ThemeResourceLoader
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ThemeResourceLoader
|
|
|
|
*/
|
|
|
|
private static $instance;
|
|
|
|
|
2018-10-04 17:32:51 +02:00
|
|
|
/**
|
|
|
|
* Internal memory cache for large sets of repeated calls
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $cacheData = [];
|
|
|
|
|
2017-07-13 00:27:27 +02:00
|
|
|
/**
|
|
|
|
* The base path of the application
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
protected $base;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of template "sets" that contain a test manifest, and have an alias.
|
|
|
|
* E.g. '$default'
|
|
|
|
*
|
|
|
|
* @var ThemeList[]
|
|
|
|
*/
|
|
|
|
protected $sets = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ThemeResourceLoader
|
|
|
|
*/
|
2017-05-19 04:38:06 +02:00
|
|
|
public static function inst()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
return self::$instance ? self::$instance : self::$instance = new self();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set instance
|
|
|
|
*
|
|
|
|
* @param ThemeResourceLoader $instance
|
|
|
|
*/
|
|
|
|
public static function set_instance(ThemeResourceLoader $instance)
|
|
|
|
{
|
|
|
|
self::$instance = $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct($base = null)
|
|
|
|
{
|
|
|
|
$this->base = $base ? $base : BASE_PATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new theme manifest for a given identifier. E.g. '$default'
|
|
|
|
*
|
|
|
|
* @param string $set
|
|
|
|
* @param ThemeList $manifest
|
|
|
|
*/
|
|
|
|
public function addSet($set, ThemeList $manifest)
|
|
|
|
{
|
|
|
|
$this->sets[$set] = $manifest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a named theme set
|
|
|
|
*
|
|
|
|
* @param string $set
|
|
|
|
* @return ThemeList
|
|
|
|
*/
|
|
|
|
public function getSet($set)
|
|
|
|
{
|
|
|
|
if (isset($this->sets[$set])) {
|
|
|
|
return $this->sets[$set];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a theme identifier, determine the path from the root directory
|
|
|
|
*
|
|
|
|
* The mapping from $identifier to path follows these rules:
|
|
|
|
* - A simple theme name ('mytheme') which maps to the standard themes dir (/themes/mytheme)
|
|
|
|
* - A theme path with a leading slash ('/mymodule/themes/mytheme') which maps directly to that path.
|
|
|
|
* - or a vendored theme path. (vendor/mymodule:mytheme) which maps to the nested 'theme' within
|
|
|
|
* that module. ('/mymodule/themes/mytheme').
|
|
|
|
* - A vendored module with no nested theme (vendor/mymodule) which maps to the root directory
|
|
|
|
* of that module. ('/mymodule').
|
|
|
|
*
|
|
|
|
* @param string $identifier Theme identifier.
|
|
|
|
* @return string Path from root, not including leading or trailing forward slash. E.g. themes/mytheme
|
|
|
|
*/
|
|
|
|
public function getPath($identifier)
|
|
|
|
{
|
|
|
|
$slashPos = strpos($identifier, '/');
|
2017-09-21 07:54:06 +02:00
|
|
|
$parts = explode(':', $identifier, 2);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// If identifier starts with "/", it's a path from root
|
|
|
|
if ($slashPos === 0) {
|
|
|
|
if (count($parts) > 1) {
|
2017-09-21 07:54:06 +02:00
|
|
|
throw new InvalidArgumentException("Invalid theme identifier {$identifier}");
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2018-01-12 04:25:02 +01:00
|
|
|
return Path::normalise($identifier, true);
|
2017-09-21 07:54:06 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
// If there is no slash / colon it's a legacy theme
|
|
|
|
if ($slashPos === false && count($parts) === 1) {
|
2018-01-12 04:25:02 +01:00
|
|
|
return Path::join(THEMES_DIR, $identifier);
|
2017-09-21 07:54:06 +02:00
|
|
|
}
|
2017-02-22 04:14:53 +01:00
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
// Extract from <vendor>/<module>:<theme> format.
|
|
|
|
// <vendor> is optional, and if <theme> is omitted it defaults to the module root dir.
|
|
|
|
// If <theme> is included, this is the name of the directory under moduleroot/themes/
|
|
|
|
// which contains the theme.
|
|
|
|
// <module> is always the name of the install directory, not necessarily the composer name.
|
|
|
|
|
|
|
|
// Find module from first part
|
|
|
|
$moduleName = $parts[0];
|
|
|
|
$module = ModuleLoader::inst()->getManifest()->getModule($moduleName);
|
|
|
|
if ($module) {
|
|
|
|
$modulePath = $module->getRelativePath();
|
|
|
|
} else {
|
|
|
|
// If no module could be found, assume based on basename
|
|
|
|
// with a warning
|
|
|
|
if (strstr('/', $moduleName)) {
|
2017-02-22 04:14:53 +01:00
|
|
|
list(, $modulePath) = explode('/', $parts[0], 2);
|
2017-09-21 07:54:06 +02:00
|
|
|
} else {
|
|
|
|
$modulePath = $moduleName;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-09-21 07:54:06 +02:00
|
|
|
trigger_error("No module named {$moduleName} found. Assuming path {$modulePath}", E_USER_WARNING);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
// Parse relative path for this theme within this module
|
|
|
|
$theme = count($parts) > 1 ? $parts[1] : '';
|
|
|
|
if (empty($theme)) {
|
|
|
|
// "module/vendor:"
|
|
|
|
// "module/vendor"
|
|
|
|
$subpath = '';
|
|
|
|
} elseif (strpos($theme, '/') === 0) {
|
|
|
|
// "module/vendor:/sub/path"
|
|
|
|
$subpath = rtrim($theme, '/');
|
|
|
|
} else {
|
|
|
|
// "module/vendor:subtheme"
|
|
|
|
$subpath = '/themes/' . $theme;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-09-21 07:54:06 +02:00
|
|
|
|
|
|
|
// Join module with subpath
|
2018-01-12 04:25:02 +01:00
|
|
|
return Path::normalise($modulePath . $subpath, true);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to find possible candidate templates from a set of template
|
|
|
|
* names from modules, current theme directory and finally the application
|
|
|
|
* folder.
|
|
|
|
*
|
|
|
|
* The template names can be passed in as plain strings, or be in the
|
|
|
|
* format "type/name", where type is the type of template to search for
|
|
|
|
* (e.g. Includes, Layout).
|
|
|
|
*
|
|
|
|
* @param string|array $template Template name, or template spec in array format with the keys
|
|
|
|
* 'type' (type string) and 'templates' (template hierarchy in order of precedence).
|
|
|
|
* If 'templates' is ommitted then any other item in the array will be treated as the template
|
|
|
|
* list, or list of templates each in the array spec given.
|
|
|
|
* Templates with an .ss extension will be treated as file paths, and will bypass
|
|
|
|
* theme-coupled resolution.
|
2018-02-15 21:09:35 +01:00
|
|
|
* @param array $themes List of themes to use to resolve themes. Defaults to {@see SSViewer::get_themes()}
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return string Absolute path to resolved template file, or null if not resolved.
|
|
|
|
* File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss
|
|
|
|
* Note that type (e.g. 'Layout') is not the root level directory under 'templates'.
|
|
|
|
*/
|
2018-02-15 21:09:35 +01:00
|
|
|
public function findTemplate($template, $themes = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2018-10-04 17:32:51 +02:00
|
|
|
// Look for a cached result for this data set
|
|
|
|
$cacheKey = json_encode($template) . json_encode($themes);
|
|
|
|
if (isset(static::$cacheData[$cacheKey])) {
|
|
|
|
return static::$cacheData[$cacheKey];
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:09:35 +01:00
|
|
|
if ($themes === null) {
|
|
|
|
$themes = SSViewer::get_themes();
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$type = '';
|
|
|
|
if (is_array($template)) {
|
|
|
|
// Check if templates has type specified
|
|
|
|
if (array_key_exists('type', $template)) {
|
|
|
|
$type = $template['type'];
|
|
|
|
unset($template['type']);
|
|
|
|
}
|
|
|
|
// Templates are either nested in 'templates' or just the rest of the list
|
|
|
|
$templateList = array_key_exists('templates', $template) ? $template['templates'] : $template;
|
|
|
|
} else {
|
|
|
|
$templateList = array($template);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($templateList as $i => $template) {
|
|
|
|
// Check if passed list of templates in array format
|
|
|
|
if (is_array($template)) {
|
|
|
|
$path = $this->findTemplate($template, $themes);
|
|
|
|
if ($path) {
|
2018-10-04 17:32:51 +02:00
|
|
|
return static::$cacheData[$cacheKey] = $path;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an .ss extension, this is a path, not a template name. We should
|
|
|
|
// pass in templates without extensions in order for template manifest to find
|
|
|
|
// files dynamically.
|
|
|
|
if (substr($template, -3) == '.ss' && file_exists($template)) {
|
2018-10-04 17:32:51 +02:00
|
|
|
return static::$cacheData[$cacheKey] = $template;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check string template identifier
|
|
|
|
$template = str_replace('\\', '/', $template);
|
|
|
|
$parts = explode('/', $template);
|
|
|
|
|
|
|
|
$tail = array_pop($parts);
|
|
|
|
$head = implode('/', $parts);
|
|
|
|
$themePaths = $this->getThemePaths($themes);
|
|
|
|
foreach ($themePaths as $themePath) {
|
|
|
|
// Join path
|
|
|
|
$pathParts = [ $this->base, $themePath, 'templates', $head, $type, $tail ];
|
2018-01-12 04:25:02 +01:00
|
|
|
$path = Path::join($pathParts) . '.ss';
|
2016-11-29 00:31:16 +01:00
|
|
|
if (file_exists($path)) {
|
2018-10-04 17:32:51 +02:00
|
|
|
return static::$cacheData[$cacheKey] = $path;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No template found
|
2018-10-04 17:32:51 +02:00
|
|
|
return static::$cacheData[$cacheKey] = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve themed CSS path
|
|
|
|
*
|
|
|
|
* @param string $name Name of CSS file without extension
|
2018-02-15 21:09:35 +01:00
|
|
|
* @param array $themes List of themes, Defaults to {@see SSViewer::get_themes()}
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return string Path to resolved CSS file (relative to base dir)
|
|
|
|
*/
|
2018-02-15 21:09:35 +01:00
|
|
|
public function findThemedCSS($name, $themes = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2018-02-15 21:09:35 +01:00
|
|
|
if ($themes === null) {
|
|
|
|
$themes = SSViewer::get_themes();
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if (substr($name, -4) !== '.css') {
|
|
|
|
$name .= '.css';
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = $this->findThemedResource("css/$name", $themes);
|
|
|
|
if ($filename === null) {
|
|
|
|
$filename = $this->findThemedResource($name, $themes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve themed javascript path
|
|
|
|
*
|
|
|
|
* A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for,
|
|
|
|
* and it that doesn't exist and the module parameter is set then a javascript file with that name in
|
|
|
|
* the module is used.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the file - eg '/js/File.js' would have the name 'File'
|
2018-02-15 21:09:35 +01:00
|
|
|
* @param array $themes List of themes, Defaults to {@see SSViewer::get_themes()}
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return string Path to resolved javascript file (relative to base dir)
|
|
|
|
*/
|
2018-02-15 21:09:35 +01:00
|
|
|
public function findThemedJavascript($name, $themes = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2018-02-15 21:09:35 +01:00
|
|
|
if ($themes === null) {
|
|
|
|
$themes = SSViewer::get_themes();
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if (substr($name, -3) !== '.js') {
|
|
|
|
$name .= '.js';
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = $this->findThemedResource("javascript/$name", $themes);
|
|
|
|
if ($filename === null) {
|
|
|
|
$filename = $this->findThemedResource($name, $themes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a themed resource
|
|
|
|
*
|
|
|
|
* A themed resource and be any file that resides in a theme folder.
|
|
|
|
*
|
|
|
|
* @param string $resource A file path relative to the root folder of a theme
|
2018-02-15 21:09:35 +01:00
|
|
|
* @param array $themes An order listed of themes to search, Defaults to {@see SSViewer::get_themes()}
|
2017-10-03 01:20:49 +02:00
|
|
|
* @return string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2018-02-15 21:09:35 +01:00
|
|
|
public function findThemedResource($resource, $themes = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2018-02-15 21:09:35 +01:00
|
|
|
if ($themes === null) {
|
|
|
|
$themes = SSViewer::get_themes();
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$paths = $this->getThemePaths($themes);
|
|
|
|
|
|
|
|
foreach ($paths as $themePath) {
|
2018-01-12 04:25:02 +01:00
|
|
|
$relativePath = Path::join($themePath, $resource);
|
|
|
|
$absolutePath = Path::join($this->base, $relativePath);
|
|
|
|
if (file_exists($absolutePath)) {
|
|
|
|
return $relativePath;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource exists in no context
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve all themes to the list of root folders relative to site root
|
|
|
|
*
|
2018-02-15 21:09:35 +01:00
|
|
|
* @param array $themes List of themes to resolve. Supports named theme sets. Defaults to {@see SSViewer::get_themes()}.
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return array List of root-relative folders in order of precendence.
|
|
|
|
*/
|
2018-02-15 21:09:35 +01:00
|
|
|
public function getThemePaths($themes = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2018-02-15 21:09:35 +01:00
|
|
|
if ($themes === null) {
|
|
|
|
$themes = SSViewer::get_themes();
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$paths = [];
|
|
|
|
foreach ($themes as $themename) {
|
|
|
|
// Expand theme sets
|
|
|
|
$set = $this->getSet($themename);
|
|
|
|
$subthemes = $set ? $set->getThemes() : [$themename];
|
|
|
|
|
|
|
|
// Resolve paths
|
|
|
|
foreach ($subthemes as $theme) {
|
|
|
|
$paths[] = $this->getPath($theme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $paths;
|
|
|
|
}
|
2016-07-13 14:36:52 +02:00
|
|
|
}
|