Merge pull request #11430 from owncloud/minify-js-css-master

adding cssmin and jssmin(minify)
This commit is contained in:
Morris Jobke 2014-10-17 15:05:59 +02:00
commit 7bf401e48a
7 changed files with 148 additions and 82 deletions

@ -1 +1 @@
Subproject commit 5db359cb710c51747d3fb78b605f8b8cdcd1e605
Subproject commit 635aaf81d0d97f000c9d4b90fc5a3240d05cf371

View File

@ -402,9 +402,10 @@ class OC_Helper {
/**
* Recursive deletion of folders
* @param string $dir path to the folder
* @param bool $deleteSelf if set to false only the content of the folder will be deleted
* @return bool
*/
static function rmdirr($dir) {
static function rmdirr($dir, $deleteSelf = true) {
if (is_dir($dir)) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
@ -419,15 +420,19 @@ class OC_Helper {
unlink($fileInfo->getRealPath());
}
}
if ($deleteSelf) {
rmdir($dir);
}
} elseif (file_exists($dir)) {
if ($deleteSelf) {
unlink($dir);
}
if (file_exists($dir)) {
return false;
} else {
}
if (!$deleteSelf) {
return true;
}
return !file_exists($dir);
}
/**

View File

@ -10,6 +10,13 @@ namespace OC;
use OC\Hooks\BasicEmitter;
use OC\Hooks\Emitter;
use OC\Repair\AssetCache;
use OC\Repair\Collation;
use OC\Repair\InnoDB;
use OC\Repair\RepairConfig;
use OC\Repair\RepairLegacyStorages;
use OC\Repair\RepairMimeTypes;
use OC\Repair\SearchLuceneTables;
class Repair extends BasicEmitter {
/**
@ -69,9 +76,10 @@ class Repair extends BasicEmitter {
*/
public static function getRepairSteps() {
return array(
new \OC\Repair\RepairMimeTypes(),
new \OC\Repair\RepairLegacyStorages(\OC::$server->getConfig(), \OC_DB::getConnection()),
new \OC\Repair\RepairConfig(),
new RepairMimeTypes(),
new RepairLegacyStorages(\OC::$server->getConfig(), \OC_DB::getConnection()),
new RepairConfig(),
new AssetCache()
);
}
@ -83,14 +91,14 @@ class Repair extends BasicEmitter {
*/
public static function getBeforeUpgradeRepairSteps() {
$steps = array(
new \OC\Repair\InnoDB(),
new \OC\Repair\Collation(\OC::$server->getConfig(), \OC_DB::getConnection()),
new \OC\Repair\SearchLuceneTables()
new InnoDB(),
new Collation(\OC::$server->getConfig(), \OC_DB::getConnection()),
new SearchLuceneTables()
);
//There is no need to delete all previews on every single update
//only 7.0.0 thru 7.0.2 generated broken previews
$currentVersion = \OC_Config::getValue('version');
//only 7.0.0 through 7.0.2 generated broken previews
$currentVersion = \OC::$server->getConfig()->getSystemValue('version');
if (version_compare($currentVersion, '7.0.0.0', '>=') &&
version_compare($currentVersion, '7.0.2.2', '<=')) {
$steps[] = new \OC\Repair\Preview();
@ -102,7 +110,7 @@ class Repair extends BasicEmitter {
/**
* {@inheritDoc}
*
* Redeclared as public to allow invocation from within the closure above in php 5.3
* Re-declared as public to allow invocation from within the closure above in php 5.3
*/
public function emit($scope, $method, $arguments = array()) {
parent::emit($scope, $method, $arguments);

View File

@ -198,8 +198,8 @@ class OC_Template extends \OC\Template\Base {
* Includes another template. use <?php echo $this->inc('template'); ?> to
* do this.
*/
public function inc( $file, $additionalparams = null ) {
return $this->load($this->path.$file.'.php', $additionalparams);
public function inc( $file, $additionalParams = null ) {
return $this->load($this->path.$file.'.php', $additionalParams);
}
/**
@ -277,4 +277,34 @@ class OC_Template extends \OC\Template\Base {
$content->printPage();
die();
}
/**
* @return bool
*/
public static function isAssetPipelineEnabled() {
// asset management enabled?
$useAssetPipeline = \OC::$server->getConfig()->getSystemValue('asset-pipeline.enabled', false);
if (!$useAssetPipeline) {
return false;
}
// assets folder exists?
$assetDir = \OC::$SERVERROOT . '/assets';
if (!is_dir($assetDir)) {
if (!mkdir($assetDir)) {
\OCP\Util::writeLog('assets',
"Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR);
return false;
}
}
// assets folder can be accessed?
if (!touch($assetDir."/.oc")) {
\OCP\Util::writeLog('assets',
"Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR);
return false;
}
return $useAssetPipeline;
}
}

View File

@ -24,6 +24,8 @@ class TemplateFileLocator {
/**
* @param string $template
* @return string
* @throws \Exception
*/
public function find( $template ) {
if ($template === '') {

View File

@ -2,8 +2,10 @@
use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\AssetWriter;
use Assetic\Filter\CssRewriteFilter;
use Assetic\Filter\CssImportFilter;
use Assetic\Filter\CssMinFilter;
use Assetic\Filter\CssRewriteFilter;
use Assetic\Filter\JSMinFilter;
/**
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
@ -17,13 +19,22 @@ class OC_TemplateLayout extends OC_Template {
private static $versionHash = '';
/**
* @param string $renderas
* @param string $appid application id
* @var \OCP\IConfig
*/
public function __construct( $renderas, $appid = '' ) {
private $config;
/**
* @param string $renderAs
* @param string $appId application id
*/
public function __construct( $renderAs, $appId = '' ) {
// yes - should be injected ....
$this->config = \OC::$server->getConfig();
// Decide which page we show
if( $renderas == 'user' ) {
if( $renderAs == 'user' ) {
parent::__construct( 'core', 'layout.user' );
if(in_array(OC_APP::getCurrentApp(), array('settings','admin', 'help'))!==false) {
$this->assign('bodyid', 'body-settings');
@ -32,9 +43,12 @@ class OC_TemplateLayout extends OC_Template {
}
// Update notification
if(OC_Config::getValue('updatechecker', true) === true) {
$data=OC_Updater::check();
if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array() && OC_User::isAdminUser(OC_User::getUser())) {
if($this->config->getSystemValue('updatechecker', true) === true &&
OC_User::isAdminUser(OC_User::getUser())) {
$updater = new \OC\Updater();
$data = $updater->check('http://apps.owncloud.com/updater.php');
if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array()) {
$this->assign('updateAvailable', true);
$this->assign('updateVersion', $data['versionstring']);
$this->assign('updateLink', $data['web']);
@ -47,7 +61,7 @@ class OC_TemplateLayout extends OC_Template {
// Add navigation entry
$this->assign( 'application', '', false );
$this->assign( 'appid', $appid );
$this->assign( 'appid', $appId );
$navigation = OC_App::getNavigation();
$this->assign( 'navigation', $navigation);
$this->assign( 'settingsnavigation', OC_App::getSettingsNavigation());
@ -57,15 +71,15 @@ class OC_TemplateLayout extends OC_Template {
break;
}
}
$user_displayname = OC_User::getDisplayName();
$this->assign( 'user_displayname', $user_displayname );
$userDisplayName = OC_User::getDisplayName();
$this->assign( 'user_displayname', $userDisplayName );
$this->assign( 'user_uid', OC_User::getUser() );
$this->assign( 'appsmanagement_active', strpos(OC_Request::requestUri(), OC_Helper::linkToRoute('settings_apps')) === 0 );
$this->assign('enableAvatars', \OC_Config::getValue('enable_avatars', true));
} else if ($renderas == 'error') {
$this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true));
} else if ($renderAs == 'error') {
parent::__construct('core', 'layout.guest', '', false);
$this->assign('bodyid', 'body-login');
} else if ($renderas == 'guest') {
} else if ($renderAs == 'guest') {
parent::__construct('core', 'layout.guest');
$this->assign('bodyid', 'body-login');
} else {
@ -76,27 +90,27 @@ class OC_TemplateLayout extends OC_Template {
self::$versionHash = md5(implode(',', OC_App::getAppVersions()));
}
$useAssetPipeline = $this->isAssetPipelineEnabled();
$useAssetPipeline = self::isAssetPipelineEnabled();
if ($useAssetPipeline) {
$this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
$this->generateAssets();
} else {
// Add the js files
$jsfiles = self::findJavascriptFiles(OC_Util::$scripts);
$jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
$this->assign('jsfiles', array(), false);
if (OC_Config::getValue('installed', false) && $renderas!='error') {
if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
$this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
}
foreach($jsfiles as $info) {
foreach($jsFiles as $info) {
$web = $info[1];
$file = $info[2];
$this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
}
// Add the css files
$cssfiles = self::findStylesheetFiles(OC_Util::$styles);
$cssFiles = self::findStylesheetFiles(OC_Util::$styles);
$this->assign('cssfiles', array());
foreach($cssfiles as $info) {
foreach($cssFiles as $info) {
$web = $info[1];
$file = $info[2];
@ -113,10 +127,10 @@ class OC_TemplateLayout extends OC_Template {
// Read the selected theme from the config file
$theme = OC_Util::getTheme();
// Read the detected formfactor and use the right file name.
$fext = self::getFormFactorExtension();
// Read the detected form factor and use the right file name.
$formFactorExt = self::getFormFactorExtension();
$locator = new \OC\Template\CSSResourceLocator( $theme, $fext,
$locator = new \OC\Template\CSSResourceLocator( $theme, $formFactorExt,
array( OC::$SERVERROOT => OC::$WEBROOT ),
array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
$locator->find($styles);
@ -131,18 +145,17 @@ class OC_TemplateLayout extends OC_Template {
// Read the selected theme from the config file
$theme = OC_Util::getTheme();
// Read the detected formfactor and use the right file name.
$fext = self::getFormFactorExtension();
// Read the detected form factor and use the right file name.
$formFactorExt = self::getFormFactorExtension();
$locator = new \OC\Template\JSResourceLocator( $theme, $fext,
$locator = new \OC\Template\JSResourceLocator( $theme, $formFactorExt,
array( OC::$SERVERROOT => OC::$WEBROOT ),
array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
$locator->find($scripts);
return $locator->getResources();
}
public function generateAssets()
{
public function generateAssets() {
$jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
$jsHash = self::hashScriptNames($jsFiles);
@ -150,7 +163,13 @@ class OC_TemplateLayout extends OC_Template {
$jsFiles = array_map(function ($item) {
$root = $item[0];
$file = $item[2];
// no need to minifiy minified files
if (substr($file, -strlen('.min.js')) === '.min.js') {
return new FileAsset($root . '/' . $file, array(), $root, $file);
}
return new FileAsset($root . '/' . $file, array(
new JSMinFilter()
), $root, $file);
}, $jsFiles);
$jsCollection = new AssetCollection($jsFiles);
$jsCollection->setTargetPath("assets/$jsHash.js");
@ -173,6 +192,7 @@ class OC_TemplateLayout extends OC_Template {
$assetPath,
array(
new CssRewriteFilter(),
new CssMinFilter(),
new CssImportFilter()
),
$sourceRoot,
@ -194,8 +214,8 @@ class OC_TemplateLayout extends OC_Template {
* @param array $files
* @return string
*/
private static function hashScriptNames($files)
{
private static function hashScriptNames($files) {
$files = array_map(function ($item) {
$root = $item[0];
$file = $item[2];
@ -204,36 +224,7 @@ class OC_TemplateLayout extends OC_Template {
sort($files);
// include the apps' versions hash to invalidate the cached assets
$files[]= self::$versionHash;
$files[] = self::$versionHash;
return hash('md5', implode('', $files));
}
/**
* @return bool
*/
private function isAssetPipelineEnabled() {
// asset management enabled?
$useAssetPipeline = OC_Config::getValue('asset-pipeline.enabled', false);
if (!$useAssetPipeline) {
return false;
}
// assets folder exists?
$assetDir = \OC::$SERVERROOT . '/assets';
if (!is_dir($assetDir)) {
if (!mkdir($assetDir)) {
\OCP\Util::writeLog('assets',
"Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR);
return false;
}
}
// assets folder can be accessed?
if (!touch($assetDir."/.oc")) {
\OCP\Util::writeLog('assets',
"Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR);
return false;
}
return $useAssetPipeline;
}
}

30
lib/repair/assetcache.php Normal file
View File

@ -0,0 +1,30 @@
<?php
/**
* Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Repair;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use OC\Hooks\BasicEmitter;
class AssetCache extends BasicEmitter implements \OC\RepairStep {
public function getName() {
return 'Clear asset cache after upgrade';
}
public function run() {
if (!\OC_Template::isAssetPipelineEnabled()) {
$this->emit('\OC\Repair', 'info', array('Asset pipeline disabled -> nothing to do'));
return;
}
$assetDir = \OC::$SERVERROOT . '/assets';
\OC_Helper::rmdirr($assetDir, false);
$this->emit('\OC\Repair', 'info', array('Asset cache cleared.'));
}
}