App.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Thomas Tanghus <thomas@tanghus.net>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. /**
  32. * Public interface of ownCloud for apps to use.
  33. * AppFramework/App class
  34. */
  35. namespace OCP\AppFramework;
  36. use OC\AppFramework\Routing\RouteConfig;
  37. use OC\ServerContainer;
  38. use OCP\Route\IRouter;
  39. /**
  40. * Class App
  41. * @package OCP\AppFramework
  42. *
  43. * Any application must inherit this call - all controller instances to be used are
  44. * to be registered using IContainer::registerService
  45. * @since 6.0.0
  46. */
  47. class App {
  48. /** @var IAppContainer */
  49. private $container;
  50. /**
  51. * Turns an app id into a namespace by convention. The id is split at the
  52. * underscores, all parts are CamelCased and reassembled. e.g.:
  53. * some_app_id -> OCA\SomeAppId
  54. * @param string $appId the app id
  55. * @param string $topNamespace the namespace which should be prepended to
  56. * the transformed app id, defaults to OCA\
  57. * @return string the starting namespace for the app
  58. * @since 8.0.0
  59. */
  60. public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string {
  61. return \OC\AppFramework\App::buildAppNamespace($appId, $topNamespace);
  62. }
  63. /**
  64. * @param string $appName
  65. * @param array $urlParams an array with variables extracted from the routes
  66. * @since 6.0.0
  67. */
  68. public function __construct(string $appName, array $urlParams = []) {
  69. if (\OC::$server->getConfig()->getSystemValueBool('debug')) {
  70. $applicationClassName = get_class($this);
  71. $e = new \RuntimeException('App class ' . $applicationClassName . ' is not setup via query() but directly');
  72. $setUpViaQuery = false;
  73. $classNameParts = explode('\\', trim($applicationClassName, '\\'));
  74. foreach ($e->getTrace() as $step) {
  75. if (isset($step['class'], $step['function'], $step['args'][0]) &&
  76. $step['class'] === ServerContainer::class &&
  77. $step['function'] === 'query' &&
  78. $step['args'][0] === $applicationClassName) {
  79. $setUpViaQuery = true;
  80. break;
  81. } else if (isset($step['class'], $step['function'], $step['args'][0]) &&
  82. $step['class'] === ServerContainer::class &&
  83. $step['function'] === 'getAppContainer' &&
  84. $step['args'][1] === $classNameParts[1]) {
  85. $setUpViaQuery = true;
  86. break;
  87. }
  88. }
  89. if (!$setUpViaQuery && $applicationClassName !== \OCP\AppFramework\App::class) {
  90. \OC::$server->getLogger()->logException($e, [
  91. 'app' => $appName,
  92. ]);
  93. }
  94. }
  95. try {
  96. $this->container = \OC::$server->getRegisteredAppContainer($appName);
  97. } catch (QueryException $e) {
  98. $this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams);
  99. }
  100. }
  101. /**
  102. * @return IAppContainer
  103. * @since 6.0.0
  104. */
  105. public function getContainer(): IAppContainer {
  106. return $this->container;
  107. }
  108. /**
  109. * This function is to be called to create single routes and restful routes based on the given $routes array.
  110. *
  111. * Example code in routes.php of tasks app (it will register two restful resources):
  112. * $routes = array(
  113. * 'resources' => array(
  114. * 'lists' => array('url' => '/tasklists'),
  115. * 'tasks' => array('url' => '/tasklists/{listId}/tasks')
  116. * )
  117. * );
  118. *
  119. * $a = new TasksApp();
  120. * $a->registerRoutes($this, $routes);
  121. *
  122. * @param \OCP\Route\IRouter $router
  123. * @param array $routes
  124. * @since 6.0.0
  125. * @suppress PhanAccessMethodInternal
  126. */
  127. public function registerRoutes(IRouter $router, array $routes) {
  128. $routeConfig = new RouteConfig($this->container, $router, $routes);
  129. $routeConfig->register();
  130. }
  131. /**
  132. * This function is called by the routing component to fire up the frameworks dispatch mechanism.
  133. *
  134. * Example code in routes.php of the task app:
  135. * $this->create('tasks_index', '/')->get()->action(
  136. * function($params){
  137. * $app = new TaskApp($params);
  138. * $app->dispatch('PageController', 'index');
  139. * }
  140. * );
  141. *
  142. *
  143. * Example for for TaskApp implementation:
  144. * class TaskApp extends \OCP\AppFramework\App {
  145. *
  146. * public function __construct($params){
  147. * parent::__construct('tasks', $params);
  148. *
  149. * $this->getContainer()->registerService('PageController', function(IAppContainer $c){
  150. * $a = $c->query('API');
  151. * $r = $c->query('Request');
  152. * return new PageController($a, $r);
  153. * });
  154. * }
  155. * }
  156. *
  157. * @param string $controllerName the name of the controller under which it is
  158. * stored in the DI container
  159. * @param string $methodName the method that you want to call
  160. * @since 6.0.0
  161. */
  162. public function dispatch(string $controllerName, string $methodName) {
  163. \OC\AppFramework\App::main($controllerName, $methodName, $this->container);
  164. }
  165. }