App.php 5.9 KB

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