Skip to content

Commit

Permalink
Refactor CSS processing to reduce helper calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jan 30, 2025
1 parent 1d11278 commit 70b6c80
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
46 changes: 38 additions & 8 deletions module/VuFindTheme/src/VuFindTheme/View/Helper/AssetPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@
class AssetPipeline extends \Laminas\View\Helper\AbstractHelper
{
/**
* Array of accumulated scripts.
* Array of accumulated styles.
*
* @return
* @var array
*/
protected $styles = [];

/**
* Array of accumulated stylesheets.
*
* @var array
*/
protected $stylesheets = [];

/**
* Add raw CSS to the pipeline.
*
Expand All @@ -55,7 +64,7 @@ class AssetPipeline extends \Laminas\View\Helper\AbstractHelper
*/
public function appendStyle(string $css, array $attributes = []): void
{
$this->getView()->plugin('headStyle')->appendStyle($css, $attributes);
$this->styles[] = compact('css', 'attributes');
}

/**
Expand All @@ -74,7 +83,7 @@ public function appendStylesheet(
string $conditionalStylesheet = '',
array $extras = []
): void {
$this->getView()->plugin('headLink')->appendStylesheet($href, $media, $conditionalStylesheet, $extras);
$this->stylesheets[] = compact('href', 'media', 'conditionalStylesheet', 'extras');
}

/**
Expand All @@ -93,7 +102,13 @@ public function forcePrependStylesheet(
string $conditionalStylesheet = '',
array $extras = []
): void {
$this->getView()->plugin('headLink')->forcePrependStylesheet($href, $media, $conditionalStylesheet, $extras);
$newSheets = [compact('href', 'media', 'conditionalStylesheet', 'extras')];
foreach ($this->stylesheets as $sheet) {
if ($sheet['href'] !== $newSheets[0]['href']) {
$newSheets[] = $sheet;
}
}
$this->stylesheets = $newSheets;
}

/**
Expand All @@ -112,7 +127,7 @@ public function setStylesheet(
string $conditionalStylesheet = '',
array $extras = []
): void {
$this->getView()->plugin('headLink')->setStylesheet($href, $media, $conditionalStylesheet, $extras);
$this->stylesheets = [compact('href', 'media', 'conditionalStylesheet', 'extras')];
}

/**
Expand Down Expand Up @@ -214,8 +229,23 @@ public function prependScript(
*/
public function outputHeaderAssets(): string
{
return ($this->getView()->plugin('headLink'))() . "\n"
. ($this->getView()->plugin('headStyle'))() . "\n"
$headLink = $this->getView()->plugin('headLink');
foreach ($this->stylesheets as $sheet) {
$headLink->appendStylesheet(
$sheet['href'],
$sheet['media'],
$sheet['conditionalStylesheet'],
$sheet['extras']
);
}

$headStyle = $this->getView()->plugin('headStyle');
foreach ($this->styles as $style) {
$headStyle->appendStyle($stye['css'], $style['attributes']);

Check failure on line 244 in module/VuFindTheme/src/VuFindTheme/View/Helper/AssetPipeline.php

View workflow job for this annotation

GitHub Actions / Tests with PHP 8.1

Undefined variable: $stye

Check failure on line 244 in module/VuFindTheme/src/VuFindTheme/View/Helper/AssetPipeline.php

View workflow job for this annotation

GitHub Actions / Tests with PHP 8.3

Undefined variable: $stye
}

return ($headLink)() . "\n"
. ($headStyle)() . "\n"
. ($this->getView()->plugin('headScript'))();
}

Expand Down
27 changes: 0 additions & 27 deletions module/VuFindTheme/src/VuFindTheme/View/Helper/HeadLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,6 @@ public function itemToString(stdClass $item)
return parent::itemToString($item);
}

/**
* Forcibly prepend a stylesheet removing it from any existing position
*
* @param string $href Stylesheet href
* @param string $media Media
* @param string $conditionalStylesheet Any conditions
* @param array $extras Array of extra attributes
*
* @return void
*/
public function forcePrependStylesheet(
$href,
$media = 'screen',
$conditionalStylesheet = '',
$extras = []
) {
// Look for existing entry and remove it if found. Comparison method
// copied from isDuplicate().
foreach ($this->getContainer() as $offset => $item) {
if (($item->rel == 'stylesheet') && ($item->href == $href)) {
$this->offsetUnset($offset);
break;
}
}
parent::prependStylesheet($href, $media, $conditionalStylesheet, $extras);
}

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

0 comments on commit 70b6c80

Please sign in to comment.