Internal.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Request Client for internal execution
  4. *
  5. * @package Kohana
  6. * @category Base
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2012 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. * @since 3.1.0
  11. */
  12. class Kohana_Request_Client_Internal extends Request_Client {
  13. /**
  14. * @var array
  15. */
  16. protected $_previous_environment;
  17. /**
  18. * Processes the request, executing the controller action that handles this
  19. * request, determined by the [Route].
  20. *
  21. * $request->execute();
  22. *
  23. * @param Request $request
  24. * @return Response
  25. * @throws Kohana_Exception
  26. * @uses [Kohana::$profiling]
  27. * @uses [Profiler]
  28. */
  29. public function execute_request(Request $request, Response $response)
  30. {
  31. // Create the class prefix
  32. $prefix = 'Controller_';
  33. // Directory
  34. $directory = $request->directory();
  35. // Controller
  36. $controller = $request->controller();
  37. if ($directory)
  38. {
  39. // Add the directory name to the class prefix
  40. $prefix .= str_replace(array('\\', '/'), '_', trim($directory, '/')).'_';
  41. }
  42. if (Kohana::$profiling)
  43. {
  44. // Set the benchmark name
  45. $benchmark = '"'.$request->uri().'"';
  46. if ($request !== Request::$initial AND Request::$current)
  47. {
  48. // Add the parent request uri
  49. $benchmark .= ' « "'.Request::$current->uri().'"';
  50. }
  51. // Start benchmarking
  52. $benchmark = Profiler::start('Requests', $benchmark);
  53. }
  54. // Store the currently active request
  55. $previous = Request::$current;
  56. // Change the current request to this request
  57. Request::$current = $request;
  58. // Is this the initial request
  59. $initial_request = ($request === Request::$initial);
  60. try
  61. {
  62. if ( ! class_exists($prefix.$controller))
  63. {
  64. throw HTTP_Exception::factory(404,
  65. 'The requested URL :uri was not found on this server.',
  66. array(':uri' => $request->uri())
  67. )->request($request);
  68. }
  69. // Load the controller using reflection
  70. $class = new ReflectionClass($prefix.$controller);
  71. if ($class->isAbstract())
  72. {
  73. throw new Kohana_Exception(
  74. 'Cannot create instances of abstract :controller',
  75. array(':controller' => $prefix.$controller)
  76. );
  77. }
  78. // Create a new instance of the controller
  79. $controller = $class->newInstance($request, $response);
  80. // Run the controller's execute() method
  81. $response = $class->getMethod('execute')->invoke($controller);
  82. if ( ! $response instanceof Response)
  83. {
  84. // Controller failed to return a Response.
  85. throw new Kohana_Exception('Controller failed to return a Response');
  86. }
  87. }
  88. catch (HTTP_Exception $e)
  89. {
  90. // Get the response via the Exception
  91. $response = $e->get_response();
  92. }
  93. catch (Exception $e)
  94. {
  95. // Generate an appropriate Response object
  96. $response = Kohana_Exception::_handler($e);
  97. }
  98. // Restore the previous request
  99. Request::$current = $previous;
  100. if (isset($benchmark))
  101. {
  102. // Stop the benchmark
  103. Profiler::stop($benchmark);
  104. }
  105. // Return the response
  106. return $response;
  107. }
  108. } // End Kohana_Request_Client_Internal