1<?php
2
3require_once dirname(__FILE__) . '/component_manager.php';
4require_once dirname(__FILE__) . '/ajax.php';
5
6// the dir holding ajax handlers under the plugin dir structure.
7// can be in its subdirs
8define(DOKU_AJAX_ROOT, '/ajax');
9
10/**
11 * The manager for AJAX handler components
12 * @author Junling Ma <junlingm@gmail.com>
13 */
14class Doku_AJAX_Manager extends Doku_Component_Manager {
15	private $handlers = array();
16
17    /**
18     * handles a new class that is loaded in
19     * @param string $class the name of the new class.
20     */
21	protected function handle($class) {
22		if (is_subclass_of($class, 'Doku_AJAX')) {
23			$handler = new $class;
24			$this->handlers[$handler->name()] = $handler;
25		}
26	}
27
28	/**
29	 * call the given ajax function
30	 * the ajax function function name must be plugin_name.function_name.
31	 * @param string $call the name fo the AJAX call;
32	 * @return bool whether the call has been make (regardless of being successful).
33	 */
34	public function call($call) {
35		$components = explode('.', $call);
36		if (count($components) <= 1) return FALSE;
37		$plugin = array_shift($components);
38		$call = implode('.', $components);
39		$path = DOKU_PLUGIN  . $plugin . DOKU_AJAX_ROOT;
40		$this->load($path, $call);
41		foreach($this->handlers as $handler)
42			if ($handler->name() == $call) {
43				$handler->handle();
44				// if we get to here, the hander should have exited.
45				// but just in case
46				return TRUE;
47			}
48		return FALSE;
49	}
50}