/** * Parse URL, extract controller and action and execute them * * @uses $action * @uses $application_controller_file * @uses $application_helper_file * @uses $controller * @uses $controller_class * @uses $controller_file * @uses $controller_object * @uses determine_layout() * @uses execute_after_filters() * @uses $helpers * @uses $helper_file * @uses $helpers_base_path * @uses $keep_flash * @uses $loaded * @uses recognize_route() * @uses raise() * @uses ScaffoldController * @uses Session::unset_var() * @uses $view_file * @uses $views_path * @return boolean true */ function process_route() { # First try to load the routes and setup the paths to everything if (!$this->loaded) { if (!$this->recognize_route()) { $this->raise("Failed to load any defined routes", "Controller " . $this->controller . " not found", "404"); } } //error_log('process_route(): controller="'.$this->controller // .'" action="'.$this->action.'"'); # Include main application controller file if (file_exists($this->application_controller_file)) { include_once $this->application_controller_file; } # If controller is loaded then start processing if ($this->loaded) { include_once $this->controller_file; if (class_exists($this->controller_class, false)) { $class = $this->controller_class; $this->controller_object = new $class(); } if (is_object($this->controller_object)) { $this->controller_object->controller = $this->controller; $this->controller_object->action = $this->action; $this->controller_object->controller_path = "{$this->added_path}/{$this->controller}"; $this->controller_object->views_path = $this->views_path; $this->controller_object->layouts_path = $this->layouts_path; $this->controller_object->plugin = $this->plugin; Trax::$current_controller_path = "{$this->added_path}/{$this->controller}"; Trax::$current_controller_name = $this->controller; Trax::$current_action_name = $this->action; Trax::$current_controller_object =& $this->controller_object; # Which layout should we use? $this->controller_object->determine_layout(); # Check if there is any defined scaffolding to load if (isset($this->controller_object->scaffold)) { $scaffold = $this->controller_object->scaffold; if (file_exists(TRAX_LIB_ROOT . "/scaffold_controller.php")) { include_once TRAX_LIB_ROOT . "/scaffold_controller.php"; $this->controller_object = new ScaffoldController($scaffold); Trax::$current_controller_object =& $this->controller_object; $render_options['scaffold'] = true; if (!file_exists($this->controller_object->layout_file)) { # the generic scaffold layout $this->controller_object->layout_file = TRAX_LIB_ROOT . "/templates/scaffolds/layout.phtml"; } } } # Include main application helper file if (file_exists($this->application_helper_file)) { include_once $this->application_helper_file; } # Include helper file for this controller if (file_exists($this->helper_file)) { include_once $this->helper_file; } # Include any extra helper files defined in this controller if (count($this->controller_object->helpers) > 0) { foreach ($this->controller_object->helpers as $helper) { if (strstr($helper, "/")) { $file = substr(strrchr($helper, "/"), 1); $path = substr($helper, 0, strripos($helper, "/")); $helper_path_with_file = $this->helpers_base_path . "/" . $path . "/" . $file . "_helper.php"; } else { $helper_path_with_file = $this->helpers_base_path . "/" . $helper . "_helper.php"; } if (file_exists($helper_path_with_file)) { # Include the helper file include $helper_path_with_file; } } } # Suppress output ob_start(); #error_log('started capturing HTML'); # Call the controller method based on the URL if ($this->controller_object->execute_before_filters()) { $controller_layout = null; if (isset($this->controller_object->layout)) { $controller_layout = $this->controller_object->layout; } #Get PUBLIC methods from controller object $all_methods = get_class_methods($this->controller_object); # Get Inherited methods from active_controller $inherited_methods = array_merge(get_class_methods(__CLASS__), $this->controller_object->before_filters, $this->controller_object->after_filters); # Get non-inherited methods $action_methods = array_diff($all_methods, $inherited_methods); #error_log("available methods:".print_r($action_methods, true)); if (in_array($this->action, $action_methods)) { #error_log('method '.$this->action.' exists, calling it'); $action = $this->controller_object->called_action = $this->action; #error_log('calling action routine ' # . get_class($this->controller_object) # .'::'.$action.'()'); $this->controller_object->{$action}(); } elseif (file_exists($this->views_path . "/" . $this->action . "." . Trax::$views_extension)) { #error_log('views file "'.$this->action.'"'); $action = $this->controller_object->called_action = $this->action; } elseif (method_exists($this->controller_object, "index")) { #error_log('calling action routine ' # . get_class($this->controller_object) # .'::index()'); $action = $this->controller_object->called_action = "index"; $this->controller_object->index(); } else { //error_log('no action'); $methods_size = count($action_methods); if ($methods_size > 1) { $last_method = ($methods_size > 2 ? "," : '') . " and " . array_pop($action_methods); } $this->raise("No action responded to " . $this->action . ". Actions:" . implode(", ", $action_methods) . $last_method, "Unknown action", "404"); } if (isset($this->controller_object->layout)) { if ($controller_layout != $this->controller_object->layout) { # layout was set in the action need to redetermine the layout file to use. $this->controller_object->determine_layout(); } } #$this->controller_object->execute_after_filters(); $this->controller_object->action_called = true; # Find out if there was a redirect to some other page if (isset($this->controller_object->redirect_to) && $this->controller_object->redirect_to != '') { $this->redirect_to($this->controller_object->redirect_to); # execution will end here redirecting to new page } # If render_text was defined as a string render it if (isset($this->controller_object->render_text) && $this->controller_object->render_text != "") { $this->render_text($this->controller_object->render_text); # execution will end here rendering only the text no layout } # If defined string render_action use that instead if (isset($this->controller_object->render_action) && $this->controller_object->render_action != '') { $action = $this->controller_object->render_action; } # Render the action / view if (!$this->controller_object->render_action($action, isset($render_options) ? $render_options : null)) { $this->raise("No view file found {$action} ({$this->view_file}).", "Unknown view", "404"); } $this->controller_object->execute_after_filters(); # Grab all the html from the view to put into the layout $content_for_layout = ob_get_contents(); ob_end_clean(); //error_log("captured ".strlen($content_for_layout)." bytes\n"); if (isset($this->controller_object->render_layout) && $this->controller_object->render_layout !== false && $this->controller_object->layout_file) { $locals['content_for_layout'] = $content_for_layout; # render the layout #error_log("rendering layout: ".$this->controller_object->layout_file); if (!$this->controller_object->render_file($this->controller_object->layout_file, false, $locals)) { # No layout template so just echo out whatever is in $content_for_layout //echo "HERE"; echo $content_for_layout; } } else { # Can't find any layout so throw an exception # $this->raise("No layout file found.", "Unknown layout", "404"); # No layout template so just echo out whatever is in $content_for_layout //error_log("no layout found: ".$this->controller_object->layout_file); echo $content_for_layout; } } } else { $this->raise("Failed to instantiate controller object \"" . $this->controller . "\".", "ActionController Error", "500"); } } else { $this->raise("No controller found.", "Unknown controller", "404"); } // error_log('keep flash='.var_export($this->keep_flash,true)); if (!$this->keep_flash) { # Nuke the flash unset($_SESSION['flash']); Session::unset_var('flash'); } return true; }
/** * Implement the "generate scaffold" command * * @param string $model_name * @param string $controller_name * @param string $views * @uses generate_controller() * @uses generate_model() * @uses Inflector::classify() * @uses Inflector::humanize() * @uses Inflector::pluralize() * @uses Inflector::singularize() * @uses Inflector::underscore() * @uses $layout_filename Set as output from * generate_controller(). * Not changed afterward. * @uses fix_php_brackets() */ function generate_scaffold($model_name, $controller_name, $views = "") { //echo 'generate_scaffold("'.$model_name.'", "' // .$controller_name.'", "'.$views.'")'."\n"; if (!($model_exists = $this->generate_model($model_name))) { echo "Error - Can't create Model: {$model_name}.\n"; return; } Trax::$current_controller_object =& $this; $model_class_name = Inflector::classify($model_name); $singular_model_name = Inflector::singularize($model_name); $plural_model_name = Inflector::pluralize($model_name); $human_model_name = Inflector::humanize($model_name); try { $this->{$singular_model_name} = new $model_class_name(); } catch (ActiveRecordError $e) { echo "Can't create model.\n"; echo $e->getMessage() . "\n"; echo "for database '" . ActiveRecord::$database_settings[TRAX_ENV]['database'] . "' on host '" . ActiveRecord::$database_settings[TRAX_ENV]['hostspec'] . "' as user '" . ActiveRecord::$database_settings[TRAX_ENV]['username'] . "'\nDid you configure file " . Trax::$config_path . "/database.ini correctly?\n"; die; } if (empty($controller_name)) { $controller_name = Inflector::underscore($model_name); } else { $controller_name = Inflector::underscore($controller_name); } Trax::$current_controller_name = $controller_name; $controller_file = "{$this->controller_path}/" . $controller_name . "_controller.php"; Trax::$current_controller_path = $controller_file; $non_scaffolded_actions = array(); $illegal_views = array("index", "add", "edit", "show"); if (is_array($views)) { foreach ($views as $view) { if (!in_array($view, $illegal_views)) { $non_scaffolded_actions[] = $view; } } } $this->generate_controller($controller_name, $non_scaffolded_actions, true); if (stristr($controller_name, "/")) { $controller_class_name = Inflector::classify(substr($controller_name, strrpos($controller_name, "/") + 1)); $human_controller_name = Inflector::humanize(substr($controller_name, strrpos($controller_name, "/") + 1)); } else { $controller_class_name = Inflector::classify($controller_name); $human_controller_name = Inflector::humanize($controller_name); } # Generate the controller ob_start(); include "{$this->scaffold_template_path}/controller.php"; $controller_contents = $this->fix_php_brackets(ob_get_contents()); ob_end_clean(); if (!file_exists($controller_file)) { if (!file_put_contents($controller_file, $controller_contents)) { echo "error creating controller class file: {$controller_file}\n"; } else { echo "create {$controller_file}\n"; } } else { echo "exists {$controller_file}\n"; } # Generate the index.phtml view $view_file = "{$this->view_path}/index." . Trax::$views_extension; ob_start(); include "{$this->scaffold_template_path}/view_index.phtml"; $index_contents = $this->fix_php_brackets(ob_get_contents()); ob_end_clean(); if (!file_exists($view_file)) { if (!file_put_contents($view_file, $index_contents)) { echo "error creating view file: {$view_file}\n"; } else { echo "create {$view_file}\n"; } } else { echo "exists {$view_file}\n"; } # Generate the add.phtml view $view_file = "{$this->view_path}/add." . Trax::$views_extension; ob_start(); include "{$this->scaffold_template_path}/view_add.phtml"; $add_contents = $this->fix_php_brackets(ob_get_contents()); ob_end_clean(); if (!file_exists($view_file)) { if (!file_put_contents($view_file, $add_contents)) { echo "error creating view file: {$view_file}\n"; } else { echo "create {$view_file}\n"; } } else { echo "exists {$view_file}\n"; } # Generate the edit.phtml view $view_file = "{$this->view_path}/edit." . Trax::$views_extension; ob_start(); include "{$this->scaffold_template_path}/view_edit.phtml"; $edit_contents = $this->fix_php_brackets(ob_get_contents()); ob_end_clean(); if (!file_exists($view_file)) { if (!file_put_contents($view_file, $edit_contents)) { echo "error creating view file: {$view_file}\n"; } else { echo "create {$view_file}\n"; } } else { echo "exists {$view_file}\n"; } # Generate the show.phtml view $view_file = "{$this->view_path}/show." . Trax::$views_extension; ob_start(); include "{$this->scaffold_template_path}/view_show.phtml"; $show_contents = $this->fix_php_brackets(ob_get_contents()); ob_end_clean(); if (!file_exists($view_file)) { if (!file_put_contents($view_file, $show_contents)) { echo "error creating view file: {$view_file}\n"; } else { echo "create {$view_file}\n"; } } else { echo "exists {$view_file}\n"; } # Generate the partial containing the form elments from the database $view_file = "{$this->view_path}/_form." . Trax::$views_extension; ob_start(); require "{$this->scaffold_template_path}/form_scaffolding.phtml"; $_form_contents = $this->fix_php_brackets(ob_get_contents()); ob_end_clean(); if (!file_exists($view_file)) { if (!file_put_contents($view_file, $_form_contents)) { echo "error creating view file: {$view_file}\n"; } else { echo "create {$view_file}\n"; } } else { echo "exists {$view_file}\n"; } # Generate the layout for the scaffolding $layout_file = $this->layouts_path . "/" . $this->layout_filename . "." . Trax::$views_extension; if (!file_exists($this->layouts_path)) { mkdir($this->layouts_path); } ob_start(); include "{$this->scaffold_template_path}/layout.phtml"; $layout_contents = $this->fix_php_brackets(ob_get_contents()); ob_end_clean(); if (!file_exists($layout_file)) { if (!file_put_contents($layout_file, $layout_contents)) { echo "error creating layout file: {$layout_file}\n"; } else { echo "create {$layout_file}\n"; } } else { echo "exists {$layout_file}\n"; } }
/** * Sets up the fixture, for example, open a network connection. * This method is called before a test is executed. * * @access protected */ protected function setUp() { Trax::$current_controller_name = 'foo_controller'; Trax::$current_controller_path = '/foo/bar/mumble'; Trax::$current_controller_object = 'nonobject'; }