From: Andrey Kutejko Date: Fri, 15 Mar 2019 14:35:58 +0000 (+0100) Subject: Spread usage of Container X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=87a793440a72b0024da242922c486caca73aa620;p=ipf.git Spread usage of Container --- diff --git a/ipf.php b/ipf.php index 6b582bc..8c8c128 100644 --- a/ipf.php +++ b/ipf.php @@ -7,9 +7,10 @@ final class IPF return '0.5 beta'; } + /** @var IPF_Settings */ public static $settings = null; - private static function checkSettings($settings) + private static function checkSettings(IPF_Settings $settings) { $db = $settings->get('database'); diff --git a/ipf/admin/controllers/base.php b/ipf/admin/controllers/base.php index 77efb17..c6a2970 100644 --- a/ipf/admin/controllers/base.php +++ b/ipf/admin/controllers/base.php @@ -22,7 +22,7 @@ abstract class IPF_Admin_Base_Controller extends IPF_Controller */ protected function adminApplication() { - foreach ($this->project->appList() as $app) { + foreach ($this->container['apps'] as $app) { if ($app instanceof IPF_Admin_App) return $app; } diff --git a/ipf/admin/controllers/file_browser.php b/ipf/admin/controllers/file_browser.php index 342509d..7040da0 100644 --- a/ipf/admin/controllers/file_browser.php +++ b/ipf/admin/controllers/file_browser.php @@ -136,7 +136,7 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller protected function backToIndex() { - $url = $this->project->router->reverse(array('IPF_Admin_FileBrowser_Controller', 'index')) . '?dir=' . urlencode($this->relative); + $url = $this->container['router']->reverse(array('IPF_Admin_FileBrowser_Controller', 'index')) . '?dir=' . urlencode($this->relative); return new IPF_HTTP_Response_Redirect($url); } diff --git a/ipf/admin/controllers/user.php b/ipf/admin/controllers/user.php index 4f3e8f9..4430c4d 100644 --- a/ipf/admin/controllers/user.php +++ b/ipf/admin/controllers/user.php @@ -8,7 +8,7 @@ class IPF_Admin_User_Controller extends IPF_Admin_Base_Controller */ private function authApp() { - foreach ($this->project->appList() as $app) + foreach ($this->container['apps'] as $app) if ($app instanceof IPF_Auth_App) return $app; throw new Exception('Auth app is not registered in project.'); @@ -18,7 +18,7 @@ class IPF_Admin_User_Controller extends IPF_Admin_Base_Controller { $success_url = trim(\PFF\Arr::get($this->request->REQUEST, 'next', '')); if (!$success_url) - $success_url = $this->project->router->reverse(array('IPF_Admin_Dashboard_Controller', 'index')); + $success_url = $this->container['router']->reverse(array('IPF_Admin_Dashboard_Controller', 'index')); $auth_app = $this->authApp(); @@ -52,7 +52,7 @@ class IPF_Admin_User_Controller extends IPF_Admin_Base_Controller { $success_url = trim(\PFF\Arr::get($this->request->REQUEST, 'next', '')); if (!$success_url) - $success_url = $this->project->router->reverse(array('IPF_Admin_Dashboard_Controller', 'index')); + $success_url = $this->container['router']->reverse(array('IPF_Admin_Dashboard_Controller', 'index')); if (!$this->request->user->isAnonymous() && $this->request->user->is_superuser) { $user = $this->authApp()->findUser($this->params[1]); diff --git a/ipf/auth/app.php b/ipf/auth/app.php index 1ad4ca9..fdf113b 100644 --- a/ipf/auth/app.php +++ b/ipf/auth/app.php @@ -20,7 +20,7 @@ class IPF_Auth_App extends IPF_Application public function configure(Container $container, IPF_Settings $settings) { $this->container = $container; - $this->userModel = $settings->get('auth_user_model', 'IPF\\Auth\\User'); + $this->userModel = $container['settings']->get('auth_user_model', User::class); } function getTitle() diff --git a/ipf/auth/middleware.php b/ipf/auth/middleware.php index ee2d567..c924d09 100644 --- a/ipf/auth/middleware.php +++ b/ipf/auth/middleware.php @@ -2,6 +2,8 @@ namespace IPF\Auth; +use Doctrine\DBAL\Connection; + class Middleware extends \IPF_Middleware { const SessionKey = 'IPF_User_auth'; @@ -12,11 +14,11 @@ class Middleware extends \IPF_Middleware $user_id = \PFF\Arr::get($request->session->data, self::SessionKey, 0); if ($user_id > 0) { - $auth_app = $this->container['apps']['auth']; - $request->user = $auth_app->findUser($user_id); + $connection = $this->getConnection(); + $request->user = $this->getAuthApp()->findUser($user_id); if ($request->user && 43200 < time() - strtotime($request->user->last_login.' GMT')) { $request->user->last_login = gmdate('Y-m-d H:i:s'); - $request->user->save(); + $request->user->save($connection); } } @@ -31,4 +33,20 @@ class Middleware extends \IPF_Middleware $request->session->data[self::SessionKey] = $request->user->id; return $response; } + + /** + * @return \IPF_Auth_App + */ + private function getAuthApp() + { + return $this->container['apps']['auth']; + } + + /** + * @return Connection + */ + private function getConnection() + { + return $this->container['db']; + } } diff --git a/ipf/controller/base.php b/ipf/controller/base.php index 2b12d41..3818b06 100644 --- a/ipf/controller/base.php +++ b/ipf/controller/base.php @@ -4,16 +4,13 @@ use Pimple\Container; class IPF_Controller { - /** @var IPF_Project */ - protected $project; protected $request; protected $params; /** @var Container */ protected $container; - function __construct(IPF_Project $project, Container $container) + function __construct(Container $container) { - $this->project = $project; $this->container = $container; } diff --git a/ipf/middleware/common.php b/ipf/middleware/common.php index e61d1d1..33196ae 100644 --- a/ipf/middleware/common.php +++ b/ipf/middleware/common.php @@ -4,7 +4,7 @@ class IPF_Middleware_Common extends IPF_Middleware { function processRequest($request) { - if ($this->settings->get('append_slash', true)) { + if ($this->container['settings']->get('append_slash', true)) { $url = $request->absoluteUrl(); if (substr($url, -1) !== '/') { return new IPF_HTTP_Response_Redirect($url.'/'); @@ -13,4 +13,3 @@ class IPF_Middleware_Common extends IPF_Middleware return false; } } - diff --git a/ipf/middleware/dispatch.php b/ipf/middleware/dispatch.php index 01ff379..0d8e1ab 100644 --- a/ipf/middleware/dispatch.php +++ b/ipf/middleware/dispatch.php @@ -4,9 +4,7 @@ class IPF_Dispatch_Middleware extends IPF_Middleware { function processRequest($request) { - $router = $this->project->router; - - $m = $router->match($request); + $m = $this->getRouter()->match($request); if (!$m) { return new IPF_HTTP_Response_NotFound($request); } @@ -17,10 +15,18 @@ class IPF_Dispatch_Middleware extends IPF_Middleware try { $controllerClass = $route->controller(); /** @var IPF_Controller $controller */ - $controller = new $controllerClass($this->project, $this->container); + $controller = new $controllerClass($this->container); return $controller->process($route->action(), $request, $match); } catch (IPF_Router_Shortcut $e) { return $e->response($request); } } + + /** + * @return IPF_Router + */ + private function getRouter() + { + return $this->container['router']; + } } diff --git a/ipf/middleware/serve_static.php b/ipf/middleware/serve_static.php index b40e9dd..584b484 100644 --- a/ipf/middleware/serve_static.php +++ b/ipf/middleware/serve_static.php @@ -4,14 +4,14 @@ class IPF_Serve_Static_Middleware extends IPF_Middleware { function processRequest($request) { - $staticUrl = $this->settings->get('static_url'); + $staticUrl = $this->getStaticUrl(); if (!preg_match('#^'.preg_quote($staticUrl).'(.*)$#', $request->query, $matches)) return false; $query = $matches[1]; - foreach ($this->project->appList() as $app) { + foreach ($this->getAppList() as $app) { $static = $app->getPath() . $staticUrl . $query; if (is_file($static)) return new IPF_HTTP_Response_File($static, null, $this->mimetype($query)); @@ -41,5 +41,20 @@ class IPF_Serve_Static_Middleware extends IPF_Middleware default: return 'application/octet-stream'; } } -} + /** + * @return IPF_Application[] + */ + private function getAppList() + { + return $this->container['apps']; + } + + /** + * @return string + */ + private function getStaticUrl() + { + return $this->container['settings']->get('static_url'); + } +} diff --git a/ipf/project.php b/ipf/project.php index d66551c..7df013c 100644 --- a/ipf/project.php +++ b/ipf/project.php @@ -22,6 +22,7 @@ final class IPF_Project private function __construct() { $this->container = new Container(); + $this->container['settings'] = IPF::$settings; $apps = array(); foreach (IPF::get('applications') as $name) { @@ -49,9 +50,12 @@ final class IPF_Project return $this->apps; } + /** + * @return IPF_Middleware + */ private function chainMiddlewares() { - $middlewares = IPF::get('middlewares', array()); + $middlewares = $this->container['settings']->get('middlewares', array()); array_unshift($middlewares, 'IPF_Error_Middleware'); array_push($middlewares, 'IPF_Dispatch_Middleware'); diff --git a/ipf/session/app.php b/ipf/session/app.php index d94f596..efb9893 100644 --- a/ipf/session/app.php +++ b/ipf/session/app.php @@ -9,7 +9,7 @@ class IPF_Session_App extends IPF_Application { $this->backends = [ new CookieSessionBackend(), - new DBSessionBackend($container, $settings->get('secret_key')), + new DBSessionBackend($container, $container['settings']->get('secret_key')), ]; } diff --git a/ipf/session/middleware.php b/ipf/session/middleware.php index 19e765e..a4fd360 100644 --- a/ipf/session/middleware.php +++ b/ipf/session/middleware.php @@ -4,18 +4,31 @@ class IPF_Session_Middleware extends IPF_Middleware { function processRequest($request) { - /** @var IPF_Session_App $session_app */ - $session_app = $this->container['apps']['session']; - - $session_cookie = \PFF\Arr::get($request->COOKIE, $this->settings->get('session_cookie_id')); - $request->session = $session_app->getSession($session_cookie); + $session_cookie = \PFF\Arr::get($request->COOKIE, $this->getSessionCookieName()); + $request->session = $this->getSessionApp()->getSession($session_cookie); return false; } function processResponse($request, $response) { $session_cookie = $request->session->cookie(); - $response->cookies[$this->settings->get('session_cookie_id')] = $session_cookie; + $response->cookies[$this->getSessionCookieName()] = $session_cookie; return $response; } + + /** + * @return string + */ + private function getSessionCookieName() + { + return $this->container['settings']->get('session_cookie_id'); + } + + /** + * @return IPF_Session_App + */ + private function getSessionApp() + { + return $this->container['apps']['session']; + } }