2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * RequireJS helper functions.
21 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Collection of requirejs related methods.
30 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class core_requirejs
{
36 * Check a single module exists and return the full path to it.
38 * The expected location for amd modules is:
39 * <componentdir>/amd/src/modulename.js
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).
44 * @return array $files An array of mappings from module names to file paths.
45 * Empty array if the file does not exist.
47 public static function find_one_amd_module($component, $jsfilename, $unused = false) {
48 $jsfileroot = core_component
::get_component_directory($component);
53 $module = str_replace('.js', '', $jsfilename);
55 $srcdir = $jsfileroot . '/amd/build';
58 $filename = $srcdir . '/' . $module . $minpart . '.js';
59 if (!file_exists($filename)) {
63 $fullmodulename = $component . '/' . $module;
64 return array($fullmodulename => $filename);
68 * Scan the source for AMD modules and return them all.
70 * The expected location for amd modules is:
71 * <componentdir>/amd/src/modulename.js
73 * @param boolean $unused
74 * @param boolean $includelazy If true, includes modules with the -lazy suffix.
75 * @return array $files An array of mappings from module names to file paths.
77 public static function find_all_amd_modules($unused = false, $includelazy = false) {
83 $dir = $CFG->libdir
. '/amd';
84 if (!empty($dir) && is_dir($dir)) {
85 $jsdirs['core'] = $dir;
87 $subsystems = core_component
::get_core_subsystems();
88 foreach ($subsystems as $subsystem => $dir) {
89 if (!empty($dir) && is_dir($dir . '/amd')) {
90 $jsdirs['core_' . $subsystem] = $dir . '/amd';
93 $plugintypes = core_component
::get_plugin_types();
94 foreach ($plugintypes as $type => $dir) {
95 $plugins = core_component
::get_plugin_list_with_file($type, 'amd', false);
96 foreach ($plugins as $plugin => $dir) {
97 if (!empty($dir) && is_dir($dir)) {
98 $jsdirs[$type . '_' . $plugin] = $dir;
103 foreach ($jsdirs as $component => $dir) {
104 $srcdir = $dir . '/build';
105 if (!is_dir($srcdir) ||
!is_readable($srcdir)) {
106 // This is probably an empty amd directory without src or build.
107 // Skip it - RecursiveDirectoryIterator fatals if the directory is not readable as an iterator.
110 $srcdir = realpath($srcdir);
111 $directory = new RecursiveDirectoryIterator($srcdir);
112 $items = new RecursiveIteratorIterator($directory);
113 foreach ($items as $item) {
114 $extension = $item->getExtension();
115 if ($extension === 'js') {
116 $filename = substr($item->getRealpath(), strlen($srcdir) +
1);
117 $filename = preg_replace('/(\.min)?\.js$/', '', $filename);
118 // We skip lazy loaded modules unless specifically requested.
119 if ($includelazy ||
strpos($filename, '-lazy') === false) {
120 $modulename = $component . '/' . $filename;
121 $jsfiles[$modulename] = $item->getRealPath();