|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\AssetsCollector; |
|
6
|
|
|
|
|
7
|
|
|
use Enjoys\AssetsCollector\CollectStrategy\StrategyFactory; |
|
8
|
|
|
use Enjoys\AssetsCollector\Exception\NotAllowedMethods; |
|
9
|
|
|
use Enjoys\AssetsCollector\Render\RenderFactory; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
|
|
12
|
|
|
class Assets |
|
13
|
|
|
{ |
|
14
|
|
|
public const NAMESPACE_COMMON = 'common'; |
|
15
|
|
|
|
|
16
|
|
|
public const RENDER_HTML = 'html'; |
|
17
|
|
|
|
|
18
|
|
|
public const STRATEGY_ONE_FILE = 0; |
|
19
|
|
|
public const STRATEGY_MANY_FILES = 1; |
|
20
|
|
|
|
|
21
|
|
|
/* |
|
22
|
|
|
* @var AssetsCollection |
|
23
|
|
|
*/ |
|
24
|
|
|
private AssetsCollection $assetsCollection; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Environment |
|
28
|
|
|
*/ |
|
29
|
|
|
private Environment $environment; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var LoggerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private LoggerInterface $logger; |
|
35
|
|
|
|
|
36
|
9 |
|
public function __construct(Environment $environment) |
|
37
|
|
|
{ |
|
38
|
9 |
|
$this->environment = $environment; |
|
39
|
9 |
|
$this->logger = $this->environment->getLogger(); |
|
40
|
9 |
|
$this->assetsCollection = new AssetsCollection($this->environment); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $type |
|
45
|
|
|
* @param string|array $paths |
|
46
|
|
|
* @param string $namespace |
|
47
|
|
|
* @param string $method |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
9 |
|
public function add(string $type, $paths, string $namespace = self::NAMESPACE_COMMON, string $method = 'push'): Assets |
|
51
|
|
|
{ |
|
52
|
9 |
|
$collection = new AssetsCollection($this->environment); |
|
53
|
|
|
/** @var array|string $path */ |
|
54
|
9 |
|
foreach ((array)$paths as $path) { |
|
55
|
9 |
|
$params = []; |
|
56
|
9 |
|
if (is_array($path)) { |
|
57
|
1 |
|
$params = $path; |
|
58
|
|
|
|
|
59
|
|
|
/** @var string $path */ |
|
60
|
1 |
|
$path = array_shift($params); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
9 |
|
$collection->add( |
|
64
|
9 |
|
new Asset($type, $path, $params), |
|
65
|
|
|
$namespace |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
9 |
|
if (!in_array($method, ['push', 'unshift'], true)) { |
|
70
|
1 |
|
throw new NotAllowedMethods('Allowed methods only `push` and `unshift`'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
8 |
|
$this->assetsCollection->$method($collection); |
|
74
|
|
|
|
|
75
|
8 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $type |
|
80
|
|
|
* @param string $namespace |
|
81
|
|
|
* @return string |
|
82
|
|
|
* @throws \Exception |
|
83
|
|
|
*/ |
|
84
|
7 |
|
public function get(string $type, string $namespace = self::NAMESPACE_COMMON): string |
|
85
|
|
|
{ |
|
86
|
7 |
|
$paths = $this->getResults($type, $this->assetsCollection->get($type, $namespace)); |
|
87
|
7 |
|
return RenderFactory::getRender(\strtolower($type), $this->environment)->getResult($paths); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $type |
|
93
|
|
|
* @param array<Asset> $assetsCollection |
|
94
|
|
|
* @return string[] |
|
95
|
|
|
* @throws \Exception |
|
96
|
|
|
*/ |
|
97
|
7 |
|
private function getResults(string $type, array $assetsCollection): array |
|
98
|
|
|
{ |
|
99
|
7 |
|
$strategy = StrategyFactory::getStrategy( |
|
100
|
7 |
|
$this->environment, |
|
101
|
|
|
$assetsCollection, |
|
102
|
|
|
$type |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
7 |
|
return $strategy->getResult(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return Environment |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function getEnvironment(): Environment |
|
112
|
|
|
{ |
|
113
|
1 |
|
return $this->environment; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return LoggerInterface |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getLogger(): LoggerInterface |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->logger; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|