]> git.andy128k.dev Git - ipf.git/commitdiff
Spread usage of Container
authorAndrey Kutejko <andy128k@gmail.com>
Fri, 15 Mar 2019 14:35:58 +0000 (15:35 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 10:50:05 +0000 (11:50 +0100)
13 files changed:
ipf.php
ipf/admin/controllers/base.php
ipf/admin/controllers/file_browser.php
ipf/admin/controllers/user.php
ipf/auth/app.php
ipf/auth/middleware.php
ipf/controller/base.php
ipf/middleware/common.php
ipf/middleware/dispatch.php
ipf/middleware/serve_static.php
ipf/project.php
ipf/session/app.php
ipf/session/middleware.php

diff --git a/ipf.php b/ipf.php
index 6b582bc53989f75dabbc4cd1d2726fa44815fa28..8c8c12873b9f54d7369e821e81af74a5e40ac8e4 100644 (file)
--- 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');
 
index 77efb17246f01d55d6fc6155197145e675555bc6..c6a29705f8a8add2a7d4165e3e379e50d7c5525a 100644 (file)
@@ -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;
         }
index 342509db226f02197dbb0e6ab5a91d579014a93b..7040da0fbafc96394527edce3fa943c022608f37 100644 (file)
@@ -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);
     }
 
index 4f3e8f919ea741f814f52ddd500739e61e040e80..4430c4d23566f96e438f1e452dee9c6c12129a72 100644 (file)
@@ -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]);
index 1ad4ca982d3d5092d829338dead7aa5caee11722..fdf113b0677cdc9c8fc1e58f58c2e12cace4a6a1 100644 (file)
@@ -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()
index ee2d567892900a270930a7d9ab4dc77a8d440dcb..c924d0912503992fd9d268910d1283418e6c3208 100644 (file)
@@ -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'];
+    }
 }
index 2b12d41fbb506b7d157ef031019499af641c2148..3818b068e907c98ea786ae3081774a4203c371e7 100644 (file)
@@ -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;
     }
 
index e61d1d1ab4d8a2d96b420791f08dc53d90f12969..33196aea0ee9840359db912c4681159299cc99e5 100644 (file)
@@ -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;
     }
 }
-
index 01ff379a01947bd31a593bd93fb9bb9495e859c4..0d8e1ab5af99fe88ade14ea004de1c0a4801ced7 100644 (file)
@@ -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'];
+    }
 }
index b40e9dd0ae4110c63738c12b6a3b0cacaa708f27..584b4842b19559baa9434441bc8544f8f4efcab9 100644 (file)
@@ -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');
+    }
+}
index d66551c1ca918a943872b8f038f57125ea65e288..7df013c92013c41cbd6f61200296f0b035b9ecd8 100644 (file)
@@ -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');
 
index d94f59672c505fe0987c02853d12edc80cbaf2ba..efb9893cf05118191992a34e0d2d125ac25b91f1 100644 (file)
@@ -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')),
         ];
     }
 
index 19e765e30dfc43e5e7e1a6ad46e8eb489012d3e1..a4fd3602bd87869d716f38615c711d20a70ac521 100644 (file)
@@ -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'];
+    }
 }