Skip to content

Commit

Permalink
fix: avoid error if JSqueeze package is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Marín committed Sep 2, 2024
1 parent 92a2531 commit d312b44
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/Tinyfier/JS/Tool.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Patchwork\JSqueeze;

/**
* Compression and processing routines for Javascript code
*/
Expand All @@ -16,7 +14,7 @@ abstract class Tinyfier_JS_Tool
* 'gclosure': allow to use the external google closure compiler
*
* @param string $source
* @param array $settings
* @param array $settings
*
* @return string
*/
Expand All @@ -43,16 +41,18 @@ public static function process(string $source, array $settings = [], &$errors =
//Compile using JSqueeze
if ($settings['pretty']) {
return $source;
} else {
} elseif (class_exists('\Patchwork\JSqueeze')) {
try {
$jz = new JSqueeze;
$jz = new \Patchwork\JSqueeze;
$result = $jz->squeeze($source, true, false, false);
} catch (Exception $e) {
$errors[] = $e->getMessage();
return $source;
}

return $result;
} else {
return $source;
}
}

Expand All @@ -63,21 +63,21 @@ public static function process(string $source, array $settings = [], &$errors =
public static function default_settings(): array
{
return [
'external_services' => true, //Use external compressors (like gclosure)
'external_services' => true, //Use external compressors (like gclosure)
'external_services_min_length' => 750, //Min source length to use external compressors (to avoid too many calls)
'gclosure' => true,
'level' => self::LEVEL_SIMPLE_OPTIMIZATIONS,
'pretty' => false
'gclosure' => true,
'level' => self::LEVEL_SIMPLE_OPTIMIZATIONS,
'pretty' => false,
];
}

/**
* Compiles javascript code using the Google Closure Compiler API
* @see http://code.google.com/intl/es/closure/compiler/docs/api-ref.html
*
* @param string $source
* @param string $source
* @param int|string $level One of LEVEL_* constants
* @param bool $pretty
* @param bool $pretty
*
* @return mixed Code compressed, FALSE if error
*/
Expand All @@ -87,19 +87,19 @@ private static function _compress_google_closure(
bool $pretty = false,
&$errors = [],
&$warnings = null,
$extra_settings = []
$extra_settings = [],
): mixed {
if (!function_exists('curl_exec')) {
return false;
}

//Generate POST data
$post = $extra_settings + [
'output_info' => 'compiled_code',
'output_format' => 'json',
'warning_level' => isset($warnings) ? 'VERBOSE' : 'QUIET',
'output_info' => 'compiled_code',
'output_format' => 'json',
'warning_level' => isset($warnings) ? 'VERBOSE' : 'QUIET',
'compilation_level' => $level,
'js_code' => $source,
'js_code' => $source,
];
if ($pretty) {
$post['formatting'] = 'pretty_print';
Expand Down

0 comments on commit d312b44

Please sign in to comment.