2011-03-24 11:30:57 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handles finding templates from a stack of template manifest objects.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-03-24 11:30:57 +01:00
|
|
|
* @subpackage manifest
|
|
|
|
*/
|
|
|
|
class SS_TemplateLoader {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SS_TemplateLoader
|
|
|
|
*/
|
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SS_TemplateManifest[]
|
|
|
|
*/
|
|
|
|
protected $manifests = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return SS_TemplateLoader
|
|
|
|
*/
|
|
|
|
public static function instance() {
|
|
|
|
return self::$instance ? self::$instance : self::$instance = new self();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the currently active template manifest instance.
|
|
|
|
*
|
|
|
|
* @return SS_TemplateManifest
|
|
|
|
*/
|
|
|
|
public function getManifest() {
|
|
|
|
return $this->manifests[count($this->manifests) - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SS_TemplateManifest $manifest
|
|
|
|
*/
|
|
|
|
public function pushManifest(SS_TemplateManifest $manifest) {
|
|
|
|
$this->manifests[] = $manifest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return SS_TemplateManifest
|
|
|
|
*/
|
|
|
|
public function popManifest() {
|
|
|
|
return array_pop($this->manifests);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to find possible candidate templates from a set of template
|
2012-11-02 08:28:39 +01:00
|
|
|
* names from modules, current theme directory and finally the application
|
|
|
|
* folder.
|
2011-03-24 11:30:57 +01:00
|
|
|
*
|
|
|
|
* 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 $templates
|
|
|
|
* @param string $theme
|
2012-11-02 08:28:39 +01:00
|
|
|
*
|
2011-03-24 11:30:57 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function findTemplates($templates, $theme = null) {
|
|
|
|
$result = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-24 11:30:57 +01:00
|
|
|
foreach ((array) $templates as $template) {
|
|
|
|
if (strpos($template, '/')) {
|
|
|
|
list($type, $template) = explode('/', $template, 2);
|
|
|
|
} else {
|
|
|
|
$type = null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-02 08:28:39 +01:00
|
|
|
if ($found = $this->getManifest()->getCandidateTemplate($template, $theme)) {
|
2013-04-29 17:19:51 +02:00
|
|
|
if ($type && isset($found[$type])) {
|
2014-01-13 21:33:22 +01:00
|
|
|
$found = array(
|
|
|
|
'main' => $found[$type]
|
|
|
|
);
|
2013-04-29 17:19:51 +02:00
|
|
|
}
|
|
|
|
$result = array_merge($found, $result);
|
2011-03-24 11:30:57 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-24 11:30:57 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|