[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/ -> requirejs.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   * This file is serving optimised JS for RequireJS.
  19   *
  20   * @package    core
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  // Disable moodle specific debug messages and any errors in output,
  26  // comment out when debugging or better look into error log!
  27  define('NO_DEBUG_DISPLAY', true);
  28  
  29  // We need just the values from config.php and minlib.php.
  30  define('ABORT_AFTER_CONFIG', true);
  31  require('../config.php'); // This stops immediately at the beginning of lib/setup.php.
  32  require_once("$CFG->dirroot/lib/jslib.php");
  33  require_once("$CFG->dirroot/lib/classes/requirejs.php");
  34  
  35  $slashargument = min_get_slash_argument();
  36  if (!$slashargument) {
  37      // The above call to min_get_slash_argument should always work.
  38      die('Invalid request');
  39  }
  40  
  41  $slashargument = ltrim($slashargument, '/');
  42  if (substr_count($slashargument, '/') < 1) {
  43      header('HTTP/1.0 404 not found');
  44      die('Slash argument must contain both a revision and a file path');
  45  }
  46  // Split into revision and module name.
  47  list($rev, $file) = explode('/', $slashargument, 2);
  48  $rev  = min_clean_param($rev, 'INT');
  49  $file = '/' . min_clean_param($file, 'SAFEPATH');
  50  
  51  // Only load js files from the js modules folder from the components.
  52  $jsfiles = array();
  53  list($unused, $component, $module) = explode('/', $file, 3);
  54  
  55  // No subdirs allowed - only flat module structure please.
  56  if (strpos('/', $module) !== false) {
  57      die('Invalid module');
  58  }
  59  
  60  // Some (huge) modules are better loaded lazily (when they are used). If we are requesting
  61  // one of these modules, only return the one module, not the combo.
  62  $lazysuffix = "-lazy.js";
  63  $lazyload = (strpos($module, $lazysuffix) !== false);
  64  
  65  if ($lazyload) {
  66      // We are lazy loading a single file - so include the component/filename pair in the etag.
  67      $etag = sha1($rev . '/' . $component . '/' . $module);
  68  } else {
  69      // We loading all (non-lazy) files - so only the rev makes this request unique.
  70      $etag = sha1($rev);
  71  }
  72  
  73  
  74  // Use the caching only for meaningful revision numbers which prevents future cache poisoning.
  75  if ($rev > 0 and $rev < (time() + 60 * 60)) {
  76      $candidate = $CFG->localcachedir . '/requirejs/' . $etag;
  77  
  78      if (file_exists($candidate)) {
  79          if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  80              // We do not actually need to verify the etag value because our files
  81              // never change in cache because we increment the rev parameter.
  82              js_send_unmodified(filemtime($candidate), $etag);
  83          }
  84          js_send_cached($candidate, $etag, 'requirejs.php');
  85          exit(0);
  86  
  87      } else {
  88          $jsfiles = array();
  89          if ($lazyload) {
  90              $jsfiles = core_requirejs::find_one_amd_module($component, $module);
  91          } else {
  92              // Here we respond to the request by returning ALL amd modules. This saves
  93              // round trips in production.
  94  
  95              $jsfiles = core_requirejs::find_all_amd_modules();
  96          }
  97  
  98          $content = '';
  99          foreach ($jsfiles as $modulename => $jsfile) {
 100              $js = file_get_contents($jsfile) . "\n";
 101              // Inject the module name into the define.
 102              $replace = 'define(\'' . $modulename . '\', ';
 103              $search = 'define(';
 104              // Replace only the first occurrence.
 105              $js = implode($replace, explode($search, $js, 2));
 106              $content .= $js;
 107          }
 108  
 109          js_write_cache_file_content($candidate, $content);
 110          // Verify nothing failed in cache file creation.
 111          clearstatcache();
 112          if (file_exists($candidate)) {
 113              js_send_cached($candidate, $etag, 'requirejs.php');
 114              exit(0);
 115          }
 116      }
 117  }
 118  
 119  if ($lazyload) {
 120      $jsfiles = core_requirejs::find_one_amd_module($component, $module, true);
 121  } else {
 122      $jsfiles = core_requirejs::find_all_amd_modules(true);
 123  }
 124  
 125  $content = '';
 126  foreach ($jsfiles as $modulename => $jsfile) {
 127      $shortfilename = str_replace($CFG->dirroot, '', $jsfile);
 128      $js = "// ---- $shortfilename ----\n";
 129      $js .= file_get_contents($jsfile) . "\n";
 130      // Inject the module name into the define.
 131      $replace = 'define(\'' . $modulename . '\', ';
 132      $search = 'define(';
 133  
 134      if (strpos($js, $search) === false) {
 135          // We can't call debugging because we only have minimal config loaded.
 136          header('HTTP/1.0 500 error');
 137          die('JS file: ' . $shortfilename . ' does not contain a javascript module in AMD format. "define()" not found.');
 138      }
 139  
 140      // Replace only the first occurrence.
 141      $js = implode($replace, explode($search, $js, 2));
 142      $content .= $js;
 143  }
 144  js_send_uncached($content, $etag, 'requirejs.php');


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