App.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 array $urlParams an array with variables extracted from the routes
  62. * @since 6.0.0
  63. */
  64. public function __construct(string $appName, array $urlParams = []) {
  65. $this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams);
  66. }
  67. /**
  68. * @return IAppContainer
  69. * @since 6.0.0
  70. */
  71. public function getContainer(): IAppContainer {
  72. return $this->container;
  73. }
  74. /**
  75. * This function is to be called to create single routes and restful routes based on the given $routes array.
  76. *
  77. * Example code in routes.php of tasks app (it will register two restful resources):
  78. * $routes = array(
  79. * 'resources' => array(
  80. * 'lists' => array('url' => '/tasklists'),
  81. * 'tasks' => array('url' => '/tasklists/{listId}/tasks')
  82. * )
  83. * );
  84. *
  85. * $a = new TasksApp();
  86. * $a->registerRoutes($this, $routes);
  87. *
  88. * @param \OCP\Route\IRouter $router
  89. * @param array $routes
  90. * @since 6.0.0
  91. * @suppress PhanAccessMethodInternal
  92. */
  93. public function registerRoutes(IRouter $router, array $routes) {
  94. $routeConfig = new RouteConfig($this->container, $router, $routes);
  95. $routeConfig->register();
  96. }
  97. /**
  98. * This function is called by the routing component to fire up the frameworks dispatch mechanism.
  99. *
  100. * Example code in routes.php of the task app:
  101. * $this->create('tasks_index', '/')->get()->action(
  102. * function($params){
  103. * $app = new TaskApp($params);
  104. * $app->dispatch('PageController', 'index');
  105. * }
  106. * );
  107. *
  108. *
  109. * Example for for TaskApp implementation:
  110. * class TaskApp extends \OCP\AppFramework\App {
  111. *
  112. * public function __construct($params){
  113. * parent::__construct('tasks', $params);
  114. *
  115. * $this->getContainer()->registerService('PageController', function(IAppContainer $c){
  116. * $a = $c->query('API');
  117. * $r = $c->query('Request');
  118. * return new PageController($a, $r);
  119. * });
  120. * }
  121. * }
  122. *
  123. * @param string $controllerName the name of the controller under which it is
  124. * stored in the DI container
  125. * @param string $methodName the method that you want to call
  126. * @since 6.0.0
  127. */
  128. public function dispatch(string $controllerName, string $methodName) {
  129. \OC\AppFramework\App::main($controllerName, $methodName, $this->container);
  130. }
  131. }