1
0

App.php 4.1 KB

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