2 /** @package verysimple::Phreeze */
5 * import supporting libraries
7 require_once ("verysimple/HTTP/RequestUtil.php");
8 require_once ("verysimple/Util/ExceptionThrower.php");
11 * Dispatcher direct a web request to the correct controller & method
13 * @package verysimple::Phreeze
14 * @author VerySimple Inc.
15 * @copyright 1997-2007 VerySimple, Inc.
16 * @license http://www.gnu.org/licenses/lgpl.html LGPL
21 * Set to true and Phreeze will not try to handle deprecated function warnings
23 * @var boolean default = true
25 static $IGNORE_DEPRECATED = true;
28 * FAST_LOOKUP mode instructs the dispatcher to assume that the controller and method
29 * supplied by the router are valid and not do any checking for the existance of
30 * the controller file or the method before trying to call it
32 * @var boolean use fast lookup mode if true
34 static $FAST_LOOKUP = false;
37 * This is a case-insensitive version of file_exists
39 * @param string $fileName
41 static function ControllerFileExists($fileName) {
42 if (file_exists ( $fileName ))
45 $directoryName = dirname ( $fileName );
46 $fileArray = glob ( $directoryName . '/*', GLOB_NOSORT
);
47 $fileNameLowerCase = strtolower ( $fileName );
49 // TODO: if not an array then this path isn't readable, should we ignore or crash...?
50 // if (!is_array($fileArray)) throw new Exception('Unreadable include path "'.$directoryName.'" for controller "' . $fileName . '"');
51 if (! is_array ( $fileArray ))
54 foreach ( $fileArray as $file ) {
55 if (strtolower ( $file ) == $fileNameLowerCase)
63 * Processes user input and executes the specified controller method, ensuring
64 * that the controller dependencies are all injected properly
66 * @param Phreezer $phreezer
67 * Object persistance engine
68 * @param IRenderEngine $renderEngine
71 * string (optional) $action the user requested action (if not provided will use router->GetRoute())
73 * Context (optional) a context object for persisting state
75 * IRouter (optional) router object for reading/writing URLs (if not provided, GenericRouter will be used)
77 static function Dispatch($phreezer, $renderEngine, $action = '', $context = null, $router = null) {
78 if ($router == null) {
79 require_once ('GenericRouter.php');
80 $router = new GenericRouter ();
83 // get the route and normalize the controller name
84 list ( $controller_param, $method_param ) = $router->GetRoute ( $action );
85 $controller_class = $controller_param . "Controller";
87 if (self
::$FAST_LOOKUP) {
89 if (! class_exists ( $controller_class )) {
90 $controller_file = "Controller/$controller_class.php";
91 include_once $controller_file;
94 $controller = new $controller_class ( $phreezer, $renderEngine, $context, $router );
95 $controller->$method_param ();
100 // if the controller was in a sub-directory, get rid of the directory path
101 $slashPos = strpos ( $controller_class, '/' );
102 while ( $slashPos !== false ) {
103 $controller_class = substr ( $controller_class, $slashPos +
1 );
104 $slashPos = strpos ( $controller_class, '/' );
107 if (! class_exists ( $controller_class )) {
108 // attempt to locate the controller file
109 $controller_file = "Controller/" . $controller_param . "Controller.php";
110 $controller_filepath = null;
112 // search for the controller file in the default locations, then the include path
113 $paths = array_merge ( array (
116 ), explode ( PATH_SEPARATOR
, get_include_path () ) );
119 foreach ( $paths as $path ) {
120 $controller_filepath = self
::ControllerFileExists ( $path . "/" . $controller_file );
121 if ($controller_filepath) {
128 throw new Exception ( "File ~/libs/" . $controller_file . " was not found in include path" );
130 // convert any php errors into an exception
131 if (self
::$IGNORE_DEPRECATED) {
132 ExceptionThrower
::Start ();
134 ExceptionThrower
::Start ( E_ALL
);
135 ExceptionThrower
::$IGNORE_DEPRECATED = false;
138 // we should be fairly certain the file exists at this point
139 include_once ($controller_filepath);
141 // we found the file but the expected class doesn't appear to be defined
142 if (! class_exists ( $controller_class )) {
143 throw new Exception ( "Controller file was found, but class '" . $controller_class . "' is not defined" );
147 // create an instance of the controller class
148 $controller = new $controller_class ( $phreezer, $renderEngine, $context, $router );
150 // we have a valid instance, just verify there is a matching method
151 if (! is_callable ( array (
155 throw new Exception ( "'" . $controller_class . "." . $method_param . "' is not a valid action" );
158 // do not call the requested method/route if the controller request has been cancelled
159 if (! $controller->IsTerminated ()) {
160 // file, class and method all are ok, go ahead and call it
161 call_user_func ( array (
167 // reset error handling back to whatever it was
168 // restore_exception_handler();
169 ExceptionThrower
::Stop ();
175 * Fired by the PHP error handler function.
176 * Calling this function will
177 * always throw an exception unless error_reporting == 0. If the
178 * PHP command is called with @ preceeding it, then it will be ignored
181 * @deprecated use ExceptionThrower::HandleError instead
182 * @param string $code
183 * @param string $string
184 * @param string $file
185 * @param string $line
186 * @param string $context
188 static function HandleException($code, $string, $file, $line, $context) {
189 ExceptionThrower
::HandleError ( $code, $string, $file, $line, $context );