Skip to content

Commit

Permalink
Factor CSS theme handling to AssetPipeline.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jan 30, 2025
1 parent 32a7fae commit 23f7fb4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 27 deletions.
2 changes: 1 addition & 1 deletion module/VuFindTheme/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function getViewHelperConfig()
{
return [
'factories' => [
View\Helper\AssetPipeline::class => InvokableFactory::class,
View\Helper\AssetPipeline::class => View\Helper\AssetPipelineFactory::class,
View\Helper\FootScript::class => View\Helper\PipelineInjectorFactory::class,
View\Helper\ImageLink::class => View\Helper\ImageLinkFactory::class,
View\Helper\HeadLink::class => View\Helper\PipelineInjectorFactory::class,
Expand Down
26 changes: 26 additions & 0 deletions module/VuFindTheme/src/VuFindTheme/View/Helper/AssetPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace VuFindTheme\View\Helper;

use VuFindTheme\ThemeInfo;

/**
* Asset pipeline view helper.
*
Expand All @@ -40,6 +42,8 @@
*/
class AssetPipeline extends \Laminas\View\Helper\AbstractHelper
{
use RelativePathTrait;

/**
* Array of accumulated scripts, indexed by position (header/footer).
*
Expand All @@ -61,6 +65,15 @@ class AssetPipeline extends \Laminas\View\Helper\AbstractHelper
*/
protected $stylesheets = [];

/**
* Constructor
*
* @param ThemeInfo $themeInfo Theme information helper
*/
public function __construct(protected ThemeInfo $themeInfo)
{
}

/**
* Add raw CSS to the pipeline.
*
Expand Down Expand Up @@ -257,6 +270,19 @@ protected function outputStyleAssets(): string
{
$headLink = $this->getView()->plugin('headLink');
foreach ($this->stylesheets as $sheet) {
// Account for the theme system (when appropriate):
if ($this->isRelativePath($sheet['href'])) {
$relPath = 'css/' . $sheet['href'];
$details = $this->themeInfo->findContainingTheme($relPath, ThemeInfo::RETURN_ALL_DETAILS);
if (!empty($details)) {
$urlHelper = $this->getView()->plugin('url');
$url = $urlHelper('home') . "themes/{$details['theme']}/" . $relPath;
$url .= strstr($url, '?') ? '&_=' : '?_=';
$url .= filemtime($details['path']);
$sheet['href'] = $url;
}
}

$headLink->appendStylesheet(
$sheet['href'],
$sheet['media'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* Factory for AssetPipeline view helper.
*
* PHP version 8
*
* Copyright (C) Villanova University 2025.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Theme
* @author Demian Katz <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/

namespace VuFindTheme\View\Helper;

use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;

/**
* Factory for AssetPipeline view helper.
*
* @category VuFind
* @package Theme
* @author Demian Katz <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
class AssetPipelineFactory implements FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container Service manager
* @param string $requestedName Service being created
* @param null|array $options Extra options (optional)
*
* @return object
*
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException&\Throwable if any other error occurs
*/
public function __invoke(
ContainerInterface $container,
$requestedName,
?array $options = null
) {
if (!empty($options)) {
throw new \Exception('Unexpected options sent to factory.');
}
return new $requestedName(
$container->get(\VuFindTheme\ThemeInfo::class)
);
}
}
26 changes: 0 additions & 26 deletions module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,6 @@ protected function getFileType()
return 'css';
}

/**
* Create HTML link element from data item
*
* @param stdClass $item data item
*
* @return string
*/
public function itemToString(stdClass $item)
{
// Normalize href to account for themes (if appropriate), then call the parent class:
if (isset($item->href) && $this->isRelativePath($item->href)) {
$relPath = 'css/' . $item->href;
$details = $this->themeInfo
->findContainingTheme($relPath, ThemeInfo::RETURN_ALL_DETAILS);
if (!empty($details)) {
$urlHelper = $this->getView()->plugin('url');
$url = $urlHelper('home') . "themes/{$details['theme']}/" . $relPath;
$url .= strstr($url, '?') ? '&_=' : '?_=';
$url .= filemtime($details['path']);
$item->href = $url;
}
}
$this->addNonce($item);
return parent::itemToString($item);
}

/**
* Returns true if file should not be included in the compressed concat file
* Required by ConcatTrait
Expand Down

0 comments on commit 23f7fb4

Please sign in to comment.