@@ -45,143 +45,143 @@ |
||
45 | 45 | */ |
46 | 46 | class App { |
47 | 47 | |
48 | - /** @var string[] */ |
|
49 | - private static $nameSpaceCache = []; |
|
50 | - |
|
51 | - /** |
|
52 | - * Turns an app id into a namespace by either reading the appinfo.xml's |
|
53 | - * namespace tag or uppercasing the appid's first letter |
|
54 | - * @param string $appId the app id |
|
55 | - * @param string $topNamespace the namespace which should be prepended to |
|
56 | - * the transformed app id, defaults to OCA\ |
|
57 | - * @return string the starting namespace for the app |
|
58 | - */ |
|
59 | - public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string { |
|
60 | - // Hit the cache! |
|
61 | - if (isset(self::$nameSpaceCache[$appId])) { |
|
62 | - return $topNamespace . self::$nameSpaceCache[$appId]; |
|
63 | - } |
|
64 | - |
|
65 | - $appInfo = \OC_App::getAppInfo($appId); |
|
66 | - if (isset($appInfo['namespace'])) { |
|
67 | - self::$nameSpaceCache[$appId] = trim($appInfo['namespace']); |
|
68 | - } else { |
|
69 | - // if the tag is not found, fall back to uppercasing the first letter |
|
70 | - self::$nameSpaceCache[$appId] = ucfirst($appId); |
|
71 | - } |
|
72 | - |
|
73 | - return $topNamespace . self::$nameSpaceCache[$appId]; |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Shortcut for calling a controller method and printing the result |
|
79 | - * @param string $controllerName the name of the controller under which it is |
|
80 | - * stored in the DI container |
|
81 | - * @param string $methodName the method that you want to call |
|
82 | - * @param DIContainer $container an instance of a pimple container. |
|
83 | - * @param array $urlParams list of URL parameters (optional) |
|
84 | - */ |
|
85 | - public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) { |
|
86 | - if (!is_null($urlParams)) { |
|
87 | - $container[IRequest::class]->setUrlParameters($urlParams); |
|
88 | - } else if (isset($container['urlParams']) && !is_null($container['urlParams'])) { |
|
89 | - $container[IRequest::class]->setUrlParameters($container['urlParams']); |
|
90 | - } |
|
91 | - $appName = $container['AppName']; |
|
92 | - |
|
93 | - // first try $controllerName then go for \OCA\AppName\Controller\$controllerName |
|
94 | - try { |
|
95 | - $controller = $container->query($controllerName); |
|
96 | - } catch(QueryException $e) { |
|
97 | - if ($appName === 'core') { |
|
98 | - $appNameSpace = 'OC\\Core'; |
|
99 | - } else if ($appName === 'settings') { |
|
100 | - $appNameSpace = 'OC\\Settings'; |
|
101 | - } else { |
|
102 | - $appNameSpace = self::buildAppNamespace($appName); |
|
103 | - } |
|
104 | - $controllerName = $appNameSpace . '\\Controller\\' . $controllerName; |
|
105 | - $controller = $container->query($controllerName); |
|
106 | - } |
|
107 | - |
|
108 | - // initialize the dispatcher and run all the middleware before the controller |
|
109 | - /** @var Dispatcher $dispatcher */ |
|
110 | - $dispatcher = $container['Dispatcher']; |
|
111 | - |
|
112 | - list( |
|
113 | - $httpHeaders, |
|
114 | - $responseHeaders, |
|
115 | - $responseCookies, |
|
116 | - $output, |
|
117 | - $response |
|
118 | - ) = $dispatcher->dispatch($controller, $methodName); |
|
119 | - |
|
120 | - $io = $container[IOutput::class]; |
|
121 | - |
|
122 | - if(!is_null($httpHeaders)) { |
|
123 | - $io->setHeader($httpHeaders); |
|
124 | - } |
|
125 | - |
|
126 | - foreach($responseHeaders as $name => $value) { |
|
127 | - $io->setHeader($name . ': ' . $value); |
|
128 | - } |
|
129 | - |
|
130 | - foreach($responseCookies as $name => $value) { |
|
131 | - $expireDate = null; |
|
132 | - if($value['expireDate'] instanceof \DateTime) { |
|
133 | - $expireDate = $value['expireDate']->getTimestamp(); |
|
134 | - } |
|
135 | - $io->setCookie( |
|
136 | - $name, |
|
137 | - $value['value'], |
|
138 | - $expireDate, |
|
139 | - $container->getServer()->getWebRoot(), |
|
140 | - null, |
|
141 | - $container->getServer()->getRequest()->getServerProtocol() === 'https', |
|
142 | - true |
|
143 | - ); |
|
144 | - } |
|
145 | - |
|
146 | - /* |
|
48 | + /** @var string[] */ |
|
49 | + private static $nameSpaceCache = []; |
|
50 | + |
|
51 | + /** |
|
52 | + * Turns an app id into a namespace by either reading the appinfo.xml's |
|
53 | + * namespace tag or uppercasing the appid's first letter |
|
54 | + * @param string $appId the app id |
|
55 | + * @param string $topNamespace the namespace which should be prepended to |
|
56 | + * the transformed app id, defaults to OCA\ |
|
57 | + * @return string the starting namespace for the app |
|
58 | + */ |
|
59 | + public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string { |
|
60 | + // Hit the cache! |
|
61 | + if (isset(self::$nameSpaceCache[$appId])) { |
|
62 | + return $topNamespace . self::$nameSpaceCache[$appId]; |
|
63 | + } |
|
64 | + |
|
65 | + $appInfo = \OC_App::getAppInfo($appId); |
|
66 | + if (isset($appInfo['namespace'])) { |
|
67 | + self::$nameSpaceCache[$appId] = trim($appInfo['namespace']); |
|
68 | + } else { |
|
69 | + // if the tag is not found, fall back to uppercasing the first letter |
|
70 | + self::$nameSpaceCache[$appId] = ucfirst($appId); |
|
71 | + } |
|
72 | + |
|
73 | + return $topNamespace . self::$nameSpaceCache[$appId]; |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Shortcut for calling a controller method and printing the result |
|
79 | + * @param string $controllerName the name of the controller under which it is |
|
80 | + * stored in the DI container |
|
81 | + * @param string $methodName the method that you want to call |
|
82 | + * @param DIContainer $container an instance of a pimple container. |
|
83 | + * @param array $urlParams list of URL parameters (optional) |
|
84 | + */ |
|
85 | + public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) { |
|
86 | + if (!is_null($urlParams)) { |
|
87 | + $container[IRequest::class]->setUrlParameters($urlParams); |
|
88 | + } else if (isset($container['urlParams']) && !is_null($container['urlParams'])) { |
|
89 | + $container[IRequest::class]->setUrlParameters($container['urlParams']); |
|
90 | + } |
|
91 | + $appName = $container['AppName']; |
|
92 | + |
|
93 | + // first try $controllerName then go for \OCA\AppName\Controller\$controllerName |
|
94 | + try { |
|
95 | + $controller = $container->query($controllerName); |
|
96 | + } catch(QueryException $e) { |
|
97 | + if ($appName === 'core') { |
|
98 | + $appNameSpace = 'OC\\Core'; |
|
99 | + } else if ($appName === 'settings') { |
|
100 | + $appNameSpace = 'OC\\Settings'; |
|
101 | + } else { |
|
102 | + $appNameSpace = self::buildAppNamespace($appName); |
|
103 | + } |
|
104 | + $controllerName = $appNameSpace . '\\Controller\\' . $controllerName; |
|
105 | + $controller = $container->query($controllerName); |
|
106 | + } |
|
107 | + |
|
108 | + // initialize the dispatcher and run all the middleware before the controller |
|
109 | + /** @var Dispatcher $dispatcher */ |
|
110 | + $dispatcher = $container['Dispatcher']; |
|
111 | + |
|
112 | + list( |
|
113 | + $httpHeaders, |
|
114 | + $responseHeaders, |
|
115 | + $responseCookies, |
|
116 | + $output, |
|
117 | + $response |
|
118 | + ) = $dispatcher->dispatch($controller, $methodName); |
|
119 | + |
|
120 | + $io = $container[IOutput::class]; |
|
121 | + |
|
122 | + if(!is_null($httpHeaders)) { |
|
123 | + $io->setHeader($httpHeaders); |
|
124 | + } |
|
125 | + |
|
126 | + foreach($responseHeaders as $name => $value) { |
|
127 | + $io->setHeader($name . ': ' . $value); |
|
128 | + } |
|
129 | + |
|
130 | + foreach($responseCookies as $name => $value) { |
|
131 | + $expireDate = null; |
|
132 | + if($value['expireDate'] instanceof \DateTime) { |
|
133 | + $expireDate = $value['expireDate']->getTimestamp(); |
|
134 | + } |
|
135 | + $io->setCookie( |
|
136 | + $name, |
|
137 | + $value['value'], |
|
138 | + $expireDate, |
|
139 | + $container->getServer()->getWebRoot(), |
|
140 | + null, |
|
141 | + $container->getServer()->getRequest()->getServerProtocol() === 'https', |
|
142 | + true |
|
143 | + ); |
|
144 | + } |
|
145 | + |
|
146 | + /* |
|
147 | 147 | * Status 204 does not have a body and no Content Length |
148 | 148 | * Status 304 does not have a body and does not need a Content Length |
149 | 149 | * https://tools.ietf.org/html/rfc7230#section-3.3 |
150 | 150 | * https://tools.ietf.org/html/rfc7230#section-3.3.2 |
151 | 151 | */ |
152 | - if ($httpHeaders !== Http::STATUS_NO_CONTENT && $httpHeaders !== Http::STATUS_NOT_MODIFIED) { |
|
153 | - if ($response instanceof ICallbackResponse) { |
|
154 | - $response->callback($io); |
|
155 | - } else if (!is_null($output)) { |
|
156 | - $io->setHeader('Content-Length: ' . strlen($output)); |
|
157 | - $io->setOutput($output); |
|
158 | - } |
|
159 | - } |
|
160 | - |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Shortcut for calling a controller method and printing the result. |
|
165 | - * Similar to App:main except that no headers will be sent. |
|
166 | - * This should be used for example when registering sections via |
|
167 | - * \OC\AppFramework\Core\API::registerAdmin() |
|
168 | - * |
|
169 | - * @param string $controllerName the name of the controller under which it is |
|
170 | - * stored in the DI container |
|
171 | - * @param string $methodName the method that you want to call |
|
172 | - * @param array $urlParams an array with variables extracted from the routes |
|
173 | - * @param DIContainer $container an instance of a pimple container. |
|
174 | - */ |
|
175 | - public static function part(string $controllerName, string $methodName, array $urlParams, |
|
176 | - DIContainer $container){ |
|
177 | - |
|
178 | - $container['urlParams'] = $urlParams; |
|
179 | - $controller = $container[$controllerName]; |
|
180 | - |
|
181 | - $dispatcher = $container['Dispatcher']; |
|
182 | - |
|
183 | - list(, , $output) = $dispatcher->dispatch($controller, $methodName); |
|
184 | - return $output; |
|
185 | - } |
|
152 | + if ($httpHeaders !== Http::STATUS_NO_CONTENT && $httpHeaders !== Http::STATUS_NOT_MODIFIED) { |
|
153 | + if ($response instanceof ICallbackResponse) { |
|
154 | + $response->callback($io); |
|
155 | + } else if (!is_null($output)) { |
|
156 | + $io->setHeader('Content-Length: ' . strlen($output)); |
|
157 | + $io->setOutput($output); |
|
158 | + } |
|
159 | + } |
|
160 | + |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Shortcut for calling a controller method and printing the result. |
|
165 | + * Similar to App:main except that no headers will be sent. |
|
166 | + * This should be used for example when registering sections via |
|
167 | + * \OC\AppFramework\Core\API::registerAdmin() |
|
168 | + * |
|
169 | + * @param string $controllerName the name of the controller under which it is |
|
170 | + * stored in the DI container |
|
171 | + * @param string $methodName the method that you want to call |
|
172 | + * @param array $urlParams an array with variables extracted from the routes |
|
173 | + * @param DIContainer $container an instance of a pimple container. |
|
174 | + */ |
|
175 | + public static function part(string $controllerName, string $methodName, array $urlParams, |
|
176 | + DIContainer $container){ |
|
177 | + |
|
178 | + $container['urlParams'] = $urlParams; |
|
179 | + $controller = $container[$controllerName]; |
|
180 | + |
|
181 | + $dispatcher = $container['Dispatcher']; |
|
182 | + |
|
183 | + list(, , $output) = $dispatcher->dispatch($controller, $methodName); |
|
184 | + return $output; |
|
185 | + } |
|
186 | 186 | |
187 | 187 | } |
@@ -47,96 +47,96 @@ |
||
47 | 47 | */ |
48 | 48 | class App { |
49 | 49 | |
50 | - /** @var IAppContainer */ |
|
51 | - private $container; |
|
50 | + /** @var IAppContainer */ |
|
51 | + private $container; |
|
52 | 52 | |
53 | - /** |
|
54 | - * Turns an app id into a namespace by convetion. The id is split at the |
|
55 | - * underscores, all parts are camelcased and reassembled. e.g.: |
|
56 | - * some_app_id -> OCA\SomeAppId |
|
57 | - * @param string $appId the app id |
|
58 | - * @param string $topNamespace the namespace which should be prepended to |
|
59 | - * the transformed app id, defaults to OCA\ |
|
60 | - * @return string the starting namespace for the app |
|
61 | - * @since 8.0.0 |
|
62 | - */ |
|
63 | - public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string { |
|
64 | - return \OC\AppFramework\App::buildAppNamespace($appId, $topNamespace); |
|
65 | - } |
|
53 | + /** |
|
54 | + * Turns an app id into a namespace by convetion. The id is split at the |
|
55 | + * underscores, all parts are camelcased and reassembled. e.g.: |
|
56 | + * some_app_id -> OCA\SomeAppId |
|
57 | + * @param string $appId the app id |
|
58 | + * @param string $topNamespace the namespace which should be prepended to |
|
59 | + * the transformed app id, defaults to OCA\ |
|
60 | + * @return string the starting namespace for the app |
|
61 | + * @since 8.0.0 |
|
62 | + */ |
|
63 | + public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string { |
|
64 | + return \OC\AppFramework\App::buildAppNamespace($appId, $topNamespace); |
|
65 | + } |
|
66 | 66 | |
67 | 67 | |
68 | - /** |
|
69 | - * @param array $urlParams an array with variables extracted from the routes |
|
70 | - * @since 6.0.0 |
|
71 | - */ |
|
72 | - public function __construct(string $appName, array $urlParams = []) { |
|
73 | - $this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams); |
|
74 | - } |
|
68 | + /** |
|
69 | + * @param array $urlParams an array with variables extracted from the routes |
|
70 | + * @since 6.0.0 |
|
71 | + */ |
|
72 | + public function __construct(string $appName, array $urlParams = []) { |
|
73 | + $this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @return IAppContainer |
|
78 | - * @since 6.0.0 |
|
79 | - */ |
|
80 | - public function getContainer(): IAppContainer { |
|
81 | - return $this->container; |
|
82 | - } |
|
76 | + /** |
|
77 | + * @return IAppContainer |
|
78 | + * @since 6.0.0 |
|
79 | + */ |
|
80 | + public function getContainer(): IAppContainer { |
|
81 | + return $this->container; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * This function is to be called to create single routes and restful routes based on the given $routes array. |
|
86 | - * |
|
87 | - * Example code in routes.php of tasks app (it will register two restful resources): |
|
88 | - * $routes = array( |
|
89 | - * 'resources' => array( |
|
90 | - * 'lists' => array('url' => '/tasklists'), |
|
91 | - * 'tasks' => array('url' => '/tasklists/{listId}/tasks') |
|
92 | - * ) |
|
93 | - * ); |
|
94 | - * |
|
95 | - * $a = new TasksApp(); |
|
96 | - * $a->registerRoutes($this, $routes); |
|
97 | - * |
|
98 | - * @param \OCP\Route\IRouter $router |
|
99 | - * @param array $routes |
|
100 | - * @since 6.0.0 |
|
101 | - * @suppress PhanAccessMethodInternal |
|
102 | - */ |
|
103 | - public function registerRoutes(IRouter $router, array $routes) { |
|
104 | - $routeConfig = new RouteConfig($this->container, $router, $routes); |
|
105 | - $routeConfig->register(); |
|
106 | - } |
|
84 | + /** |
|
85 | + * This function is to be called to create single routes and restful routes based on the given $routes array. |
|
86 | + * |
|
87 | + * Example code in routes.php of tasks app (it will register two restful resources): |
|
88 | + * $routes = array( |
|
89 | + * 'resources' => array( |
|
90 | + * 'lists' => array('url' => '/tasklists'), |
|
91 | + * 'tasks' => array('url' => '/tasklists/{listId}/tasks') |
|
92 | + * ) |
|
93 | + * ); |
|
94 | + * |
|
95 | + * $a = new TasksApp(); |
|
96 | + * $a->registerRoutes($this, $routes); |
|
97 | + * |
|
98 | + * @param \OCP\Route\IRouter $router |
|
99 | + * @param array $routes |
|
100 | + * @since 6.0.0 |
|
101 | + * @suppress PhanAccessMethodInternal |
|
102 | + */ |
|
103 | + public function registerRoutes(IRouter $router, array $routes) { |
|
104 | + $routeConfig = new RouteConfig($this->container, $router, $routes); |
|
105 | + $routeConfig->register(); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * This function is called by the routing component to fire up the frameworks dispatch mechanism. |
|
110 | - * |
|
111 | - * Example code in routes.php of the task app: |
|
112 | - * $this->create('tasks_index', '/')->get()->action( |
|
113 | - * function($params){ |
|
114 | - * $app = new TaskApp($params); |
|
115 | - * $app->dispatch('PageController', 'index'); |
|
116 | - * } |
|
117 | - * ); |
|
118 | - * |
|
119 | - * |
|
120 | - * Example for for TaskApp implementation: |
|
121 | - * class TaskApp extends \OCP\AppFramework\App { |
|
122 | - * |
|
123 | - * public function __construct($params){ |
|
124 | - * parent::__construct('tasks', $params); |
|
125 | - * |
|
126 | - * $this->getContainer()->registerService('PageController', function(IAppContainer $c){ |
|
127 | - * $a = $c->query('API'); |
|
128 | - * $r = $c->query('Request'); |
|
129 | - * return new PageController($a, $r); |
|
130 | - * }); |
|
131 | - * } |
|
132 | - * } |
|
133 | - * |
|
134 | - * @param string $controllerName the name of the controller under which it is |
|
135 | - * stored in the DI container |
|
136 | - * @param string $methodName the method that you want to call |
|
137 | - * @since 6.0.0 |
|
138 | - */ |
|
139 | - public function dispatch(string $controllerName, string $methodName) { |
|
140 | - \OC\AppFramework\App::main($controllerName, $methodName, $this->container); |
|
141 | - } |
|
108 | + /** |
|
109 | + * This function is called by the routing component to fire up the frameworks dispatch mechanism. |
|
110 | + * |
|
111 | + * Example code in routes.php of the task app: |
|
112 | + * $this->create('tasks_index', '/')->get()->action( |
|
113 | + * function($params){ |
|
114 | + * $app = new TaskApp($params); |
|
115 | + * $app->dispatch('PageController', 'index'); |
|
116 | + * } |
|
117 | + * ); |
|
118 | + * |
|
119 | + * |
|
120 | + * Example for for TaskApp implementation: |
|
121 | + * class TaskApp extends \OCP\AppFramework\App { |
|
122 | + * |
|
123 | + * public function __construct($params){ |
|
124 | + * parent::__construct('tasks', $params); |
|
125 | + * |
|
126 | + * $this->getContainer()->registerService('PageController', function(IAppContainer $c){ |
|
127 | + * $a = $c->query('API'); |
|
128 | + * $r = $c->query('Request'); |
|
129 | + * return new PageController($a, $r); |
|
130 | + * }); |
|
131 | + * } |
|
132 | + * } |
|
133 | + * |
|
134 | + * @param string $controllerName the name of the controller under which it is |
|
135 | + * stored in the DI container |
|
136 | + * @param string $methodName the method that you want to call |
|
137 | + * @since 6.0.0 |
|
138 | + */ |
|
139 | + public function dispatch(string $controllerName, string $methodName) { |
|
140 | + \OC\AppFramework\App::main($controllerName, $methodName, $this->container); |
|
141 | + } |
|
142 | 142 | } |