Advertisement
Guest User

Untitled

a guest
Sep 5th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. public function dispatch()
  2. {
  3. try
  4. {
  5. // Instantiate the requested controller class
  6. $class = ( $this->getController() != null ) ? ucwords( $this->getController() ) . 'Controller' : BaseController::ERROR_CONTROLLER;
  7.  
  8. /**
  9. * Ensure this is a class that's contained in the Manifest get_class(..,true);
  10. * If it doesn't exist in the manifest, it will not be executed.
  11. */
  12. $ref = new ReflectionClass( ( class_exists($class, true) ) ? $class : BaseController::ERROR_CONTROLLER );
  13.  
  14. /**
  15. * Ensure that the class follows conventions:
  16. * 1. Controller class must implement IController
  17. * 2. Controller class must implement a Controller::index() method -- this is implied since
  18. * the first condition won't be satisfied unless the IController interface is implemented.
  19. */
  20. if( $ref->implementsInterface('IController') )
  21. {
  22. // Instantiate a new Controller
  23. $controller = $ref->newInstance( $this->registry );
  24.  
  25. // Check to see if the method exists
  26. if( !$ref->hasMethod( $this->getAction() ) ) trigger_error("Method, "{$this->getAction()}" does not exist.", E_USER_WARNING);
  27.  
  28. // Get a ReflectionMethod object
  29. $method = $ref->getMethod( ( $this->getAction() != null && $ref->hasMethod( $this->getAction() ) ) ? $this->getAction() : BaseController::DEFAULT_METHOD );
  30.  
  31. // Track the request status
  32. $this->request->requestStatus( ( $ref->name == BaseController::ERROR_CONTROLLER ) ? HttpStatus::HTTP_NOT_FOUND : HttpStatus::HTTP_OK );
  33.  
  34. // Invoke the Controller::method()
  35. $method->invoke( $controller );
  36. }
  37. else
  38. {
  39. trigger_error( 'Interface IContoller must be implemented.', E_USER_ERROR );
  40. }
  41. }
  42. catch( Exception $e )
  43. {
  44. header( HttpStatus::getHttpHeader( HttpStatus::HTTP_NOT_IMPLEMENTED ) );
  45. throw new Exception(__METHOD__ . ' threw an exception: ' . $e->getMessage());
  46. }
  47.  
  48. }
  49.  
  50. /**
  51. * This methods parses a given URI into it's various segments, e.g. controller|action|params
  52. *
  53. * @param array $pattern_collection A list of regex patterns to match
  54. */
  55. private function parseURI( array $pattern_collection )
  56. {
  57. $request_uri = $_SERVER['REQUEST_URI'];
  58.  
  59. // Find the first match (not necessarily the best match)
  60. foreach( $pattern_collection as $route => $patterns )
  61. {
  62. foreach( $patterns as $pattern )
  63. {
  64.  
  65. $match = preg_match( $pattern, $request_uri, $uri_fragments );
  66. if( $match && $match !== false)
  67. {
  68. break 2;
  69. }
  70. }
  71. }
  72.  
  73. // Populate object members match to one of the patterns was made,
  74. $this->uri = array_shift( $uri_fragments ); // 0 Always contains the original string
  75. $this->controller = ( !empty($uri_fragments) && array_key_exists( 'controller', $uri_fragments ) ) ? $uri_fragments['controller'] : 'index';
  76. $this->action = ( !empty($uri_fragments) && array_key_exists( 'action', $uri_fragments ) ) ? $uri_fragments['action'] : 'index';
  77.  
  78. // Arguments
  79. $params = ( !empty($uri_fragments) && array_key_exists( 'params', $uri_fragments ) ) ? $uri_fragments['params'] : null;
  80. $this->params = ( $params != null ) ? array_filter( explode( '/', $params ) ) : null;
  81. }
  82.  
  83. /** Short Description
  84. *
  85. * Long Description
  86. * @param
  87. * @return
  88. */
  89. public function dispatch() {
  90.  
  91. /*
  92. This is a multiline comment
  93. */
  94.  
  95. $controller = $this->getController() . 'Controller';
  96. $controller = ucwords( $controller );
  97. $class = $controller == 'Controller' ? BaseController::ERROR_CONTROLLER : $controller;
  98.  
  99. $this->getController() != null
  100. //is the same as
  101. $this->getController()
  102.  
  103. $error = BaseController::ERROR_CONTROLLER;
  104. $classDefined = $controller != 'Controller'
  105. $classExists = class_exists( $class, true );
  106. $class = $classDefined && $classExists ? $controller : $error;
  107. $ref = new ReflectionClass( $class );
  108.  
  109. if( !$ref->hasMethod( $this->getAction() ) ) {
  110. trigger_error("Method, "{$this->getAction()}" does not exist.", E_USER_WARNING);
  111. }
  112.  
  113. foreach( $pattern_collection as $route => $patterns )
  114. {
  115. foreach( $patterns as $pattern )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement