2 namespace Civi\Angular
;
4 class ChangeSet
implements ChangeSetInterface
{
7 * Update a listing of resources.
9 * @param array $changeSets
11 * @param string $resourceType
12 * Ex: 'requires', 'settings'
13 * @param array $resources
14 * The list of resources.
17 public static function applyResourceFilters($changeSets, $resourceType, $resources) {
18 if ($resourceType === 'partials') {
19 return self
::applyHtmlFilters($changeSets, $resources);
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);
33 * Update a set of HTML snippets.
35 * @param array $changeSets
37 * @param array $strings
38 * Array(string $path => string $html).
40 * Updated list of $strings.
41 * @throws \CRM_Core_Exception
43 private static function applyHtmlFilters($changeSets, $strings) {
46 foreach ($strings as $path => $html) {
47 /** @var \phpQueryObject $doc */
50 // Most docs don't need phpQueryObject. Initialize phpQuery on first match.
52 foreach ($changeSets as $changeSet) {
53 /** @var ChangeSet $changeSet */
54 foreach ($changeSet->htmlFilters
as $filter) {
55 if (preg_match($filter['regex'], $path)) {
57 $doc = \phpQuery
::newDocument($html, 'text/html');
59 call_user_func($filter['callback'], $doc, $path);
65 $strings[$path] = $coder->encode($doc);
78 * Each item is an array with keys:
79 * - resourceType: string
80 * - callback: function
82 protected $resFilters = [];
86 * Each item is an array with keys:
88 * - callback: function
90 protected $htmlFilters = [];
94 * Symbolic name for this changeset.
95 * @return \Civi\Angular\ChangeSetInterface
97 public static function create($name) {
98 $changeSet = new ChangeSet();
99 $changeSet->name
= $name;
104 * Declare that $module requires additional dependencies.
106 * @param string $module
107 * @param string|array $dependencies
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])) {
115 $values[$module] = [];
117 $values[$module] = array_unique(array_merge($values[$module], $dependencies));
123 * Declare a change to a resource.
125 * @param string $resourceType
126 * @param callable $callback
129 public function alterResource($resourceType, $callback) {
130 $this->resFilters
[] = [
131 'resourceType' => $resourceType,
132 'callback' => $callback,
138 * Declare a change to HTML.
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
151 public function alterHtml($file, $callback) {
152 $this->htmlFilters
[] = [
153 'regex' => ($file{0} === ';') ?
$file : $this->createRegex($file),
154 'callback' => $callback,
160 * Convert a string with a wildcard (*) to a regex.
162 * @param string $filterExpr
165 * Ex: ";^/foo/[^/]*\.bar$;"
167 protected function createRegex($filterExpr) {
168 $regex = preg_quote($filterExpr, ';');
169 $regex = str_replace('\\*', '[^/]*', $regex);
170 $regex = ";^$regex$;";
177 public function getName() {
182 * @param string $name
184 public function setName($name) {