1
|
|
|
<?php |
2
|
|
|
namespace Mouf\Html\Utils\WebLibraryManager; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* A WebpackWebLibrary represents a set of CSS and JS files that can be integrated into your web application |
6
|
|
|
* with the help of a manifest.json file. |
7
|
|
|
* |
8
|
|
|
* @author Julien Neuhart |
9
|
|
|
*/ |
10
|
|
|
class WebpackWebLibrary implements WebLibraryInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The path to the JSON manifest file, relative to ROOT_PATH. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $jsonManifestPath; |
18
|
|
|
|
19
|
|
|
/** @var null|array<string,string[]> */ |
20
|
|
|
private $filesPaths = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* List of libraries this library depends on. |
24
|
|
|
* |
25
|
|
|
* @var array<WebLibraryInterface> |
26
|
|
|
*/ |
27
|
|
|
private $dependencies = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
* |
32
|
|
|
* @param string $jsonManifestPath The path to the JSON manifest file, relative to ROOT_PATH. |
33
|
|
|
*/ |
34
|
|
|
public function __construct($jsonManifestPath) { |
35
|
|
|
$this->jsonManifestPath = $jsonManifestPath; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns an array of Javascript files to be included for this library. |
40
|
|
|
* |
41
|
|
|
* @return array<string> |
42
|
|
|
* @throws \Exception |
43
|
|
|
*/ |
44
|
|
|
public function getJsFiles() { |
45
|
|
|
$filesPaths = $this->readManifestFile(); |
46
|
|
|
return $filesPaths['js']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns an array of CSS files to be included for this library. |
52
|
|
|
* |
53
|
|
|
* @return array<string> |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
public function getCssFiles() { |
57
|
|
|
$filesPaths = $this->readManifestFile(); |
58
|
|
|
return $filesPaths['css']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns a list of libraries that must be included before this library is included. |
63
|
|
|
* |
64
|
|
|
* @return array<WebLibraryInterface> |
65
|
|
|
*/ |
66
|
|
|
public function getDependencies() { |
67
|
|
|
return $this->dependencies; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns a list of features provided by this library. |
72
|
|
|
* A feature is typically a string describing what the file contains. |
73
|
|
|
* |
74
|
|
|
* For instance, an object representing the JQuery library would provide the "jquery" feature. |
75
|
|
|
* |
76
|
|
|
* @return array<string> |
77
|
|
|
*/ |
78
|
|
|
public function getFeatures() { |
79
|
|
|
throw new \Exception("Not implemented yet!"); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Reads the manifest file and returns an array |
84
|
|
|
* of CSS (key = 'css') and JS (key = 'js') files paths. |
85
|
|
|
* |
86
|
|
|
* @return array<string,string[]> |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
private function readManifestFile() { |
90
|
|
|
if (!empty($this->filesPaths)) { |
91
|
|
|
return $this->filesPaths; |
92
|
|
|
} |
93
|
|
|
$content = file_get_contents(ROOT_PATH . $this->jsonManifestPath); |
94
|
|
|
if ($content === false) { |
95
|
|
|
throw new \Exception('Unable to read the given JSON manifest file.'); |
96
|
|
|
} |
97
|
|
|
$decoded = json_decode($content, true); |
98
|
|
|
$baseDir = dirname($this->jsonManifestPath); |
99
|
|
|
$this->filesPaths = array(); |
100
|
|
|
$this->filesPaths['css'] = array(); |
101
|
|
|
$this->filesPaths['js'] = array(); |
102
|
|
|
foreach ($decoded as $name => $path) { |
103
|
|
|
$ext = pathinfo($baseDir . $path, PATHINFO_EXTENSION); |
104
|
|
|
if ($ext === 'js') { |
105
|
|
|
$this->filesPaths['js'][] = $baseDir . $path; |
106
|
|
|
} else if ($ext === 'css') { |
107
|
|
|
$this->filesPaths['css'][] = $baseDir . $path; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $this->filesPaths; |
111
|
|
|
} |
112
|
|
|
} |