1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Fabiang\AsseticBundle\View; |
6
|
|
|
|
7
|
|
|
use Assetic\Asset\AssetCollection; |
8
|
|
|
use Assetic\Contracts\Asset\AssetInterface; |
9
|
|
|
use Laminas\View\Helper\HeadLink; |
10
|
|
|
use Laminas\View\Helper\HeadScript; |
11
|
|
|
use Laminas\View\Helper\InlineScript; |
12
|
|
|
use Laminas\View\Renderer\PhpRenderer; |
13
|
|
|
|
14
|
|
|
use function pathinfo; |
15
|
|
|
use function strpos; |
16
|
|
|
use function strtolower; |
17
|
|
|
|
18
|
|
|
use const PATHINFO_EXTENSION; |
19
|
|
|
|
20
|
|
|
class ViewHelperStrategy extends AbstractStrategy |
21
|
|
|
{ |
22
|
|
|
public function setupAsset(AssetInterface $asset): void |
23
|
|
|
{ |
24
|
|
|
$baseUrl = $this->str($this->getBaseUrl()); |
25
|
|
|
$basePath = $this->str($this->getBasePath()); |
26
|
|
|
|
27
|
|
|
if ($this->isDebug() && ! $this->isCombine() && $asset instanceof AssetCollection) { |
28
|
|
|
// Move assets as single instance not as a collection |
29
|
|
|
/** @var AssetCollection $value */ |
30
|
|
|
foreach ($asset as $value) { |
31
|
|
|
$path = $baseUrl . $basePath . $this->str($value->getTargetPath()); |
32
|
|
|
$this->helper($path); |
33
|
|
|
} |
34
|
|
|
} else { |
35
|
|
|
$path = $baseUrl . $basePath . $this->str($asset->getTargetPath()); |
36
|
|
|
$this->helper($path); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function str(?string $s): string |
41
|
|
|
{ |
42
|
|
|
return $s ?? ''; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function helper(string $path): void |
46
|
|
|
{ |
47
|
|
|
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
|
|
|
|
48
|
|
|
|
49
|
|
|
switch ($extension) { |
50
|
|
|
case 'js': |
51
|
|
|
$this->appendScript($path); |
52
|
|
|
break; |
53
|
|
|
|
54
|
|
|
case 'css': |
55
|
|
|
$this->appendStylesheet($path); |
56
|
|
|
break; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function appendScript(string $path): void |
61
|
|
|
{ |
62
|
|
|
$renderer = $this->getRenderer(); |
63
|
|
|
switch (true) { |
64
|
|
|
case $renderer instanceof PhpRenderer: |
65
|
|
|
if (strpos($path, "head_") !== false) { |
66
|
|
|
/** @var HeadScript $headScript */ |
67
|
|
|
$headScript = $renderer->plugin('HeadScript'); |
68
|
|
|
$headScript->appendFile($path); |
69
|
|
|
} else { |
70
|
|
|
/** @var InlineScript $inlineScript */ |
71
|
|
|
$inlineScript = $renderer->plugin('InlineScript'); |
72
|
|
|
$inlineScript->appendFile($path); |
73
|
|
|
} |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function appendStylesheet(string $path): void |
79
|
|
|
{ |
80
|
|
|
$renderer = $this->getRenderer(); |
81
|
|
|
switch (true) { |
82
|
|
|
case $renderer instanceof PhpRenderer: |
83
|
|
|
/** @var HeadLink $headLink */ |
84
|
|
|
$headLink = $renderer->plugin('HeadLink'); |
85
|
|
|
$headLink->appendStylesheet($path); |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|