MDL-31789: Allowing context object to be passed to JS using json_encode()
Classes with magic properties such as context do not pass through json_encode(). The solution is to implement iterator in such classes and convert what we feed to json_encode to array using 'foreach' before we pass it to json_encode(): - class context implements IteratorAggregate - added function convert_to_array() that converts anything to array - before calling json_encode we convert the argument to array
This commit is contained in:
@@ -1589,7 +1589,7 @@ class js_writer {
|
||||
*/
|
||||
public static function function_call($function, array $arguments = null, $delay=0) {
|
||||
if ($arguments) {
|
||||
$arguments = array_map('json_encode', $arguments);
|
||||
$arguments = array_map('json_encode', convert_to_array($arguments));
|
||||
$arguments = implode(', ', $arguments);
|
||||
} else {
|
||||
$arguments = '';
|
||||
@@ -1611,7 +1611,7 @@ class js_writer {
|
||||
*/
|
||||
public static function function_call_with_Y($function, array $extraarguments = null) {
|
||||
if ($extraarguments) {
|
||||
$extraarguments = array_map('json_encode', $extraarguments);
|
||||
$extraarguments = array_map('json_encode', convert_to_array($extraarguments));
|
||||
$arguments = 'Y, ' . implode(', ', $extraarguments);
|
||||
} else {
|
||||
$arguments = 'Y';
|
||||
@@ -1630,7 +1630,7 @@ class js_writer {
|
||||
*/
|
||||
public static function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) {
|
||||
if (is_array($arguments)) {
|
||||
$arguments = array_map('json_encode', $arguments);
|
||||
$arguments = array_map('json_encode', convert_to_array($arguments));
|
||||
$arguments = implode(', ', $arguments);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user