<?php


class ActionService {



	/**
	 * Dispatches action to controller
	 */
	public static function execAction($action, $args = array()) {
		if ($action instanceof Action) {
			$args = $action->getArgs();
			$action = $action->getActionString();
		}
		if (false !==  strpos($action, '.')) {
			throw new Exception("action string \"$action\"' cannot contain dot");
		}
		$a = explode('/', $action);
		if ($action[0] != '/') {
			array_unshift($a, 'ctrl');
		}
		if (count($a) < 3 or strstr($action, ' ')) {
			throw new Exception("malformed action string \"$action\"");
		}
		$method = array_pop($a);
		$class = array_pop($a);
		$path = implode('/', $a);
		$fileName = "$path/$class.class.php";
		if (!file_exists('./' . $fileName)) {
			throw new Exception("cannot find ctrl file $fileName for action $action");
		}
		require_once './' . $fileName;
		if (!class_exists($class)) {
			throw new Exception("class $class not found in file $fileName for action $action");
		}
		$c = new $class();
		return call_user_func_array(array($c, $method), $args);
	}



} // class ActionService






?>