Merge branch 'wip-MDL-32018-m21' of git://github.com/samhemelryk/moodle into MOODLE_2...
[moodle.git] / webservice / amf / introspector.php
blob6e8bef782dd22df40604e8b5e42561103351b19f
1 <?php
2 /**
3 * Moodle - Modular Object-Oriented Dynamic Learning Environment
4 * http://moodle.org
5 * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @package moodle
21 * @author Penny Leach <penny@liip.ch>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
23 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
25 * Introspection for amf - figures out where all the services are and
26 * returns a list of their available methods.
27 * Requires $CFG->amf_introspection = true for security.
31 /**
32 * Provides a function to get details of methods available on another class.
33 * @author HP
36 class MethodDescriptor {
38 private $methods;
39 private $classes;
41 static public $classnametointrospect;
44 public function __construct() {
45 $this->setup();
48 private function setup() {
49 global $CFG;
50 if (!empty($this->nothing)) {
51 return; // we've already tried, no classes.
53 if (!empty($this->classes)) { // we've already done it successfully.
54 return;
56 /*if (empty($CFG->amf_introspection)) {
57 throw new Exception(get_string('amfintrospectiondisabled', 'local'));
58 }*/
60 //just one class here, possibility for expansion in future
61 $classes = array(MethodDescriptor::$classnametointrospect);
63 $hugestructure = array();
65 foreach ($classes as $c) {
66 $r = new ReflectionClass($c);
68 if (!$methods = $r->getMethods()) {
69 continue;
71 $this->classes[] = $c;
72 $hugestructure[$c] = array('docs' => $r->getDocComment(), 'methods' => array());
73 foreach ($methods as $method) {
74 if (!$method->isPublic()) {
75 continue;
77 $params = array();
78 foreach ($method->getParameters() as $param) {
79 $params[] = array('name' => $param->getName(), 'required' => !$param->isOptional());
81 $hugestructure[$c]['methods'][$method->getName()] = array(
82 'docs' => $method->getDocComment(),
83 'params' => $params,
87 $this->methods = $hugestructure;
88 if (empty($this->classes)) {
89 $this->nothing = true;
93 public function getMethods() {
94 $this->setup();
95 return $this->methods;
98 public function getClasses() {
99 $this->setup();
100 return $this->classes;
103 public function isConnected() {
104 return true;