-
Notifications
You must be signed in to change notification settings - Fork 1
/
Collection.php
116 lines (91 loc) · 2.95 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Diversity;
/**
* Representing the bulk of all Components used in one page or template.
*
* On a page using Components, this Collection can be used to get all scripts, angular bootstrap,
* and whatnot.
*/
class Collection {
private $components = array();
public function __construct() {}
public function add(Component $component) { $this->components[$component->name] = $component; }
/**
* @return array List of scripts (URLs or filenames).
*/
public function getScripts() {
$scripts = array();
foreach ($this->getAllComponents() as $component) {
$scripts = array_merge($scripts, $component->getScripts());
}
return $scripts;
}
public function getStyles() {
$styles = array();
foreach ($this->getAllComponents() as $component) {
$styles = array_merge($styles, $component->getStyles());
}
return $styles;
}
public function renderScriptTags() {
$tags = '';
foreach ($this->getScripts() as $script) {
/// @todo Fetch and minify proprietary scripts.
$tags .= '<script src="' . htmlspecialchars($script) . '"></script>' . "\n";
}
return $tags;
}
public function renderStyleTags() {
$tags = '';
foreach ($this->getStyles() as $style) {
/// @todo Fetch and minify proprietary styles.
$tags .= '<link rel="stylesheet" type="text/css" '
. 'href="' . htmlspecialchars($style) . '"></link>' . "\n";
}
return $tags;
}
/**
* Return an ordered array of all components. Dependencies up front before their dependees.
*/
public function getAllComponents() {
$all_components = array();
foreach ($this->components as $component) {
$all_components[$component->name] = $component;
$all_components = array_merge($all_components, $component->getDependencies());
}
// First we reverse the list to get the correct dependency oredering, i.e. deps upfront
$all_components = array_reverse($all_components);
// Then we need to remove duplicate components, while keeping order.
$lookup = array();
$filtered = array();
foreach ($all_components as $component) {
if (!isset($lookup[$component->name])) {
$lookup[$component->name] = true;
$filtered[] = $component;
}
}
return $filtered;
}
public function needsAngular() {
foreach ($this->getAllComponents() as $component) {
if (isset($component->spec->angular)) return true;
}
return false;
}
/**
* @return string JavaScript with the angular bootstrap call.
*/
public function renderAngularBootstrap() {
return 'angular.bootstrap(document, ' . json_encode($this->getAngularModules()) . ");\n";
}
/**
* @return array Flat array of module names.
*/
public function getAngularModules() {
$modules = array();
foreach ($this->getAllComponents() as $component) {
if (isset($component->spec->angular)) $modules[] = $component->spec->angular;
}
return $modules;
}
}