Commit 6b882d39 authored by George's avatar George Committed by Scott Joudry
Browse files

Issue #3230610 by sriharsha.uppuluri, geoanders: Minify Settings Ignore Admin Pages

parent 2b0794eb
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
strip_comments: true
minify: false
exclude_pages: ''
+3 −0
Original line number Diff line number Diff line
@@ -8,3 +8,6 @@ minifyhtml.config:
    minify:
      type: boolean
      label: 'Minified Source HTML'
    exclude_pages:
      type: string
      label: 'Exclude From Pages'
+34 −8
Original line number Diff line number Diff line
@@ -47,7 +47,13 @@ function minifyhtml_form_system_performance_settings_alter(array &$form, FormSta

  $form['bandwidth_optimization']['strip_comments'] = [
    '#title' => t('Strip comments from the source HTML'),
    '#description'   => t('If checked, strip HTML comments and multi-line comments in @script and @style tags.', ['@script' => '<script>', '@style' => '<style>']),
    '#description' => t(
      'If checked, strip HTML comments and multi-line comments in @script and @style tags.',
      [
        '@script' => '<script>',
        '@style' => '<style>',
      ]
    ),
    '#type' => 'checkbox',
    '#default_value' => $config->get('strip_comments'),
    '#states' => [
@@ -58,6 +64,25 @@ function minifyhtml_form_system_performance_settings_alter(array &$form, FormSta
    '#access' => $access,
  ];

  $form['bandwidth_optimization']['exclude_pages'] = [
    '#type' => 'textarea',
    '#title' => t('Disable minification for specific pages'),
    '#default_value' => $config->get('exclude_pages'),
    '#description' => t(
        "Specify pages by using their paths with leading forward slash. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.",
        [
          '%blog' => '/blog',
          '%blog-wildcard' => '/blog/*',
          '%front' => '<front>',
        ]
    ),
    '#states' => [
      'visible' => [
        ':input[name="minifyhtml_minify"]' => ['checked' => TRUE],
      ],
    ],
  ];

  if ($access) {
    $form['#submit'][] = 'minifyhtml_form_system_performance_settings_submit';
  }
@@ -69,6 +94,7 @@ function minifyhtml_form_system_performance_settings_alter(array &$form, FormSta
function minifyhtml_form_system_performance_settings_submit(array $form, FormStateInterface $form_state) {
  \Drupal::configFactory()->getEditable('minifyhtml.config')
    ->set('strip_comments', $form_state->getValue('strip_comments'))
    ->set('exclude_pages', $form_state->getValue('exclude_pages'))
    ->set('minify', $form_state->getValue('minifyhtml_minify'))
    ->save();
}
+9 −2
Original line number Diff line number Diff line
services:
  minifyhtml_exit:
    class: Drupal\minifyhtml\EventSubscriber\MinifyHTMLExit
    arguments: ['@config.factory', '@datetime.time', '@logger.factory']
    arguments:
      [
        '@config.factory',
        '@datetime.time',
        '@logger.factory',
        '@path.matcher',
        '@path.current',
      ]
    tags:
      - { name: event_subscriber }
+45 −21
Original line number Diff line number Diff line
@@ -13,6 +13,27 @@ use Symfony\Component\HttpKernel\KernelEvents;
 */
class MinifyHTMLExit implements EventSubscriberInterface {

  /**
   * Abort flag, when dependencies have not been injected.
   *
   * @var bool
   */
  protected $abort = FALSE;

  /**
   * Config Factory object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * The current path.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPath;

  /**
   * The content that this class minifies.
   *
@@ -21,25 +42,25 @@ class MinifyHTMLExit implements EventSubscriberInterface {
  protected $content;

  /**
   * A list of placeholders for HTML elements that won't be minified.
   * Logger Factory object.
   *
   * @var array
   * @var \Drupal\Core\Logger\LoggerChannelFactory
   */
  protected $placeholders = [];
  protected $logger;

  /**
   * The placeholder token.
   * The path matcher.
   *
   * @var string
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  protected $token;
  protected $pathMatcher;

  /**
   * Config Factory object.
   * A list of placeholders for HTML elements that won't be minified.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   * @var array
   */
  protected $config;
  protected $placeholders = [];

  /**
   * Time object.
@@ -49,18 +70,11 @@ class MinifyHTMLExit implements EventSubscriberInterface {
  protected $time;

  /**
   * Logger Factory object.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactory
   */
  protected $logger;

  /**
   * Abort flag, when dependencies have not been injected.
   * The placeholder token.
   *
   * @var bool
   * @var string
   */
  protected $abort = FALSE;
  protected $token;

  /**
   * Constructs a MinifyHTMLExit object.
@@ -68,17 +82,20 @@ class MinifyHTMLExit implements EventSubscriberInterface {
  public function __construct() {

    // To prevent warnings thrown by func_get_arg(), only attempt to get the
    // args if there are exactly 3.
    // args if there are exactly 5.
    $arg_error = TRUE;
    if (func_num_args() == 3) {
    if (func_num_args() == 5) {

      // Assigning the arguments this way prevents a signature mismatch
      // exception that will occur until the cache is cleared to update the
      // service definition. However, the cache cannot be cleared due to the
      // exception.
      // @todo create proper signature in 2.0.0 version.
      $this->config = func_get_arg(0);
      $this->time = func_get_arg(1);
      $this->logger = func_get_arg(2);
      $this->pathMatcher = func_get_arg(3);
      $this->currentPath = func_get_arg(4);

      if ($this->config) {
        $this->token = 'MINIFYHTML_' . md5($this->time->getRequestTime());
@@ -101,6 +118,13 @@ class MinifyHTMLExit implements EventSubscriberInterface {
   */
  public function response(FilterResponseEvent $event) {
    if (!$this->abort && $this->config->get('minifyhtml.config')->get('minify')) {

      // Skip excluded pages.
      $pages = $this->config->get('minifyhtml.config')->get('exclude_pages');
      if (!empty($pages) && $this->pathMatcher->matchPath($this->currentPath->getPath(), \mb_strtolower($pages))) {
        return;
      }

      $response = $event->getResponse();

      // Make sure that the following render classes are the only ones that