1
0

App.php 4.4 KB

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