Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * RequireJS helper functions.
  19   *
  20   * @package    core
  21   * @copyright  2015 Damyon Wiese <damyon@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Collection of requirejs related methods.
  29   *
  30   * @copyright  2015 Damyon Wiese <damyon@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class core_requirejs {
  34  
  35      /**
  36       * Check a single module exists and return the full path to it.
  37       *
  38       * The expected location for amd modules is:
  39       *  <componentdir>/amd/src/modulename.js
  40       *
  41       * @param string $component The component determines the folder the js file should be in.
  42       * @param string $jsfilename The filename for the module (with the js extension).
  43       * @param boolean $debug If true, returns the paths to the original (unminified) source files.
  44       * @return array $files An array of mappings from module names to file paths.
  45       *                      Empty array if the file does not exist.
  46       */
  47      public static function find_one_amd_module($component, $jsfilename, $debug = false) {
  48          $jsfileroot = core_component::get_component_directory($component);
  49          if (!$jsfileroot) {
  50              return array();
  51          }
  52  
  53          $module = str_replace('.js', '', $jsfilename);
  54  
  55          $srcdir = $jsfileroot . '/amd/build';
  56          $minpart = '.min';
  57          if ($debug) {
  58              $srcdir = $jsfileroot . '/amd/src';
  59              $minpart = '';
  60          }
  61  
  62          $filename = $srcdir . '/' . $module . $minpart . '.js';
  63          if (!file_exists($filename)) {
  64              return array();
  65          }
  66  
  67          $fullmodulename = $component . '/' . $module;
  68          return array($fullmodulename => $filename);
  69      }
  70  
  71      /**
  72       * Scan the source for AMD modules and return them all.
  73       *
  74       * The expected location for amd modules is:
  75       *  <componentdir>/amd/src/modulename.js
  76       *
  77       * @param boolean $debug If true, returns the paths to the original (unminified) source files.
  78       * @param boolean $includelazy If true, includes modules with the -lazy suffix.
  79       * @return array $files An array of mappings from module names to file paths.
  80       */
  81      public static function find_all_amd_modules($debug = false, $includelazy = false) {
  82          global $CFG;
  83  
  84          $jsdirs = array();
  85          $jsfiles = array();
  86  
  87          $dir = $CFG->libdir . '/amd';
  88          if (!empty($dir) && is_dir($dir)) {
  89              $jsdirs['core'] = $dir;
  90          }
  91          $subsystems = core_component::get_core_subsystems();
  92          foreach ($subsystems as $subsystem => $dir) {
  93              if (!empty($dir) && is_dir($dir . '/amd')) {
  94                  $jsdirs['core_' . $subsystem] = $dir . '/amd';
  95              }
  96          }
  97          $plugintypes = core_component::get_plugin_types();
  98          foreach ($plugintypes as $type => $dir) {
  99              $plugins = core_component::get_plugin_list_with_file($type, 'amd', false);
 100              foreach ($plugins as $plugin => $dir) {
 101                  if (!empty($dir) && is_dir($dir)) {
 102                      $jsdirs[$type . '_' . $plugin] = $dir;
 103                  }
 104              }
 105          }
 106  
 107          foreach ($jsdirs as $component => $dir) {
 108              $srcdir = $dir . '/build';
 109              if ($debug) {
 110                  $srcdir = $dir . '/src';
 111              }
 112              if (!is_dir($srcdir) || !is_readable($srcdir)) {
 113                  // This is probably an empty amd directory without src or build.
 114                  // Skip it - RecursiveDirectoryIterator fatals if the directory is not readable as an iterator.
 115                  continue;
 116              }
 117              $srcdir = realpath($srcdir);
 118              $directory = new RecursiveDirectoryIterator($srcdir);
 119              $items = new RecursiveIteratorIterator($directory);
 120              foreach ($items as $item) {
 121                  $extension = $item->getExtension();
 122                  if ($extension === 'js') {
 123                      $filename = substr($item->getRealpath(), strlen($srcdir) + 1);
 124                      $filename = preg_replace('/(\.min)?\.js$/', '', $filename);
 125                      // We skip lazy loaded modules unless specifically requested.
 126                      if ($includelazy || strpos($filename, '-lazy') === false) {
 127                          $modulename = $component . '/' . $filename;
 128                          $jsfiles[$modulename] = $item->getRealPath();
 129                      }
 130                  }
 131                  unset($item);
 132              }
 133              unset($items);
 134          }
 135  
 136          return $jsfiles;
 137      }
 138  
 139  }