Minifier.php in Advanced CSS/JS Aggregation 8.4
File
advagg_ext_minify/src/Asset/Minifier.php
View source
<?php
namespace Drupal\advagg_ext_minify\Asset;
use Drupal\advagg\Asset\SingleAssetOptimizerBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use Psr\Log\LoggerInterface;
class Minifier extends SingleAssetOptimizerBase {
protected $root;
protected $in;
protected $out;
protected $file;
public function __construct(string $root, LoggerInterface $logger, ConfigFactoryInterface $config_factory, FileSystemInterface $file) {
parent::__construct($logger);
$this->config = $config_factory
->get('advagg_ext_minify.settings');
$this->file = $file;
$this->in = $file
->realpath($file
->tempnam('public://js/optimized', 'advagg_in'));
$this->out = $file
->realpath($file
->tempnam('public://js/optimized', 'advagg_out'));
$this->root = $root;
}
public function __destruct() {
$this->file
->unlink($this->in);
$this->file
->unlink($this->out);
}
public function js($contents) {
file_put_contents($this->in, $contents);
$output = $this
->execute('js');
$contents = file_get_contents($output);
return $contents;
}
public function css($contents) {
file_put_contents($this->in, $contents);
$output = $this
->execute('css');
$contents = file_get_contents($output);
return $contents;
}
protected function execute($ext) {
$run = $this->config
->get($ext . '_cmd');
$run = str_replace([
'{%CWD%}',
'{%IN%}',
'{%IN_URL_ENC%}',
'{%OUT%}',
], [
$this->root,
$this->in,
urlencode(file_create_url($this->in)),
escapeshellarg(realpath($this->out)),
], $run);
shell_exec($run);
return $this->out;
}
public function optimize($contents, array $asset, array $data) {
return $contents;
}
}
Classes
Name |
Description |
Minifier |
Optimizes a asset via external minifiers. |