<?php
function mergeAttachments($a, $b) {
// If both #attached arrays contain drupalSettings, then merge them
// correctly; adding the same settings multiple times needs to behave
// idempotently.
if (!empty($a['drupalSettings']) && !empty($b['drupalSettings'])) {
$drupalSettings = mergeDeepArray(array($a['drupalSettings'], $b['drupalSettings']), TRUE);
// No need for re-merging them.
unset($a['drupalSettings']);
unset($b['drupalSettings']);
}
// Apply the normal merge.
$a = mergeDeepArray(array($a, $b));
if (isset($drupalSettings)) {
// Save the custom merge for the drupalSettings.
$a['drupalSettings'] = $drupalSettings;
}
return $a;
}
function mergeAttachmentsOld($a, $b) {
if (!empty($a['drupalSettings']) && !empty($b['drupalSettings'])) {
$a['drupalSettings'] = mergeDeepArray([$a['drupalSettings'], $b['drupalSettings']], TRUE);
unset($b['drupalSettings']);
}
return mergeDeep($a, $b);
}
function mergeDeepArray($arrays, $preserve_integer_keys = FALSE) {
$result = array();
foreach ($arrays as $array) {
foreach ($array as $key => $value) {
// Renumber integer keys as array_merge_recursive() does unless
// $preserve_integer_keys is set to TRUE. Note that PHP automatically
// converts array keys that are integer strings (e.g., '1') to integers.
if (is_integer($key) && !$preserve_integer_keys) {
$result[] = $value;
}
// Recurse when both values are arrays.
elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
$result[$key] = mergeDeepArray(array($result[$key], $value), $preserve_integer_keys);
}
// Otherwise, use the latter value, overriding any previous value.
else {
$result[$key] = $value;
}
}
}
return $result;
}
function mergeDeep() {
return mergeDeepArray(func_get_args());
}
for($i = 0; $i <= 100000; $i++) {
$a['#attached'] = array(
'library' => array(
'core/drupal',
'core/drupalSettings',
),
);
$b['#attached'] = array(
'library' => array(
'core/jquery',
),
);
mergeAttachments($a['#attached'], $b['#attached']);
}
- Output for git.master, git.master_jit, rfc.property-hooks
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
48.44 ms | 401 KiB | 8 Q