Same name in this branch
  1. 5.0.x advagg_js_minify/jshrink.inc \JShrink\Minifier
Same name in other branches
  1. 6.0.x advagg_js_minify/jshrink.inc \JShrink\Minifier
  2. 6.0.x advagg_ext_minify/src/Asset/Minifier.php \Drupal\advagg_ext_minify\Asset\Minifier
  3. 7.x-2.x advagg_js_compress/jshrink.inc \JShrink\Minifier
  4. 8.x-2.x advagg_js_minify/jshrink.inc \JShrink\Minifier
  5. 8.x-3.x advagg_js_minify/jshrink.inc \JShrink\Minifier
  6. 8.x-3.x advagg_ext_minify/src/Asset/Minifier.php \Drupal\advagg_ext_minify\Asset\Minifier
  7. 8.x-4.x advagg_js_minify/jshrink.inc \JShrink\Minifier
  8. 8.x-4.x advagg_ext_minify/src/Asset/Minifier.php \Drupal\advagg_ext_minify\Asset\Minifier

Optimizes a asset via external minifiers.

Hierarchy

Expanded class hierarchy of Minifier

7 string references to 'Minifier'
advagg_css_minify_requirements dans advagg_css_minify/advagg_css_minify.install
Implements hook_requirements().
advagg_ext_minify.services.yml dans advagg_ext_minify/advagg_ext_minify.services.yml
advagg_ext_minify/advagg_ext_minify.services.yml
advagg_js_minify_requirements dans advagg_js_minify/advagg_js_minify.install
Implements hook_requirements().
CssMinifier::optimize dans advagg_css_minify/src/Asset/CssMinifier.php
Optimize the asset's content.
JsMinifier::optimize dans advagg_js_minify/src/Asset/JsMinifier.php
Optimize the asset's content.

... See full list

1 service uses Minifier
advagg.ext_minifier dans advagg_ext_minify/advagg_ext_minify.services.yml
Drupal\advagg_ext_minify\Asset\Minifier

Fichier

advagg_ext_minify/src/Asset/Minifier.php, line 13

Namespace

Drupal\advagg_ext_minify\Asset
View source
class Minifier extends SingleAssetOptimizerBase {
    
    /**
     * Gets the app root.
     *
     * @var string
     */
    protected $root;
    
    /**
     * Temporary file path to read data from in the command line.
     *
     * @var string
     */
    protected $in;
    
    /**
     * Temporary file path to write data to in the command line.
     *
     * @var string
     */
    protected $out;
    
    /**
     * File System Service.
     *
     * @var \Drupal\Core\File\FileSystemInterface
     */
    protected $file;
    
    /**
     * Construct the optimizer instance.
     *
     * @param string $root
     *   Gets the app root.
     * @param \Psr\Log\LoggerInterface $logger
     *   The logger service.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   A config factory for retrieving required config objects.
     * @param \Drupal\Core\File\FileSystemInterface $file
     *   The filesystem service.
     */
    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;
    }
    
    /**
     * Destructor to clean up temporary files.
     */
    public function __destruct() {
        $this->file
            ->unlink($this->in);
        $this->file
            ->unlink($this->out);
    }
    
    /**
     * Minify Javascript using via command line.
     *
     * @param string $contents
     *   The JavaScript to minify.
     *
     * @return string|bool
     *   The process JavaScript or FALSE on failure.
     */
    public function js($contents) {
        file_put_contents($this->in, $contents);
        $output = $this->execute('js');
        $contents = file_get_contents($output);
        return $contents;
    }
    
    /**
     * Minify CSS using via command line.
     *
     * @param string $contents
     *   The CSS to minify.
     *
     * @return string
     *   The processed CSS or FALSE on failure.
     */
    public function css($contents) {
        file_put_contents($this->in, $contents);
        $output = $this->execute('css');
        $contents = file_get_contents($output);
        return $contents;
    }
    
    /**
     * Run the command line.
     *
     * @param string $ext
     *   The string css or js.
     *
     * @return string
     *   The file containing the minified data.
     */
    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);
        // Run command and return the output file.
        shell_exec($run);
        return $this->out;
    }
    
    /**
     * {@inheritdoc}
     */
    public function optimize($contents, array $asset, array $data) {
        return $contents;
    }

}

Members

Titre Trier par ordre décroissant Modifiers Object type Résumé Overriden Title
Minifier::$file protected property File System Service.
Minifier::$in protected property Temporary file path to read data from in the command line.
Minifier::$out protected property Temporary file path to write data to in the command line.
Minifier::$root protected property Gets the app root.
Minifier::css public function Minify CSS using via command line.
Minifier::execute protected function Run the command line.
Minifier::js public function Minify Javascript using via command line.
Minifier::optimize public function Optimize the asset's content. Overrides SingleAssetOptimizerBase::optimize
Minifier::__construct public function Construct the optimizer instance. Overrides SingleAssetOptimizerBase::__construct
Minifier::__destruct public function Destructor to clean up temporary files.
SingleAssetOptimizerBase::$config protected property A config object for optimizer.
SingleAssetOptimizerBase::$logger protected property Logger service.
SingleAssetOptimizerBase::addLicense public function If configured, add licence string to top/bottom of file.
SingleAssetOptimizerBase::isMinificationSuccess protected function Check if minification was successful before saving changes.
SingleAssetOptimizerBase::isMinified protected function Check if the asset is already minified.
SingleAssetOptimizerBase::stringContainsMultibyteCharacters protected function Checks if string contains multibyte characters.