PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/classes/squeeze/squeeze.php

https://github.com/ratbird/hope
PHP | 138 lines | 73 code | 14 blank | 51 comment | 8 complexity | 1689384edeb6cfa6471389ad5831926a MD5 | raw file
Possible License(s): LGPL-2.1, CC-BY-SA-3.0, MIT, BSD-3-Clause, GPL-2.0
  1. <?php
  2. /*
  3. * JS packaging and compression inspired by jammit
  4. *
  5. * Copyright (c) 2011 <mlunzena@uos.de>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. namespace {
  13. // emulate #ctype_digit, unless it exists
  14. // yaml lib needs it
  15. if (!function_exists('ctype_digit')) {
  16. function ctype_digit($text) {
  17. return preg_match('/^\d+$/', $text);
  18. }
  19. }
  20. }
  21. namespace Studip\Squeeze {
  22. /**
  23. * Write all packages specified in $configFile to $outputDir.
  24. * The default $configFile is "$STUDIP_BASE_PATH/config/assets.yml"
  25. *
  26. * @param string $configFile path to the config file
  27. * @param string $outputDir path to the output directory
  28. */
  29. function packageAll($configFile = NULL, $outputDir = NULL)
  30. {
  31. global $STUDIP_BASE_PATH;
  32. $configFile = $configFile ?: "$STUDIP_BASE_PATH/config/assets.yml";
  33. $configuration = Configuration::load($configFile, $forced = TRUE);
  34. $packager = new Packager($configuration);
  35. $packager->cacheAll($outputDir);
  36. $compressor = new Compressor($configuration);
  37. if (is_array($configuration['css'])) {
  38. $compress = $compressor->shouldCompress();
  39. if ($compress && !$compressor->hasJava()) {
  40. $compress = false;
  41. error_log('CSS could not be compressed, since Java is missing.');
  42. }
  43. $config_time = filemtime($configFile);
  44. foreach ($configuration['css'] as $package => $files) {
  45. foreach (array_keys($files) as $file) {
  46. $src = $configuration['assets_root'] . '/stylesheets/' . $file;
  47. $dest = $configuration['package_path'] . '/' . $package . '-' . $file;
  48. if (!file_exists($dest) || (max($config_time, filemtime($src)) > filemtime($dest))) {
  49. $contents = file_get_contents($src);
  50. if ($compress) {
  51. $contents = $compressor->callCompressor($contents, 'css');
  52. }
  53. file_put_contents($dest, $contents);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * Include a single squeeze package depending on \Studip\ENV as
  61. * individual script elements or as a single one containing the
  62. * squeezed source code of all files comprising the package.
  63. *
  64. * @param Packager $packager the packager instance to use
  65. * @param array $packages an array containing the names of packages
  66. *
  67. * @return an array containing PageLayout style HTML elements
  68. */
  69. function includePackages($packager, $packages)
  70. {
  71. return array_reduce($packages, function ($memo, $package) use ($packager) {
  72. return array_merge($memo,
  73. shouldPackage()
  74. ? packageAsCompressedURL($packager, $package)
  75. : packageAsIndividualURLs($packager, $package));
  76. }, array());
  77. }
  78. /**
  79. * @return bool TRUE in production mode, FALSE in development or
  80. * while debugging (= GET request contains a
  81. * 'debug_assets' param)
  82. */
  83. function shouldPackage()
  84. {
  85. return \Studip\ENV !== 'development' && !\Request::submitted('debug_assets');
  86. }
  87. /**
  88. * Include a single squeeze package as individual script elements.
  89. *
  90. * @param Packager $packager the packager instance to use
  91. * @param string $package the name of a package
  92. *
  93. * @return an array containing PageLayout style HTML elements
  94. */
  95. function packageAsIndividualURLs($packager, $package)
  96. {
  97. $elements = array();
  98. foreach ($packager->individualURLs($package) as $src) {
  99. $elements[] = array(
  100. 'name' => 'script',
  101. 'attributes' => compact('src'),
  102. 'content' => '');
  103. }
  104. return $elements;
  105. }
  106. /**
  107. * Include a single squeeze package as a single one containing the
  108. * squeezed source code of all files comprising the package.
  109. *
  110. * @param Packager $packager the packager instance to use
  111. * @param string $package the name of a package
  112. *
  113. * @return an array containing PageLayout style HTML elements
  114. */
  115. function packageAsCompressedURL($packager, $package)
  116. {
  117. return array(
  118. array(
  119. 'name' => 'script',
  120. 'attributes' => array('src' => $packager->packageURL($package), 'charset' => 'utf-8'),
  121. 'content' => '')
  122. );
  123. }
  124. }