Merges a context with the shared variables, same as mergeGlobals()

@param array $context An array representing the context

@return array The context merged with the globals
335697
public function mergeShared(array $context)
    {
        // we don't use array_merge as the context being generally
        // bigger than globals, this code is faster.
        foreach ($this->app['view']->getShared() as $key => $value) {
            if (!array_key_exists($key, $context)) {
                $context[$key] = $value;
            }
        }

        return $context;
    }
371286
public function apply( $context ) {
		if ( ! $context instanceof RContext ) {
			$context = new Context( $context );
		}

		return $this->evaluate( $context );
	}
1163474
public static function merge($array1, $array2)
	{
		$result = [];
		for ($i = 0; $i < func_num_args(); $i++) {
			$result = parent::merge($result, static::asArray(func_get_arg($i)));
		}

		return $result;
	}
881778
public static function merge(&$context, $tmp) {
        $context['error'] = $tmp['error'];
        $context['helpers'] = $tmp['helpers'];
        $context['partials'] = $tmp['partials'];
        $context['partialCode'] = $tmp['partialCode'];
        $context['partialStack'] = $tmp['partialStack'];
        $context['usedCount'] = $tmp['usedCount'];
        $context['usedFeature'] = $tmp['usedFeature'];
        $context['usedPartial'] = $tmp['usedPartial'];
    }
620538
private function merge(array $merged, array $array)
    {
        foreach ($array as $key => $value) {
            if (is_array($value) && isset ($merged[$key]) && is_array($merged[$key])) {
                $merged[$key] = $this->merge($merged[$key], $value);
            } else {
                $merged[$key] = $value;
            }
        }

        return $merged;
    }
1127258
public static function merge($arrays)
    {
        $merged = [];

        foreach ($arrays as $array)
        {
            $merged = array_merge($merged, static::flatten($array));
        }

        return static::expand($merged);
    }
424927
public function saveContext($context) {
        global $DB;
        $now = time();
        $context->updated = $now;
        $settingsvalue = serialize($context->getSettings());
        $id = $context->getRecordId();
        $consumerpk = $context->getConsumer()->getRecordId();

        $isinsert = empty($id);
        if ($isinsert) {
            $context->created = $now;
            $params = [
                'consumerid' => $consumerpk,
                'lticontextkey' => $context->ltiContextId,
                'type' => $context->type,
                'settings' => $settingsvalue,
                'created' => $context->created,
                'updated' => $context->updated,
            ];
            $id = $DB->insert_record($this->contexttable, (object) $params);
            if ($id) {
                $context->setRecordId($id);
                return true;
            }
        } else {
            $data = (object) [
                'id' => $id,
                'contextid' => $consumerpk,
                'lticontextkey' => $context->ltiContextId,
                'type' => $context->type,
                'settings' => $settingsvalue,
                'updated' => $context->updated,
            ];
            return $DB->update_record($this->contexttable, $data);
        }

        return false;
    }
120463
private static function mergeConfigs(array $global, array $user)
	{
		foreach ($user as $key => $value)
		{
			if (!isset($global[$key])
				|| (gettype($value) !== gettype($global[$key]))
				|| !is_array($value))
			{
				$global[$key] = $value;
			}
			else if (self::isList($value) && self::isList($global[$key]))
			{
				$global[$key] = $value;
			}
			else
			{
				$global[$key] = self::mergeConfigs($global[$key], $value);
			}
		}
		
		return $global;
	}
1027282
public function setContext($context)
    {
        if ($context instanceof Context) {
            $context = [$context];
        }

        if ($context instanceof \ArrayObject || is_array($context) || $context instanceof \Iterator) {
            foreach ($context as $key => $value) {
                if (!$value instanceof Context) {
                    if (is_int($key) && is_array($value) && array_key_exists('key', $value) && array_key_exists('value', $value)) {
                        $contextData = ['key' => $value['key'], 'value' => $value['value']];
                        if (isset($value['id'])) {
                            $contextData['id'] = $value['id'];
                        }
                    } else {
                        $contextData = ['key' => $key, 'value' => $value];
                    }
                    $value = new Context($contextData);
                }

                $value->setNotification($this);
                $this->contexts->add($value);
            }
        }

        return $this;
    }
544272
protected function buildTopLevelArrayNestedForContext(array $context)
    {
        // Context becomes the main level, so anything in context (with some caveats)
        // is moved up to the top level -- by making %context% the main 'message'
        // output in the log line.

        // Rename to prefix context variables with 'reserved' names
        $nested = [];

        foreach ($this->reservedTopLevelKeys as $key) {

            if (array_key_exists($key, $context)) {
                $nested[ $key ] = $context[ $key ];
                unset ($context[ $key ]);
            }
        }

        // The 'context' key is a special case: its value should be merged
        // into the nested context at that level.
        // (So context.context.key becomes context.key)

        if (array_key_exists('context', $context)) {

            if ( ! is_array($context['context'])) {
                $context['context'] = (array) $context['context'];
            }

            $context['context'] = array_merge($context['context'], $nested);

        } else {
            // if it does not exist yet, just make a nested context
            $context['context'] = $nested;
        }

        return $context;
    }
1329121
protected function mergeShared(array $params)
    {
        $proxy = $this->getProxy();

        return array_merge($this->globals, $params, ['view' => $proxy, 'func' => [$this, 'func']]);
    }
998493
protected function mergeGenericLocales(array $inputLocales)
    {
        $locales = [];
        foreach ($inputLocales as $locale) {
            $locales[] = $locale;
            if (strlen($locale) === 5) {
                $locale = substr($locale, 0, 2);
                $locales[] = $locale;
            }
        }

        $locales = array_reverse(array_unique(array_reverse($locales)));

        return $locales;
    }
224045
public function render($context)
    {
        $values = array();

        foreach ($this->vars as $var) {
            $actualRow = $this->getCell()->getActualRow();
            if (is_object($actualRow)) {
                $actualRow = $actualRow->getArrayCopy();
            }
            $values[] = $actualRow[$var];
        }

        $value = vsprintf($this->template, $values);

        if ($this->place == self::RESET_CONTEXT) {
            return $value;
        } else {
            return ($this->place == self::PRE_CONTEXT) ? $value . $context :  $context . $value;
        }
    }
43965
protected function getData(array $context)
    {
        $data = [];
        switch ($context['action']) {
            case 'UPDATE':
                $diff = $this->diff($context['old'], $context['new']);
                foreach ($diff as $item) {
                    list($k, $old, $new) = $item;
                    if (isset($context['new'][$k])) {
                        $data[$k] = [$old, $new];
                    }
                }
                break;
            case 'INSERT':
                foreach ($context['new'] as $k => $val) {
                    $data[$k] = [null, $val];
                }
                break;
            case 'DELETE':
                foreach ($context['old'] as $k => $val) {
                    $data[$k] = [$val, null];
                }
                break;
        }

        return $data;
    }
936549
protected function assimilateContext(array $context = [])
    {
        $this->_context_ = $context;
        foreach ($context as $key => $value) {
            $this->$key = $value;
        }
    }
306963
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = NULL)
    {
        return array_map(function($m) use ($context, $types) {
            return $m->jsonLDSerialize($context, $types ?? true); 
        }, $this->members);
    }
662223
public function setContext(array $context)
    {
        $this->clearContext();
        foreach ($context as $key => $value) {
            $this->setContextValue($key, $value);
        }
    }