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');
58 if (\CRM_Core_Config
::singleton()->debug
&& !$coder->checkConsistentHtml($html)) {
59 throw new \
CRM_Core_Exception("Cannot process $path: inconsistent markup. Use check-angular.php to investigate.");
62 call_user_func($filter['callback'], $doc, $path);
68 $strings[$path] = $coder->encode($doc);
81 * Each item is an array with keys:
82 * - resourceType: string
83 * - callback: function
85 protected $resFilters = array();
89 * Each item is an array with keys:
91 * - callback: function
93 protected $htmlFilters = array();
97 * Symbolic name for this changeset.
98 * @return \Civi\Angular\ChangeSetInterface
100 public static function create($name) {
101 $changeSet = new ChangeSet();
102 $changeSet->name
= $name;
107 * Declare that $module requires additional dependencies.
109 * @param string $module
110 * @param string|array $dependencies
113 public function requires($module, $dependencies) {
114 $dependencies = (array) $dependencies;
115 return $this->alterResource('requires',
116 function ($values) use ($module, $dependencies) {
117 if (!isset($values[$module])) {
118 $values[$module] = array();
120 $values[$module] = array_unique(array_merge($values[$module], $dependencies));
126 * Declare a change to a resource.
128 * @param string $resourceType
129 * @param callable $callback
132 public function alterResource($resourceType, $callback) {
133 $this->resFilters
[] = array(
134 'resourceType' => $resourceType,
135 'callback' => $callback,
141 * Declare a change to HTML.
143 * @param string $file
144 * A file name, wildcard, or regex.
145 * Ex: '~/crmHello/intro.html' (filename)
146 * Ex: '~/crmHello/*.html' (wildcard)
147 * Ex: ';(Edit|List)Ctrl\.html$;' (regex)
148 * @param callable $callback
149 * Function which accepts up to two parameters:
150 * - phpQueryObject $doc
154 public function alterHtml($file, $callback) {
155 $this->htmlFilters
[] = array(
156 'regex' => ($file{0} === ';') ?
$file : $this->createRegex($file),
157 'callback' => $callback,
163 * Convert a string with a wildcard (*) to a regex.
165 * @param string $filterExpr
168 * Ex: ";^/foo/[^/]*\.bar$;"
170 protected function createRegex($filterExpr) {
171 $regex = preg_quote($filterExpr, ';');
172 $regex = str_replace('\\*', '[^/]*', $regex);
173 $regex = ";^$regex$;";
180 public function getName() {
185 * @param string $name
187 public function setName($name) {