[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/classes/output/ -> mustache_template_finder.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * List the valid locations to search for a template with a given name.
  19   *
  20   * @package    core
  21   * @category   output
  22   * @copyright  2015 Damyon Wiese
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\output;
  27  
  28  use coding_exception;
  29  use moodle_exception;
  30  use core_component;
  31  use theme_config;
  32  
  33  /**
  34   * Get information about valid locations for mustache templates.
  35   *
  36   * @copyright  2015 Damyon Wiese
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @since      2.9
  39   */
  40  class mustache_template_finder {
  41  
  42      /**
  43       * Helper function for getting a list of valid template directories for a specific component.
  44       *
  45       * @param string $component The component to search
  46       * @param string $themename The current theme name
  47       * @return string[] List of valid directories for templates for this compoonent. Directories are not checked for existence.
  48       */
  49      public static function get_template_directories_for_component($component, $themename = '') {
  50          global $CFG, $PAGE;
  51  
  52          // Default the param.
  53          if ($themename == '') {
  54              $themename = $PAGE->theme->name;
  55          }
  56  
  57          // Clean params for safety.
  58          $component = clean_param($component, PARAM_COMPONENT);
  59          $themename = clean_param($themename, PARAM_COMPONENT);
  60  
  61          // Validate the component.
  62          $dirs = array();
  63          $compdirectory = core_component::get_component_directory($component);
  64          if (!$compdirectory) {
  65              throw new coding_exception("Component was not valid: " . s($component));
  66          }
  67  
  68          // Find the parent themes.
  69          $parents = array();
  70          if ($themename === $PAGE->theme->name) {
  71              $parents = $PAGE->theme->parents;
  72          } else {
  73              $themeconfig = theme_config::load($themename);
  74              $parents = $themeconfig->parents;
  75          }
  76  
  77          // First check the theme.
  78          $dirs[] = $CFG->dirroot . '/theme/' . $themename . '/templates/' . $component . '/';
  79          // Now check the parent themes.
  80          // Search each of the parent themes second.
  81          foreach ($parents as $parent) {
  82              $dirs[] = $CFG->dirroot . '/theme/' . $parent . '/templates/' . $component . '/';
  83          }
  84  
  85          $dirs[] = $compdirectory . '/templates/';
  86  
  87          return $dirs;
  88      }
  89  
  90      /**
  91       * Helper function for getting a filename for a template from the template name.
  92       *
  93       * @param string $name - This is the componentname/templatename combined.
  94       * @param string $themename - This is the current theme name.
  95       * @return string
  96       */
  97      public static function get_template_filepath($name, $themename = '') {
  98          global $CFG, $PAGE;
  99  
 100          if (strpos($name, '/') === false) {
 101              throw new coding_exception('Templates names must be specified as "componentname/templatename"' .
 102                                         ' (' . s($name) . ' requested) ');
 103          }
 104          list($component, $templatename) = explode('/', $name, 2);
 105          $component = clean_param($component, PARAM_COMPONENT);
 106          if (strpos($templatename, '/') !== false) {
 107              throw new coding_exception('Templates cannot be placed in sub directories (' . s($name) . ' requested)');
 108          }
 109  
 110          $dirs = self::get_template_directories_for_component($component, $themename);
 111  
 112          foreach ($dirs as $dir) {
 113              $candidate = $dir . $templatename . '.mustache';
 114              if (file_exists($candidate)) {
 115                  return $candidate;
 116              }
 117          }
 118  
 119          throw new moodle_exception('filenotfound', 'error');
 120      }
 121  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1