App.php 4.1 KB

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