1
0

App.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCP\AppFramework;
  9. use OC\AppFramework\Routing\RouteConfig;
  10. use OC\Route\Router;
  11. use OC\ServerContainer;
  12. use OCP\Route\IRouter;
  13. use Psr\Log\LoggerInterface;
  14. /**
  15. * Class App
  16. *
  17. * Any application must inherit this call - all controller instances to be used are
  18. * to be registered using IContainer::registerService
  19. * @since 6.0.0
  20. */
  21. class App {
  22. /** @var IAppContainer */
  23. private $container;
  24. /**
  25. * Turns an app id into a namespace by convention. The id is split at the
  26. * underscores, all parts are CamelCased and reassembled. e.g.:
  27. * some_app_id -> OCA\SomeAppId
  28. * @param string $appId the app id
  29. * @param string $topNamespace the namespace which should be prepended to
  30. * the transformed app id, defaults to OCA\
  31. * @return string the starting namespace for the app
  32. * @since 8.0.0
  33. */
  34. public static function buildAppNamespace(string $appId, string $topNamespace = 'OCA\\'): string {
  35. return \OC\AppFramework\App::buildAppNamespace($appId, $topNamespace);
  36. }
  37. /**
  38. * @param string $appName
  39. * @param array $urlParams an array with variables extracted from the routes
  40. * @since 6.0.0
  41. */
  42. public function __construct(string $appName, array $urlParams = []) {
  43. $runIsSetupDirectly = \OC::$server->getConfig()->getSystemValueBool('debug')
  44. && !ini_get('zend.exception_ignore_args');
  45. if ($runIsSetupDirectly) {
  46. $applicationClassName = get_class($this);
  47. $e = new \RuntimeException('App class ' . $applicationClassName . ' is not setup via query() but directly');
  48. $setUpViaQuery = false;
  49. $classNameParts = explode('\\', trim($applicationClassName, '\\'));
  50. foreach ($e->getTrace() as $step) {
  51. if (isset($step['class'], $step['function'], $step['args'][0]) &&
  52. $step['class'] === ServerContainer::class &&
  53. $step['function'] === 'query' &&
  54. $step['args'][0] === $applicationClassName) {
  55. $setUpViaQuery = true;
  56. break;
  57. } elseif (isset($step['class'], $step['function'], $step['args'][0]) &&
  58. $step['class'] === ServerContainer::class &&
  59. $step['function'] === 'getAppContainer' &&
  60. $step['args'][1] === $classNameParts[1]) {
  61. $setUpViaQuery = true;
  62. break;
  63. }
  64. }
  65. if (!$setUpViaQuery && $applicationClassName !== \OCP\AppFramework\App::class) {
  66. \OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), [
  67. 'app' => $appName,
  68. 'exception' => $e,
  69. ]);
  70. }
  71. }
  72. try {
  73. $this->container = \OC::$server->getRegisteredAppContainer($appName);
  74. } catch (QueryException $e) {
  75. $this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams);
  76. }
  77. }
  78. /**
  79. * @return IAppContainer
  80. * @since 6.0.0
  81. */
  82. public function getContainer(): IAppContainer {
  83. return $this->container;
  84. }
  85. /**
  86. * This function is to be called to create single routes and restful routes based on the given $routes array.
  87. *
  88. * Example code in routes.php of tasks app (it will register two restful resources):
  89. * $routes = array(
  90. * 'resources' => array(
  91. * 'lists' => array('url' => '/tasklists'),
  92. * 'tasks' => array('url' => '/tasklists/{listId}/tasks')
  93. * )
  94. * );
  95. *
  96. * $a = new TasksApp();
  97. * $a->registerRoutes($this, $routes);
  98. *
  99. * @param \OCP\Route\IRouter $router
  100. * @param array $routes
  101. * @since 6.0.0
  102. * @suppress PhanAccessMethodInternal
  103. * @deprecated 20.0.0 Just return an array from your routes.php
  104. */
  105. public function registerRoutes(IRouter $router, array $routes) {
  106. if (!($router instanceof Router)) {
  107. throw new \RuntimeException('Can only setup routes with real router');
  108. }
  109. $routeConfig = new RouteConfig($this->container, $router, $routes);
  110. $routeConfig->register();
  111. }
  112. /**
  113. * This function is called by the routing component to fire up the frameworks dispatch mechanism.
  114. *
  115. * Example code in routes.php of the task app:
  116. * $this->create('tasks_index', '/')->get()->action(
  117. * function($params){
  118. * $app = new TaskApp($params);
  119. * $app->dispatch('PageController', 'index');
  120. * }
  121. * );
  122. *
  123. *
  124. * Example for for TaskApp implementation:
  125. * class TaskApp extends \OCP\AppFramework\App {
  126. *
  127. * public function __construct($params){
  128. * parent::__construct('tasks', $params);
  129. *
  130. * $this->getContainer()->registerService('PageController', function(IAppContainer $c){
  131. * $a = $c->query('API');
  132. * $r = $c->query('Request');
  133. * return new PageController($a, $r);
  134. * });
  135. * }
  136. * }
  137. *
  138. * @param string $controllerName the name of the controller under which it is
  139. * stored in the DI container
  140. * @param string $methodName the method that you want to call
  141. * @since 6.0.0
  142. */
  143. public function dispatch(string $controllerName, string $methodName) {
  144. \OC\AppFramework\App::main($controllerName, $methodName, $this->container);
  145. }
  146. }