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