/lib/classes/requirejs.php

https://gitlab.com/unofficial-mirrors/moodle · PHP · 135 lines · 70 code · 14 blank · 51 comment · 15 complexity · a38c05ab724c08e84b9180a92244e4ff MD5 · raw file

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