app.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\AppFramework;
  23. use OC\AppFramework\DependencyInjection\DIContainer;
  24. use OCP\AppFramework\IAppContainer;
  25. /**
  26. * Entry point for every request in your app. You can consider this as your
  27. * public static void main() method
  28. *
  29. * Handles all the dependency injection, controllers and output flow
  30. */
  31. class App {
  32. /**
  33. * Shortcut for calling a controller method and printing the result
  34. * @param string $controllerName the name of the controller under which it is
  35. * stored in the DI container
  36. * @param string $methodName the method that you want to call
  37. * @param DIContainer $container an instance of a pimple container.
  38. * @param array $urlParams list of URL parameters (optional)
  39. */
  40. public static function main($controllerName, $methodName, DIContainer $container, array $urlParams = null) {
  41. if (!is_null($urlParams)) {
  42. $container['urlParams'] = $urlParams;
  43. }
  44. $controller = $container[$controllerName];
  45. // initialize the dispatcher and run all the middleware before the controller
  46. $dispatcher = $container['Dispatcher'];
  47. list($httpHeaders, $responseHeaders, $output) =
  48. $dispatcher->dispatch($controller, $methodName);
  49. if(!is_null($httpHeaders)) {
  50. header($httpHeaders);
  51. }
  52. foreach($responseHeaders as $name => $value) {
  53. header($name . ': ' . $value);
  54. }
  55. if(!is_null($output)) {
  56. header('Content-Length: ' . strlen($output));
  57. print($output);
  58. }
  59. }
  60. /**
  61. * Shortcut for calling a controller method and printing the result.
  62. * Similar to App:main except that no headers will be sent.
  63. * This should be used for example when registering sections via
  64. * \OC\AppFramework\Core\API::registerAdmin()
  65. *
  66. * @param string $controllerName the name of the controller under which it is
  67. * stored in the DI container
  68. * @param string $methodName the method that you want to call
  69. * @param array $urlParams an array with variables extracted from the routes
  70. * @param DIContainer $container an instance of a pimple container.
  71. */
  72. public static function part($controllerName, $methodName, array $urlParams,
  73. DIContainer $container){
  74. $container['urlParams'] = $urlParams;
  75. $controller = $container[$controllerName];
  76. $dispatcher = $container['Dispatcher'];
  77. list(, , $output) = $dispatcher->dispatch($controller, $methodName);
  78. return $output;
  79. }
  80. }