You are here

RemoveConsoleLog.php in Advanced CSS/JS Aggregation 8.3

Same filename and directory in other branches
  1. 8.4 advagg_mod/src/Asset/RemoveConsoleLog.php

File

advagg_mod/src/Asset/RemoveConsoleLog.php
View source
<?php

namespace Drupal\advagg_mod\Asset;


/**
 * Remove console.log() calls from JavaScript Files.
 */
class RemoveConsoleLog {

  /**
   * Scan asset contents for console.log() calls and remove them.
   *
   * @param string $contents
   *   The asset contents.
   *
   * @return string
   *   The updated contents.
   */
  public function optimize($contents) {
    $pattern = "/ console.log(.*)/";
    return preg_replace_callback($pattern, [
      $this,
      'removeCallback',
    ], $contents);
  }

  /**
   * Remove the console.log() call.
   *
   * @param array $matches
   *   Array of matches from preg_replace_callback().
   *
   * @return string
   *   Replaced string.
   */
  protected function removeCallback(array $matches) {
    return '';
  }

}

Classes

Namesort descending Description
RemoveConsoleLog Remove console.log() calls from JavaScript Files.