Skip to content

Commit

Permalink
Merge branch 'wip-MDL-31789-MOODLE_22_STABLE' of git://github.com/mar…
Browse files Browse the repository at this point in the history
…inaglancy/moodle into MOODLE_22_STABLE
  • Loading branch information
Aparup Banerjee committed Apr 3, 2012
2 parents 97c0418 + 67ace38 commit 3ba909d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
21 changes: 20 additions & 1 deletion lib/accesslib.php
Expand Up @@ -4487,7 +4487,7 @@ function role_change_permission($roleid, $context, $capname, $permission) {
* @property-read string $path path to context, starts with system context * @property-read string $path path to context, starts with system context
* @property-read dept $depth * @property-read dept $depth
*/ */
abstract class context extends stdClass { abstract class context extends stdClass implements IteratorAggregate {


/* /*
* Google confirms that no other important framework is using "context" class, * Google confirms that no other important framework is using "context" class,
Expand Down Expand Up @@ -4695,6 +4695,25 @@ public function __unset($name) {
debugging('Can not unset context instance properties!'); debugging('Can not unset context instance properties!');
} }


// ====== implementing method from interface IteratorAggregate ======

/**
* Create an iterator because magic vars can't be seen by 'foreach'.
*
* Now we can convert context object to array using convert_to_array(),
* and feed it properly to json_encode().
*/
public function getIterator() {
$ret = array(
'id' => $this->id,
'contextlevel' => $this->contextlevel,
'instanceid' => $this->instanceid,
'path' => $this->path,
'depth' => $this->depth
);
return new ArrayIterator($ret);
}

// ====== general context methods ====== // ====== general context methods ======


/** /**
Expand Down
34 changes: 34 additions & 0 deletions lib/moodlelib.php
Expand Up @@ -10081,6 +10081,40 @@ function object_property_exists( $obj, $property ) {
return array_key_exists( $property, $properties ); return array_key_exists( $property, $properties );
} }


/**
* Converts an object into an associative array
*
* This function converts an object into an associative array by iterating
* over its public properties. Because this function uses the foreach
* construct, Iterators are respected. It works recursively on arrays of objects.
* Arrays and simple values are returned as is.
*
* If class has magic properties, it can implement IteratorAggregate
* and return all available properties in getIterator()
*
* @param mixed $var
* @return array
*/
function convert_to_array($var) {
$result = array();
$references = array();

// loop over elements/properties
foreach ($var as $key => $value) {
// recursively convert objects
if (is_object($value) || is_array($value)) {
// but prevent cycles
if (!in_array($value, $references)) {
$result[$key] = convert_to_array($value);
$references[] = $value;
}
} else {
// simple values are untouched
$result[$key] = $value;
}
}
return $result;
}


/** /**
* Detect a custom script replacement in the data directory that will * Detect a custom script replacement in the data directory that will
Expand Down
6 changes: 3 additions & 3 deletions lib/outputcomponents.php
Expand Up @@ -1589,7 +1589,7 @@ class js_writer {
*/ */
public static function function_call($function, array $arguments = null, $delay=0) { public static function function_call($function, array $arguments = null, $delay=0) {
if ($arguments) { if ($arguments) {
$arguments = array_map('json_encode', $arguments); $arguments = array_map('json_encode', convert_to_array($arguments));
$arguments = implode(', ', $arguments); $arguments = implode(', ', $arguments);
} else { } else {
$arguments = ''; $arguments = '';
Expand All @@ -1611,7 +1611,7 @@ public static function function_call($function, array $arguments = null, $delay=
*/ */
public static function function_call_with_Y($function, array $extraarguments = null) { public static function function_call_with_Y($function, array $extraarguments = null) {
if ($extraarguments) { if ($extraarguments) {
$extraarguments = array_map('json_encode', $extraarguments); $extraarguments = array_map('json_encode', convert_to_array($extraarguments));
$arguments = 'Y, ' . implode(', ', $extraarguments); $arguments = 'Y, ' . implode(', ', $extraarguments);
} else { } else {
$arguments = 'Y'; $arguments = 'Y';
Expand All @@ -1630,7 +1630,7 @@ public static function function_call_with_Y($function, array $extraarguments = n
*/ */
public static function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) { public static function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) {
if (is_array($arguments)) { if (is_array($arguments)) {
$arguments = array_map('json_encode', $arguments); $arguments = array_map('json_encode', convert_to_array($arguments));
$arguments = implode(', ', $arguments); $arguments = implode(', ', $arguments);
} }


Expand Down
2 changes: 1 addition & 1 deletion lib/outputrequirementslib.php
Expand Up @@ -715,7 +715,7 @@ public function yui_module($modules, $function, array $arguments = null, $galler
// Set Y's config.gallery to the version // Set Y's config.gallery to the version
$jscode = 'Y.config.gallery='.json_encode($galleryversion).';'; $jscode = 'Y.config.gallery='.json_encode($galleryversion).';';
} }
$jscode .= 'Y.use('.join(',', array_map('json_encode', $modules)).',function() {'.js_writer::function_call($function, $arguments).'});'; $jscode .= 'Y.use('.join(',', array_map('json_encode', convert_to_array($modules))).',function() {'.js_writer::function_call($function, $arguments).'});';
if ($ondomready) { if ($ondomready) {
$jscode = "Y.on('domready', function() { $jscode });"; $jscode = "Y.on('domready', function() { $jscode });";
} }
Expand Down

0 comments on commit 3ba909d

Please sign in to comment.