You are here

class Minifier in Advanced CSS/JS Aggregation 8.3

Same name in this branch
  1. 8.3 advagg_js_minify/jshrink.inc \JShrink\Minifier
  2. 8.3 advagg_ext_minify/src/Asset/Minifier.php \Drupal\advagg_ext_minify\Asset\Minifier
Same name and namespace in other branches
  1. 8.4 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

1 string reference to 'Minifier'
advagg_ext_minify.services.yml in advagg_ext_minify/advagg_ext_minify.services.yml
advagg_ext_minify/advagg_ext_minify.services.yml
1 service uses Minifier
advagg.ext_minifier in advagg_ext_minify/advagg_ext_minify.services.yml
Drupal\advagg_ext_minify\Asset\Minifier

File

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

Namesort descending Modifiers Type Description Overrides
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.