/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
- <?php
- /*
- * JS packaging and compression inspired by jammit
- *
- * Copyright (c) 2011 <mlunzena@uos.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- */
- namespace {
- // emulate #ctype_digit, unless it exists
- // yaml lib needs it
- if (!function_exists('ctype_digit')) {
- function ctype_digit($text) {
- return preg_match('/^\d+$/', $text);
- }
- }
- }
- namespace Studip\Squeeze {
- /**
- * Write all packages specified in $configFile to $outputDir.
- * The default $configFile is "$STUDIP_BASE_PATH/config/assets.yml"
- *
- * @param string $configFile path to the config file
- * @param string $outputDir path to the output directory
- */
- function packageAll($configFile = NULL, $outputDir = NULL)
- {
- global $STUDIP_BASE_PATH;
- $configFile = $configFile ?: "$STUDIP_BASE_PATH/config/assets.yml";
- $configuration = Configuration::load($configFile, $forced = TRUE);
- $packager = new Packager($configuration);
- $packager->cacheAll($outputDir);
- $compressor = new Compressor($configuration);
- if (is_array($configuration['css'])) {
- $compress = $compressor->shouldCompress();
- if ($compress && !$compressor->hasJava()) {
- $compress = false;
- error_log('CSS could not be compressed, since Java is missing.');
- }
- $config_time = filemtime($configFile);
- foreach ($configuration['css'] as $package => $files) {
- foreach (array_keys($files) as $file) {
- $src = $configuration['assets_root'] . '/stylesheets/' . $file;
- $dest = $configuration['package_path'] . '/' . $package . '-' . $file;
- if (!file_exists($dest) || (max($config_time, filemtime($src)) > filemtime($dest))) {
- $contents = file_get_contents($src);
- if ($compress) {
- $contents = $compressor->callCompressor($contents, 'css');
- }
- file_put_contents($dest, $contents);
- }
- }
- }
- }
- }
- /**
- * Include a single squeeze package depending on \Studip\ENV as
- * individual script elements or as a single one containing the
- * squeezed source code of all files comprising the package.
- *
- * @param Packager $packager the packager instance to use
- * @param array $packages an array containing the names of packages
- *
- * @return an array containing PageLayout style HTML elements
- */
- function includePackages($packager, $packages)
- {
- return array_reduce($packages, function ($memo, $package) use ($packager) {
- return array_merge($memo,
- shouldPackage()
- ? packageAsCompressedURL($packager, $package)
- : packageAsIndividualURLs($packager, $package));
- }, array());
- }
- /**
- * @return bool TRUE in production mode, FALSE in development or
- * while debugging (= GET request contains a
- * 'debug_assets' param)
- */
- function shouldPackage()
- {
- return \Studip\ENV !== 'development' && !\Request::submitted('debug_assets');
- }
- /**
- * Include a single squeeze package as individual script elements.
- *
- * @param Packager $packager the packager instance to use
- * @param string $package the name of a package
- *
- * @return an array containing PageLayout style HTML elements
- */
- function packageAsIndividualURLs($packager, $package)
- {
- $elements = array();
- foreach ($packager->individualURLs($package) as $src) {
- $elements[] = array(
- 'name' => 'script',
- 'attributes' => compact('src'),
- 'content' => '');
- }
- return $elements;
- }
- /**
- * Include a single squeeze package as a single one containing the
- * squeezed source code of all files comprising the package.
- *
- * @param Packager $packager the packager instance to use
- * @param string $package the name of a package
- *
- * @return an array containing PageLayout style HTML elements
- */
- function packageAsCompressedURL($packager, $package)
- {
- return array(
- array(
- 'name' => 'script',
- 'attributes' => array('src' => $packager->packageURL($package), 'charset' => 'utf-8'),
- 'content' => '')
- );
- }
- }