app.php 4.0 KB

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