Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   * JS and CSS compression.
  19   *
  20   * @package    core
  21   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  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 JS and CSS compression methods.
  29   */
  30  class core_minify {
  31      /**
  32       * Minify JS code.
  33       *
  34       * @param string $content
  35       * @return string minified JS code
  36       */
  37      public static function js($content) {
  38          try {
  39              $minifier = new MatthiasMullie\Minify\JS($content);
  40              return $minifier->minify();
  41          } catch (Exception $e) {
  42              ob_end_clean();
  43              $error = $e->getMessage();
  44          }
  45  
  46          $return = <<<EOD
  47  
  48  try {console.log('Error: Minimisation of JavaScript failed!');} catch (e) {}
  49  
  50  // Error: $error
  51  // Problem detected during JavaScript minimisation, please review the following code
  52  // =================================================================================
  53  
  54  
  55  EOD;
  56  
  57          return $return.$content;
  58      }
  59  
  60      /**
  61       * Minify JS files.
  62       *
  63       * @param array $files
  64       * @return string minified JS code
  65       */
  66      public static function js_files(array $files) {
  67          if (empty($files)) {
  68              return '';
  69          }
  70  
  71          $compressed = array();
  72          foreach ($files as $file) {
  73              $content = file_get_contents($file);
  74              if ($content === false) {
  75                  $compressed[] = "\n\n// Cannot read JS file ".basename(dirname(dirname($file))).'/'.basename(dirname($file)).'/'.basename($file)."\n\n";
  76                  continue;
  77              }
  78              $compressed[] = self::js($content);
  79          }
  80  
  81          return implode(";\n", $compressed);
  82      }
  83  
  84      /**
  85       * Minify CSS code.
  86       *
  87       * @param string $content
  88       * @return string minified CSS
  89       */
  90      public static function css($content) {
  91          $error = 'unknown';
  92          try {
  93              $minifier = new MatthiasMullie\Minify\CSS($content);
  94              return $minifier->minify();
  95          } catch (Exception $e) {
  96              $error = $e->getMessage();
  97          }
  98  
  99          $return = <<<EOD
 100  
 101  /* Error: $error */
 102  /* Problem detected during CSS minimisation, please review the following code */
 103  /* ========================================================================== */
 104  
 105  
 106  EOD;
 107  
 108          return $return.$content;
 109      }
 110  
 111      /**
 112       * Minify CSS files.
 113       *
 114       * @param array $files
 115       * @return string minified CSS code
 116       */
 117      public static function css_files(array $files) {
 118          if (empty($files)) {
 119              return '';
 120          }
 121  
 122          $compressed = array();
 123          foreach ($files as $file) {
 124              $content = file_get_contents($file);
 125              if ($content === false) {
 126                  $compressed[] = "\n\n/* Cannot read CSS file ".basename(dirname(dirname($file))).'/'.basename(dirname($file)).'/'.basename($file)."*/\n\n";
 127                  continue;
 128              }
 129              $compressed[] = self::css($content);
 130          }
 131  
 132          return implode("\n", $compressed);
 133      }
 134  }