1
0

App.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Thomas Tanghus <thomas@tanghus.net>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCP\AppFramework;
  34. use OC\AppFramework\Routing\RouteConfig;
  35. use OC\Route\Router;
  36. use OC\ServerContainer;
  37. use OCP\Route\IRouter;
  38. /**
  39. * Class App
  40. *
  41. * Any application must inherit this call - all controller instances to be used are
  42. * to be registered using IContainer::registerService
  43. * @since 6.0.0
  44. */
  45. class App {
  46. /** @var IAppContainer */
  47. private $container;
  48. /**
  49. * Turns an app id into a namespace by convention. The id is split at the
  50. * underscores, all parts are CamelCased and reassembled. e.g.:
  51. * some_app_id -> OCA\SomeAppId
  52. * @param string $appId the app id
  53. * @param string $topNamespace the namespace which should be prepended to
  54. * the transformed app id, defaults to OCA\
  55. * @return string the starting namespace for the app
  56. * @since 8.0.0
  57. */
  58. public static function buildAppNamespace(string $appId, string $topNamespace = 'OCA\\'): string {
  59. return \OC\AppFramework\App::buildAppNamespace($appId, $topNamespace);
  60. }
  61. /**
  62. * @param string $appName
  63. * @param array $urlParams an array with variables extracted from the routes
  64. * @since 6.0.0
  65. */
  66. public function __construct(string $appName, array $urlParams = []) {
  67. $runIsSetupDirectly = \OC::$server->getConfig()->getSystemValueBool('debug')
  68. && (PHP_VERSION_ID < 70400 || (PHP_VERSION_ID >= 70400 && !ini_get('zend.exception_ignore_args')));
  69. if ($runIsSetupDirectly) {
  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. } elseif (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. * @deprecated 20.0.0 Just return an array from your routes.php
  127. */
  128. public function registerRoutes(IRouter $router, array $routes) {
  129. if (!($router instanceof Router)) {
  130. throw new \RuntimeException('Can only setup routes with real router');
  131. }
  132. $routeConfig = new RouteConfig($this->container, $router, $routes);
  133. $routeConfig->register();
  134. }
  135. /**
  136. * This function is called by the routing component to fire up the frameworks dispatch mechanism.
  137. *
  138. * Example code in routes.php of the task app:
  139. * $this->create('tasks_index', '/')->get()->action(
  140. * function($params){
  141. * $app = new TaskApp($params);
  142. * $app->dispatch('PageController', 'index');
  143. * }
  144. * );
  145. *
  146. *
  147. * Example for for TaskApp implementation:
  148. * class TaskApp extends \OCP\AppFramework\App {
  149. *
  150. * public function __construct($params){
  151. * parent::__construct('tasks', $params);
  152. *
  153. * $this->getContainer()->registerService('PageController', function(IAppContainer $c){
  154. * $a = $c->query('API');
  155. * $r = $c->query('Request');
  156. * return new PageController($a, $r);
  157. * });
  158. * }
  159. * }
  160. *
  161. * @param string $controllerName the name of the controller under which it is
  162. * stored in the DI container
  163. * @param string $methodName the method that you want to call
  164. * @since 6.0.0
  165. */
  166. public function dispatch(string $controllerName, string $methodName) {
  167. \OC\AppFramework\App::main($controllerName, $methodName, $this->container);
  168. }
  169. }