From d8d7f8858af37fbfc0da79fe15917f4ce9aaa77b Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Thu, 20 Apr 2017 19:47:04 +0300
Subject: [PATCH] Fix CSS problems in asset pipeline (#957)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- Add a path converter to make any css files minified by the asset pipeline use correct paths for any relative url(…) directives.
---
 .../src/VuFindTheme/Minify/CSS.php            | 54 ++++++++++++++++++
 .../src/VuFindTheme/Minify/PathConverter.php  | 56 +++++++++++++++++++
 .../VuFindTheme/View/Helper/ConcatTrait.php   |  2 +-
 .../src/VuFindTheme/View/Helper/HeadLink.php  |  2 +-
 4 files changed, 112 insertions(+), 2 deletions(-)
 create mode 100644 module/VuFindTheme/src/VuFindTheme/Minify/CSS.php
 create mode 100644 module/VuFindTheme/src/VuFindTheme/Minify/PathConverter.php

diff --git a/module/VuFindTheme/src/VuFindTheme/Minify/CSS.php b/module/VuFindTheme/src/VuFindTheme/Minify/CSS.php
new file mode 100644
index 00000000000..77460d4ef05
--- /dev/null
+++ b/module/VuFindTheme/src/VuFindTheme/Minify/CSS.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * CSS minifier extension
+ *
+ * PHP version 5
+ *
+ * Copyright (C) The National Library of Finland 2017.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind
+ * @package  View_Helpers
+ * @author   Ere Maijala <ere.maijala@helsinki.fi>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+namespace VuFindTheme\Minify;
+
+/**
+ * CSS minifier extensions
+ *
+ * @category VuFind
+ * @package  View_Helpers
+ * @author   Ere Maijala <ere.maijala@helsinki.fi>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:testing:unit_tests Wiki
+ */
+class CSS extends \MatthiasMullie\Minify\CSS
+{
+    /**
+     * Return a converter to update relative paths to be relative to the new
+     * destination.
+     *
+     * @param string $source Source path
+     * @param string $target Target path
+     *
+     * @return \MatthiasMullie\PathConverter\ConverterInterface
+     */
+    protected function getPathConverter($source, $target)
+    {
+        return new PathConverter($source, $target);
+    }
+}
diff --git a/module/VuFindTheme/src/VuFindTheme/Minify/PathConverter.php b/module/VuFindTheme/src/VuFindTheme/Minify/PathConverter.php
new file mode 100644
index 00000000000..1b2254c153f
--- /dev/null
+++ b/module/VuFindTheme/src/VuFindTheme/Minify/PathConverter.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * CSS path converter extension
+ *
+ * PHP version 5
+ *
+ * Copyright (C) The National Library of Finland 2017.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind
+ * @package  View_Helpers
+ * @author   Ere Maijala <ere.maijala@helsinki.fi>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+namespace VuFindTheme\Minify;
+
+/**
+ * CSS path converter extension
+ *
+ * @category VuFind
+ * @package  View_Helpers
+ * @author   Ere Maijala <ere.maijala@helsinki.fi>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+class PathConverter extends \MatthiasMullie\PathConverter\Converter
+{
+    /**
+     * Normalize path.
+     *
+     * @param string $path Path
+     *
+     * @return string
+     */
+    protected function normalize($path)
+    {
+        $path = parent::normalize($path);
+
+        $path = str_replace('/local/cache/public', '/cache', $path);
+
+        return $path;
+    }
+}
diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/ConcatTrait.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/ConcatTrait.php
index f7c7fe6dc2a..24dd1dda584 100644
--- a/module/VuFindTheme/src/VuFindTheme/View/Helper/ConcatTrait.php
+++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/ConcatTrait.php
@@ -24,7 +24,7 @@
  * @package  View_Helpers
  * @author   Demian Katz <demian.katz@villanova.edu>
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
- * @link     https://vufind.org/wiki/development:testing:unit_tests Wiki
+ * @link     https://vufind.org/wiki/development Wiki
  */
 namespace VuFindTheme\View\Helper;
 use VuFindTheme\ThemeInfo;
diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php
index 0fbd23405da..c9478a4ae3d 100644
--- a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php
+++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php
@@ -203,6 +203,6 @@ class HeadLink extends \Zend\View\Helper\HeadLink
      */
     protected function getMinifier()
     {
-        return new \MatthiasMullie\Minify\CSS();
+        return new \VuFindTheme\Minify\CSS();
     }
 }
-- 
GitLab