Commit | Line | Data |
---|---|---|
98e9fb33 TO |
1 | <?php |
2 | namespace Civi\Angular; | |
3 | ||
4 | class ChangeSet implements ChangeSetInterface { | |
5 | ||
6 | /** | |
7 | * Update a listing of resources. | |
8 | * | |
9 | * @param array $changeSets | |
10 | * Array(ChangeSet). | |
11 | * @param string $resourceType | |
12 | * Ex: 'requires', 'settings' | |
13 | * @param array $resources | |
14 | * The list of resources. | |
15 | * @return mixed | |
16 | */ | |
17 | public static function applyResourceFilters($changeSets, $resourceType, $resources) { | |
18 | if ($resourceType === 'partials') { | |
19 | return self::applyHtmlFilters($changeSets, $resources); | |
20 | } | |
21 | foreach ($changeSets as $changeSet) { | |
22 | /** @var ChangeSet $changeSet */ | |
23 | foreach ($changeSet->resFilters as $filter) { | |
24 | if ($filter['resourceType'] === $resourceType) { | |
25 | $resources = call_user_func($filter['callback'], $resources); | |
26 | } | |
27 | } | |
28 | } | |
29 | return $resources; | |
30 | } | |
31 | ||
32 | /** | |
33 | * Update a set of HTML snippets. | |
34 | * | |
35 | * @param array $changeSets | |
36 | * Array(ChangeSet). | |
37 | * @param array $strings | |
38 | * Array(string $path => string $html). | |
39 | * @return array | |
40 | * Updated list of $strings. | |
41 | * @throws \CRM_Core_Exception | |
42 | */ | |
43 | private static function applyHtmlFilters($changeSets, $strings) { | |
44 | $coder = new Coder(); | |
45 | ||
46 | foreach ($strings as $path => $html) { | |
47 | /** @var \phpQueryObject $doc */ | |
48 | $doc = NULL; | |
49 | ||
50 | // Most docs don't need phpQueryObject. Initialize phpQuery on first match. | |
51 | ||
52 | foreach ($changeSets as $changeSet) { | |
53 | /** @var ChangeSet $changeSet */ | |
54 | foreach ($changeSet->htmlFilters as $filter) { | |
55 | if (preg_match($filter['regex'], $path)) { | |
56 | if ($doc === NULL) { | |
57 | $doc = \phpQuery::newDocument($html, 'text/html'); | |
98e9fb33 TO |
58 | } |
59 | call_user_func($filter['callback'], $doc, $path); | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | if ($doc !== NULL) { | |
65 | $strings[$path] = $coder->encode($doc); | |
66 | } | |
67 | } | |
68 | return $strings; | |
69 | } | |
70 | ||
71 | /** | |
72 | * @var string | |
73 | */ | |
74 | protected $name; | |
75 | ||
76 | /** | |
77 | * @var array | |
78 | * Each item is an array with keys: | |
79 | * - resourceType: string | |
80 | * - callback: function | |
81 | */ | |
c64f69d9 | 82 | protected $resFilters = []; |
98e9fb33 TO |
83 | |
84 | /** | |
85 | * @var array | |
86 | * Each item is an array with keys: | |
87 | * - regex: string | |
88 | * - callback: function | |
89 | */ | |
c64f69d9 | 90 | protected $htmlFilters = []; |
98e9fb33 TO |
91 | |
92 | /** | |
93 | * @param string $name | |
94 | * Symbolic name for this changeset. | |
95 | * @return \Civi\Angular\ChangeSetInterface | |
96 | */ | |
97 | public static function create($name) { | |
98 | $changeSet = new ChangeSet(); | |
99 | $changeSet->name = $name; | |
100 | return $changeSet; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Declare that $module requires additional dependencies. | |
105 | * | |
106 | * @param string $module | |
107 | * @param string|array $dependencies | |
108 | * @return ChangeSet | |
109 | */ | |
110 | public function requires($module, $dependencies) { | |
111 | $dependencies = (array) $dependencies; | |
112 | return $this->alterResource('requires', | |
113 | function ($values) use ($module, $dependencies) { | |
114 | if (!isset($values[$module])) { | |
c64f69d9 | 115 | $values[$module] = []; |
98e9fb33 TO |
116 | } |
117 | $values[$module] = array_unique(array_merge($values[$module], $dependencies)); | |
118 | return $values; | |
119 | }); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Declare a change to a resource. | |
124 | * | |
125 | * @param string $resourceType | |
126 | * @param callable $callback | |
127 | * @return ChangeSet | |
128 | */ | |
129 | public function alterResource($resourceType, $callback) { | |
c64f69d9 | 130 | $this->resFilters[] = [ |
98e9fb33 TO |
131 | 'resourceType' => $resourceType, |
132 | 'callback' => $callback, | |
c64f69d9 | 133 | ]; |
98e9fb33 TO |
134 | return $this; |
135 | } | |
136 | ||
137 | /** | |
138 | * Declare a change to HTML. | |
139 | * | |
140 | * @param string $file | |
141 | * A file name, wildcard, or regex. | |
142 | * Ex: '~/crmHello/intro.html' (filename) | |
143 | * Ex: '~/crmHello/*.html' (wildcard) | |
144 | * Ex: ';(Edit|List)Ctrl\.html$;' (regex) | |
145 | * @param callable $callback | |
146 | * Function which accepts up to two parameters: | |
147 | * - phpQueryObject $doc | |
148 | * - string $path | |
149 | * @return ChangeSet | |
150 | */ | |
151 | public function alterHtml($file, $callback) { | |
c64f69d9 | 152 | $this->htmlFilters[] = [ |
98e9fb33 TO |
153 | 'regex' => ($file{0} === ';') ? $file : $this->createRegex($file), |
154 | 'callback' => $callback, | |
c64f69d9 | 155 | ]; |
98e9fb33 TO |
156 | return $this; |
157 | } | |
158 | ||
159 | /** | |
160 | * Convert a string with a wildcard (*) to a regex. | |
161 | * | |
162 | * @param string $filterExpr | |
163 | * Ex: "/foo/*.bar" | |
164 | * @return string | |
165 | * Ex: ";^/foo/[^/]*\.bar$;" | |
166 | */ | |
167 | protected function createRegex($filterExpr) { | |
168 | $regex = preg_quote($filterExpr, ';'); | |
169 | $regex = str_replace('\\*', '[^/]*', $regex); | |
170 | $regex = ";^$regex$;"; | |
171 | return $regex; | |
172 | } | |
173 | ||
174 | /** | |
175 | * @return string | |
176 | */ | |
177 | public function getName() { | |
178 | return $this->name; | |
179 | } | |
180 | ||
181 | /** | |
182 | * @param string $name | |
183 | */ | |
184 | public function setName($name) { | |
185 | $this->name = $name; | |
186 | } | |
187 | ||
188 | } |