|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace PTS\StaticManager; |
|
4
|
|
|
|
|
5
|
|
|
use PTS\Tools\CollectionInterface; |
|
6
|
|
|
use PTS\Tools\NotFoundKeyException; |
|
7
|
|
|
use Symfony\Component\Asset\PackageInterface; |
|
8
|
|
|
|
|
9
|
|
|
class StaticManager |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var CollectionInterface[] */ |
|
12
|
|
|
protected $collections; |
|
13
|
|
|
/** @var PackageInterface[] */ |
|
14
|
|
|
protected $packages = []; |
|
15
|
|
|
|
|
16
|
10 |
|
public function __construct(CollectionInterface $emptyCollection) |
|
17
|
|
|
{ |
|
18
|
10 |
|
$this->collections = [ |
|
|
|
|
|
|
19
|
10 |
|
'css' => $emptyCollection, |
|
20
|
10 |
|
'jsHeader' => clone $emptyCollection, |
|
21
|
10 |
|
'jsFooter' => clone $emptyCollection, |
|
22
|
|
|
]; |
|
23
|
10 |
|
} |
|
24
|
|
|
|
|
25
|
3 |
|
public function getCssSet() : CollectionInterface |
|
26
|
|
|
{ |
|
27
|
3 |
|
return $this->collections['css']; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
public function getJsHeaderSet() : CollectionInterface |
|
31
|
|
|
{ |
|
32
|
3 |
|
return $this->collections['jsHeader']; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
public function getJsFooterSet() : CollectionInterface |
|
36
|
|
|
{ |
|
37
|
3 |
|
return $this->collections['jsFooter']; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public function drawHeaderScripts() : string |
|
41
|
|
|
{ |
|
42
|
2 |
|
$collection = $this->getJsHeaderSet(); |
|
43
|
2 |
|
return $this->drawScripts($collection); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function drawFooterScripts() : string |
|
47
|
|
|
{ |
|
48
|
2 |
|
$collection = $this->getJsFooterSet(); |
|
49
|
2 |
|
return $this->drawScripts($collection); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
4 |
|
protected function drawScripts(CollectionInterface $collection) : string |
|
53
|
|
|
{ |
|
54
|
4 |
|
$scripts = $collection->getFlatItems(true); |
|
55
|
|
|
|
|
56
|
4 |
|
$result = ''; |
|
57
|
4 |
|
foreach ($scripts as $script) { |
|
58
|
4 |
|
$result .= "<script src='" . $script . "'></script>\n"; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
return $result; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public function drawStyles() : string |
|
65
|
|
|
{ |
|
66
|
2 |
|
$collection = $this->getCssSet(); |
|
67
|
2 |
|
$styles = $collection->getFlatItems(true); |
|
68
|
|
|
|
|
69
|
2 |
|
$result = ''; |
|
70
|
2 |
|
foreach ($styles as $style) { |
|
71
|
2 |
|
$result .= "<link rel='stylesheet' href='" . $style . "' />\n"; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function setPackage(string $name, PackageInterface $package) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->packages[$name] = $package; |
|
80
|
1 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $name |
|
85
|
|
|
* @return PackageInterface |
|
86
|
|
|
* @throws NotFoundKeyException |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function getPackage(string $name) : PackageInterface |
|
89
|
|
|
{ |
|
90
|
2 |
|
if (!array_key_exists($name, $this->packages)) { |
|
91
|
1 |
|
throw new NotFoundKeyException('Package not found'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
return $this->packages[$name]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..