You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.9 KiB

3 years ago
  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. /**
  34. * Public interface of ownCloud for apps to use.
  35. * AppFramework/App class
  36. */
  37. namespace OCP\AppFramework;
  38. use OC\AppFramework\Routing\RouteConfig;
  39. use OC\ServerContainer;
  40. use OCP\Route\IRouter;
  41. /**
  42. * Class App
  43. *
  44. * Any application must inherit this call - all controller instances to be used are
  45. * to be registered using IContainer::registerService
  46. * @since 6.0.0
  47. */
  48. class App {
  49. /** @var IAppContainer */
  50. private $container;
  51. /**
  52. * Turns an app id into a namespace by convention. The id is split at the
  53. * underscores, all parts are CamelCased and reassembled. e.g.:
  54. * some_app_id -> OCA\SomeAppId
  55. * @param string $appId the app id
  56. * @param string $topNamespace the namespace which should be prepended to
  57. * the transformed app id, defaults to OCA\
  58. * @return string the starting namespace for the app
  59. * @since 8.0.0
  60. */
  61. public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string {
  62. return \OC\AppFramework\App::buildAppNamespace($appId, $topNamespace);
  63. }
  64. /**
  65. * @param string $appName
  66. * @param array $urlParams an array with variables extracted from the routes
  67. * @since 6.0.0
  68. */
  69. public function __construct(string $appName, array $urlParams = []) {
  70. $runIsSetupDirectly = \OC::$server->getConfig()->getSystemValueBool('debug')
  71. && (PHP_VERSION_ID < 70400 || (PHP_VERSION_ID >= 70400 && !ini_get('zend.exception_ignore_args')));
  72. if ($runIsSetupDirectly) {
  73. $applicationClassName = get_class($this);
  74. $e = new \RuntimeException('App class ' . $applicationClassName . ' is not setup via query() but directly');
  75. $setUpViaQuery = false;
  76. $classNameParts = explode('\\', trim($applicationClassName, '\\'));
  77. foreach ($e->getTrace() as $step) {
  78. if (isset($step['class'], $step['function'], $step['args'][0]) &&
  79. $step['class'] === ServerContainer::class &&
  80. $step['function'] === 'query' &&
  81. $step['args'][0] === $applicationClassName) {
  82. $setUpViaQuery = true;
  83. break;
  84. } elseif (isset($step['class'], $step['function'], $step['args'][0]) &&
  85. $step['class'] === ServerContainer::class &&
  86. $step['function'] === 'getAppContainer' &&
  87. $step['args'][1] === $classNameParts[1]) {
  88. $setUpViaQuery = true;
  89. break;
  90. }
  91. }
  92. if (!$setUpViaQuery && $applicationClassName !== \OCP\AppFramework\App::class) {
  93. \OC::$server->getLogger()->logException($e, [
  94. 'app' => $appName,
  95. ]);
  96. }
  97. }
  98. try {
  99. $this->container = \OC::$server->getRegisteredAppContainer($appName);
  100. } catch (QueryException $e) {
  101. $this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams);
  102. }
  103. }
  104. /**
  105. * @return IAppContainer
  106. * @since 6.0.0
  107. */
  108. public function getContainer(): IAppContainer {
  109. return $this->container;
  110. }
  111. /**
  112. * This function is to be called to create single routes and restful routes based on the given $routes array.
  113. *
  114. * Example code in routes.php of tasks app (it will register two restful resources):
  115. * $routes = array(
  116. * 'resources' => array(
  117. * 'lists' => array('url' => '/tasklists'),
  118. * 'tasks' => array('url' => '/tasklists/{listId}/tasks')
  119. * )
  120. * );
  121. *
  122. * $a = new TasksApp();
  123. * $a->registerRoutes($this, $routes);
  124. *
  125. * @param \OCP\Route\IRouter $router
  126. * @param array $routes
  127. * @since 6.0.0
  128. * @suppress PhanAccessMethodInternal
  129. * @deprecated 20.0.0 Just return an array from your routes.php
  130. */
  131. public function registerRoutes(IRouter $router, array $routes) {
  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. }