Internal.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Request Client for internal execution
  4. *
  5. * @package Kohana
  6. * @category Base
  7. * @author Kohana Team
  8. * @copyright (c) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  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(['\\', '/'], '_', 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. try
  59. {
  60. if ( ! class_exists($prefix.$controller))
  61. {
  62. throw HTTP_Exception::factory(404,
  63. 'The requested URL :uri was not found on this server.',
  64. [':uri' => $request->uri()]
  65. )->request($request);
  66. }
  67. // Load the controller using reflection
  68. $class = new ReflectionClass($prefix.$controller);
  69. if ($class->isAbstract())
  70. {
  71. throw new Kohana_Exception(
  72. 'Cannot create instances of abstract :controller',
  73. [':controller' => $prefix.$controller]
  74. );
  75. }
  76. // Create a new instance of the controller
  77. $controller = $class->newInstance($request, $response);
  78. // Run the controller's execute() method
  79. $response = $class->getMethod('execute')->invoke($controller);
  80. if ( ! $response instanceof Response)
  81. {
  82. // Controller failed to return a Response.
  83. throw new Kohana_Exception('Controller failed to return a Response');
  84. }
  85. }
  86. catch (HTTP_Exception $e)
  87. {
  88. // Store the request context in the Exception
  89. if ($e->request() === NULL)
  90. {
  91. $e->request($request);
  92. }
  93. // Get the response via the Exception
  94. $response = $e->get_response();
  95. }
  96. catch (Exception $e)
  97. {
  98. // Generate an appropriate Response object
  99. $response = Kohana_Exception::_handler($e);
  100. }
  101. // Restore the previous request
  102. Request::$current = $previous;
  103. if (isset($benchmark))
  104. {
  105. // Stop the benchmark
  106. Profiler::stop($benchmark);
  107. }
  108. // Return the response
  109. return $response;
  110. }
  111. }