You are here

class MinifyJsCommands in Minify JS 8

Same name and namespace in other branches
  1. 8.2 src/Commands/MinifyJsCommands.php \Drupal\minifyjs\Commands\MinifyJsCommands

A Drush commandfile.

In addition to this file, you need a drush.services.yml in root of your module, and a composer.json file that provides the name of the services file to use.

See these files for an example of injecting Drupal services:

Hierarchy

  • class \Drupal\minifyjs\Commands\MinifyJsCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of MinifyJsCommands

1 string reference to 'MinifyJsCommands'
minifyjs.services.yml in ./minifyjs.services.yml
minifyjs.services.yml
1 service uses MinifyJsCommands
minifyjs.commands in ./minifyjs.services.yml
Drupal\minifyjs\Commands\MinifyJsCommands

File

src/Commands/MinifyJsCommands.php, line 20

Namespace

Drupal\minifyjs\Commands
View source
class MinifyJsCommands extends DrushCommands {

  /**
   * Cache.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * MinifyJsCommands constructor.
   */
  public function __construct(CacheBackendInterface $cache) {
    $this->cache = $cache;
  }

  /**
   * All js files minification.
   *
   * @usage drush minify-js
   *   Js files minification.
   *
   * @command minify-js
   * @aliases minifyjs
   */
  public function minifyJs() {
    $this
      ->output()
      ->writeln('Minifying all JS files...');
    $files = minifyjs_load_all_files();
    foreach ($files as $fid => $file) {
      $status = minifyjs_minify_file($fid);

      // Only output error messages.
      if ($status !== TRUE) {
        $this
          ->output()
          ->writeln($status);
      }
    }
    $this->cache
      ->delete(MINIFYJS_CACHE_CID);
    $this
      ->output()
      ->writeln('Complete!');
  }

  /**
   * Minify all JS files that are not currently minified.
   *
   * @usage drush minify-js-skip
   *   Js files minification.
   *
   * @command minify-js-skip
   * @aliases minifyjslite
   */
  public function minifyJsSkip() {
    $this
      ->output()
      ->writeln('Minifying all JS files not currently minified...');
    $files = minifyjs_load_all_files();
    foreach ($files as $fid => $file) {
      if (!empty($file->minified_uri)) {
        $status = minifyjs_minify_file($fid);

        // Only output error messages.
        if ($status !== TRUE) {
          $this
            ->output()
            ->writeln($status);
        }
      }
    }
    $this->cache
      ->delete(MINIFYJS_CACHE_CID);
    $this
      ->output()
      ->writeln('Complete!');
  }

  /**
   * Drush command to find all JS files.
   *
   * @usage drush scan-js
   *   Drush command to find all JS files.
   *
   * @command scan-js
   * @aliases scanjs
   */
  public function scanJs() {
    $this
      ->output()
      ->writeln('Scanning for JS files...');
    FileManager::scan(TRUE);
    $this
      ->output()
      ->writeln('Complete!');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MinifyJsCommands::$cache protected property Cache.
MinifyJsCommands::minifyJs public function All js files minification.
MinifyJsCommands::minifyJsSkip public function Minify all JS files that are not currently minified.
MinifyJsCommands::scanJs public function Drush command to find all JS files.
MinifyJsCommands::__construct public function MinifyJsCommands constructor.