Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function dispatch()
- {
- try
- {
- // Instantiate the requested controller class
- $class = ( $this->getController() != null ) ? ucwords( $this->getController() ) . 'Controller' : BaseController::ERROR_CONTROLLER;
- /**
- * Ensure this is a class that's contained in the Manifest get_class(..,true);
- * If it doesn't exist in the manifest, it will not be executed.
- */
- $ref = new ReflectionClass( ( class_exists($class, true) ) ? $class : BaseController::ERROR_CONTROLLER );
- /**
- * Ensure that the class follows conventions:
- * 1. Controller class must implement IController
- * 2. Controller class must implement a Controller::index() method -- this is implied since
- * the first condition won't be satisfied unless the IController interface is implemented.
- */
- if( $ref->implementsInterface('IController') )
- {
- // Instantiate a new Controller
- $controller = $ref->newInstance( $this->registry );
- // Check to see if the method exists
- if( !$ref->hasMethod( $this->getAction() ) ) trigger_error("Method, "{$this->getAction()}" does not exist.", E_USER_WARNING);
- // Get a ReflectionMethod object
- $method = $ref->getMethod( ( $this->getAction() != null && $ref->hasMethod( $this->getAction() ) ) ? $this->getAction() : BaseController::DEFAULT_METHOD );
- // Track the request status
- $this->request->requestStatus( ( $ref->name == BaseController::ERROR_CONTROLLER ) ? HttpStatus::HTTP_NOT_FOUND : HttpStatus::HTTP_OK );
- // Invoke the Controller::method()
- $method->invoke( $controller );
- }
- else
- {
- trigger_error( 'Interface IContoller must be implemented.', E_USER_ERROR );
- }
- }
- catch( Exception $e )
- {
- header( HttpStatus::getHttpHeader( HttpStatus::HTTP_NOT_IMPLEMENTED ) );
- throw new Exception(__METHOD__ . ' threw an exception: ' . $e->getMessage());
- }
- }
- /**
- * This methods parses a given URI into it's various segments, e.g. controller|action|params
- *
- * @param array $pattern_collection A list of regex patterns to match
- */
- private function parseURI( array $pattern_collection )
- {
- $request_uri = $_SERVER['REQUEST_URI'];
- // Find the first match (not necessarily the best match)
- foreach( $pattern_collection as $route => $patterns )
- {
- foreach( $patterns as $pattern )
- {
- $match = preg_match( $pattern, $request_uri, $uri_fragments );
- if( $match && $match !== false)
- {
- break 2;
- }
- }
- }
- // Populate object members match to one of the patterns was made,
- $this->uri = array_shift( $uri_fragments ); // 0 Always contains the original string
- $this->controller = ( !empty($uri_fragments) && array_key_exists( 'controller', $uri_fragments ) ) ? $uri_fragments['controller'] : 'index';
- $this->action = ( !empty($uri_fragments) && array_key_exists( 'action', $uri_fragments ) ) ? $uri_fragments['action'] : 'index';
- // Arguments
- $params = ( !empty($uri_fragments) && array_key_exists( 'params', $uri_fragments ) ) ? $uri_fragments['params'] : null;
- $this->params = ( $params != null ) ? array_filter( explode( '/', $params ) ) : null;
- }
- /** Short Description
- *
- * Long Description
- * @param
- * @return
- */
- public function dispatch() {
- /*
- This is a multiline comment
- */
- $controller = $this->getController() . 'Controller';
- $controller = ucwords( $controller );
- $class = $controller == 'Controller' ? BaseController::ERROR_CONTROLLER : $controller;
- $this->getController() != null
- //is the same as
- $this->getController()
- $error = BaseController::ERROR_CONTROLLER;
- $classDefined = $controller != 'Controller'
- $classExists = class_exists( $class, true );
- $class = $classDefined && $classExists ? $controller : $error;
- $ref = new ReflectionClass( $class );
- if( !$ref->hasMethod( $this->getAction() ) ) {
- trigger_error("Method, "{$this->getAction()}" does not exist.", E_USER_WARNING);
- }
- foreach( $pattern_collection as $route => $patterns )
- {
- foreach( $patterns as $pattern )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement