Overhauled some classes using DI
authorAlexander Ebert <ebert@woltlab.com>
Fri, 20 Nov 2015 16:50:00 +0000 (17:50 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 20 Nov 2015 16:50:00 +0000 (17:50 +0100)
wcfsetup/install/files/lib/system/WCF.class.php
wcfsetup/install/files/lib/system/WCFACP.class.php
wcfsetup/install/files/lib/system/language/LanguageFactory.class.php
wcfsetup/install/files/lib/system/request/LinkHandler.class.php
wcfsetup/install/files/lib/system/request/RequestHandler.class.php
wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php
wcfsetup/install/files/lib/system/session/SessionFactory.class.php
wcfsetup/install/files/lib/system/session/SessionHandler.class.php

index dd4209d835e6a7607ce9f7c4eb4bd5a8b830c266..17bbb79dbc9ab969180feec7acc72816aa680332 100644 (file)
@@ -325,7 +325,7 @@ class WCF {
         * Starts the session system.
         */
        protected function initSession() {
-               $factory = new SessionFactory();
+               $factory = self::$diContainer->get(SessionFactory::class);
                $factory->load();
                
                self::$sessionObj = self::$diContainer->get(SessionHandler::class);
@@ -346,7 +346,7 @@ class WCF {
                mb_language('uni');
                
                // get language
-               self::$languageObj = LanguageFactory::getInstance()->getUserLanguage(self::getSession()->getLanguageID());
+               self::$languageObj = self::$diContainer->get(LanguageFactory::class)->getUserLanguage(self::getSession()->getLanguageID());
        }
        
        /**
@@ -368,7 +368,6 @@ class WCF {
                        self::getSession()->setStyleID(intval($_REQUEST['styleID']));
                }
                
-               /** @var $styleHandler \wcf\system\style\StyleHandler */
                $styleHandler = self::$diContainer->get(StyleHandler::class);
                $styleHandler->changeStyle(self::getSession()->getStyleID());
        }
index 9513e839b605ca91eb201dcaa0dcdde2c6b4bd54..6eab8d5e535dac775286fdbbce7620fd3ddc4bce 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system;
+use DI\ContainerBuilder;
 use wcf\acp\form\MasterPasswordForm;
 use wcf\acp\form\MasterPasswordInitForm;
 use wcf\system\application\ApplicationHandler;
@@ -35,6 +36,9 @@ class WCFACP extends WCF {
                // define tmp directory
                if (!defined('TMP_DIR')) define('TMP_DIR', FileUtil::getTempFolder());
                
+               $builder = new ContainerBuilder();
+               self::$diContainer = $builder->build();
+               
                // start initialization
                $this->initDB();
                $this->loadOptions();
@@ -116,10 +120,10 @@ class WCFACP extends WCF {
         * @see \wcf\system\WCF::initSession()
         */
        protected function initSession() {
-               $factory = new ACPSessionFactory();
+               $factory = self::$diContainer->get(ACPSessionFactory::class);
                $factory->load();
                
-               self::$sessionObj = SessionHandler::getInstance();
+               self::$sessionObj = self::$diContainer->get(SessionHandler::class);
        }
        
        /**
index 619d54d3e1be757202f5fe5561698fcdb79858cc..cd1eda6277d114fd28ae95d9d67ca7de09b22417 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system\language;
+use wcf\data\language\category\LanguageCategory;
 use wcf\data\language\Language;
 use wcf\data\language\LanguageEditor;
 use wcf\system\cache\builder\LanguageCacheBuilder;
@@ -24,20 +25,36 @@ class LanguageFactory extends SingletonFactory {
         */
        protected $cache = null;
        
+       /**
+        * @var LanguageCacheBuilder
+        */
+       protected $languageCacheBuilder;
+       
        /**
         * initialized languages
-        * @var array<\wcf\data\language\Language>
+        * @var Language[]
         */
        protected $languages = array();
        
        /**
         * active template scripting compiler
-        * @var \wcf\system\template\TemplateScriptingCompiler
+        * @var TemplateScriptingCompiler
         */
        protected $scriptingCompiler = null;
        
        /**
-        * @see \wcf\system\SingletonFactory::init()
+        * LanguageFactory constructor.
+        * 
+        * @param       LanguageCacheBuilder    $languageCacheBuilder
+        */
+       public function __construct(LanguageCacheBuilder $languageCacheBuilder) {
+               $this->languageCacheBuilder = $languageCacheBuilder;
+               
+               parent::__construct();
+       }
+       
+       /**
+        * @see SingletonFactory::init()
         */
        protected function init() {
                $this->loadCache();
@@ -47,7 +64,7 @@ class LanguageFactory extends SingletonFactory {
         * Returns a Language object for the language with the given id.
         * 
         * @param       integer         $languageID
-        * @return      \wcf\data\language\Language
+        * @return      Language
         */
        public function getLanguage($languageID) {
                if (!isset($this->languages[$languageID])) {
@@ -65,7 +82,7 @@ class LanguageFactory extends SingletonFactory {
         * Gets the preferred language of the current user.
         * 
         * @param       integer         $languageID
-        * @return      \wcf\data\language\Language
+        * @return      Language
         */
        public function getUserLanguage($languageID = 0) {
                if ($languageID) {
@@ -82,7 +99,7 @@ class LanguageFactory extends SingletonFactory {
         * language exists.
         * 
         * @param       string          $languageCode
-        * @return      \wcf\data\language\Language
+        * @return      Language
         */
        public function getLanguageByCode($languageCode) {
                // called within WCFSetup
@@ -116,7 +133,7 @@ class LanguageFactory extends SingletonFactory {
         * Returns the language category with the given name.
         * 
         * @param       string          $categoryName
-        * @return      \wcf\data\language\category\LanguageCategory
+        * @return      LanguageCategory
         */
        public function getCategory($categoryName) {
                if (isset($this->cache['categories'][$categoryName])) {
@@ -130,7 +147,7 @@ class LanguageFactory extends SingletonFactory {
         * Returns language category by id.
         * 
         * @param       integer         $languageCategoryID
-        * @return      \wcf\data\language\category\LanguageCategory
+        * @return      LanguageCategory
         */
        public function getCategoryByID($languageCategoryID) {
                if (isset($this->cache['categoryIDs'][$languageCategoryID])) {
@@ -143,7 +160,7 @@ class LanguageFactory extends SingletonFactory {
        /**
         * Returns a list of available language categories.
         * 
-        * @return      array<\wcf\data\language\category\LanguageCategory>
+        * @return      LanguageCategory[]
         */
        public function getCategories() {
                return $this->cache['categories'];
@@ -200,7 +217,7 @@ class LanguageFactory extends SingletonFactory {
        /**
         * Returns the active scripting compiler object.
         * 
-        * @return      \wcf\system\template\TemplateScriptingCompiler
+        * @return      TemplateScriptingCompiler
         */
        public function getScriptingCompiler() {
                if ($this->scriptingCompiler === null) {
@@ -215,7 +232,7 @@ class LanguageFactory extends SingletonFactory {
         */
        protected function loadCache() {
                if (defined('WCF_N')) {
-                       $this->cache = LanguageCacheBuilder::getInstance()->getData();
+                       $this->cache = $this->languageCacheBuilder->getData();
                }
        }
        
@@ -223,7 +240,7 @@ class LanguageFactory extends SingletonFactory {
         * Clears languages cache.
         */
        public function clearCache() {
-               LanguageCacheBuilder::getInstance()->reset();
+               $this->languageCacheBuilder->reset();
        }
        
        /**
@@ -249,7 +266,7 @@ class LanguageFactory extends SingletonFactory {
        /**
         * Returns all available languages.
         * 
-        * @return      array<\wcf\data\language\Language>
+        * @return      Language[]
         */
        public function getLanguages() {
                return $this->cache['languages'];
@@ -258,7 +275,7 @@ class LanguageFactory extends SingletonFactory {
        /**
         * Returns all available content languages for given package.
         * 
-        * @return      array<\wcf\data\language\Language>
+        * @return      Language[]
         */
        public function getContentLanguages() {
                $availableLanguages = array();
index 585ead8002214f6b2314f616df8ddbf4c85e3955..dbdfef6f376b8547549440a320b3f4b454deb643 100644 (file)
@@ -24,6 +24,11 @@ class LinkHandler extends SingletonFactory {
         */
        protected $applicationHandler;
        
+       /**
+        * @var PageMenu
+        */
+       protected $pageMenu;
+       
        /**
         * @var RequestHandler
         */
@@ -59,8 +64,9 @@ class LinkHandler extends SingletonFactory {
         * @param       RequestHandler          $requestHandler
         * @param       RouteHandler            $routeHandler
         */
-       public function __construct(ApplicationHandler $applicationHandler, RequestHandler $requestHandler, RouteHandler $routeHandler) {
+       public function __construct(ApplicationHandler $applicationHandler, PageMenu $pageMenu, RequestHandler $requestHandler, RouteHandler $routeHandler) {
                $this->applicationHandler = $applicationHandler;
+               $this->pageMenu = $pageMenu;
                $this->requestHandler = $requestHandler;
                $this->routeHandler = $routeHandler;
                
@@ -157,7 +163,7 @@ class LinkHandler extends SingletonFactory {
                                $controller = 'Index';
                        }
                        else {
-                               return WCF::getDIContainer()->get(PageMenu::class)->getLandingPage()->getProcessor()->getLink();
+                               return $this->pageMenu->getLandingPage()->getProcessor()->getLink();
                        }
                }
                
index 137f16c171e5e0832e33df8ac9b1cef11d248697..0e880d784b4244af7f35c2fc268695aaeaa65821 100644 (file)
@@ -49,6 +49,11 @@ class RequestHandler extends SingletonFactory {
         */
        protected $isACPRequest = false;
        
+       /**
+        * @var PageMenu
+        */
+       protected $pageMenu;
+       
        /**
         * @var RouteHandler
         */
@@ -61,9 +66,10 @@ class RequestHandler extends SingletonFactory {
         * @param       ControllerMap           $controllerMap
         * @param       RouteHandler            $routeHandler
         */
-       public function __construct(ApplicationHandler $applicationHandler, ControllerMap $controllerMap, RouteHandler $routeHandler) {
+       public function __construct(ApplicationHandler $applicationHandler, ControllerMap $controllerMap, PageMenu $pageMenu, RouteHandler $routeHandler) {
                $this->applicationHandler = $applicationHandler;
                $this->controllerMap = $controllerMap;
+               $this->pageMenu = $pageMenu;
                $this->routeHandler = $routeHandler;
                
                parent::__construct();
@@ -267,7 +273,7 @@ class RequestHandler extends SingletonFactory {
                        return;
                }
                
-               $landingPage = WCF::getDIContainer()->get(PageMenu::class)->getLandingPage();
+               $landingPage = $this->pageMenu->getLandingPage();
                if ($landingPage === null) {
                        return;
                }
index 7371b22ffe5c2849dad758b6373a806fe5d48930..aa11ef2fbd9d0847c909473d24c28183447b01f5 100644 (file)
@@ -13,30 +13,51 @@ use wcf\system\event\EventHandler;
  * @category   Community Framework
  */
 class ACPSessionFactory {
+       /**
+        * @var EventHandler
+        */
+       protected $eventHandler;
+       
        /**
         * session editor class name
         * @var string
         */
        protected $sessionEditor = 'wcf\data\acp\session\ACPSessionEditor';
        
+       /**
+        * @var SessionHandler
+        */
+       protected $sessionHandler;
+       
+       /**
+        * ACPSessionFactory constructor.
+        * 
+        * @param       EventHandler    $eventHandler
+        * @param       SessionHandler  $sessionHandler
+        */
+       public function __construct(EventHandler $eventHandler, SessionHandler $sessionHandler) {
+               $this->eventHandler = $eventHandler;
+               $this->sessionHandler = $sessionHandler;
+       }
+       
        /**
         * Loads the object of the active session.
         */
        public function load() {
                // get session
                $sessionID = $this->readSessionID();
-               SessionHandler::getInstance()->load($this->sessionEditor, $sessionID);
+               $this->sessionHandler->load($this->sessionEditor, $sessionID);
                
                // call beforeInit event
                if (!defined('NO_IMPORTS')) {
-                       EventHandler::getInstance()->fireAction($this, 'beforeInit');
+                       $this->eventHandler->fireAction($this, 'beforeInit');
                }
                
                $this->init();
                
                // call afterInit event
                if (!defined('NO_IMPORTS')) {
-                       EventHandler::getInstance()->fireAction($this, 'afterInit');
+                       $this->eventHandler->fireAction($this, 'afterInit');
                }
        }
        
@@ -53,7 +74,7 @@ class ACPSessionFactory {
         * Initializes the session system.
         */
        protected function init() {
-               SessionHandler::getInstance()->initSession();
+               $this->sessionHandler->initSession();
        }
        
        /**
index 493514677eaffd9bf3ffe1697dcf4d48f1bd33aa..943695bbb7ad881bcea73a84a47cc9151ebd9be7 100644 (file)
@@ -23,7 +23,7 @@ class SessionFactory extends ACPSessionFactory {
         */
        public function hasValidCookie() {
                if (isset($_COOKIE[COOKIE_PREFIX.'cookieHash'])) {
-                       if ($_COOKIE[COOKIE_PREFIX.'cookieHash'] == SessionHandler::getInstance()->sessionID) {
+                       if ($_COOKIE[COOKIE_PREFIX.'cookieHash'] == $this->sessionHandler->sessionID) {
                                return true;
                        }
                }
@@ -49,11 +49,11 @@ class SessionFactory extends ACPSessionFactory {
        protected function init() {
                if (!$this->hasValidCookie()) {
                        // cookie support will be enabled upon next request
-                       HeaderUtil::setCookie('cookieHash', SessionHandler::getInstance()->sessionID);
+                       HeaderUtil::setCookie('cookieHash', $this->sessionHandler->sessionID);
                }
                
                // enable cookie support
-               SessionHandler::getInstance()->enableCookies();
+               $this->sessionHandler->enableCookies();
                
                parent::init();
        }
index fd0f7828f23bcd3caef586dec6e3d611d03e3fa0..3440a6366a8809c9a815d9cfe2ca3929ba083d43 100644 (file)
@@ -237,6 +237,7 @@ class SessionHandler extends SingletonFactory {
                $oldSessionID = $this->session->sessionID;
                $newSessionID = StringUtil::getRandomID();
                
+               /** @var \wcf\data\DatabaseObjectEditor $sessionEditor */
                $sessionEditor = new $this->sessionEditorClassName($this->session);
                $sessionEditor->update(array(
                        'sessionID' => $newSessionID
@@ -365,6 +366,7 @@ class SessionHandler extends SingletonFactory {
         * Returns the value of a session variable.
         * 
         * @param       string          $key
+        * @return      mixed
         */
        public function getVar($key) {
                if (isset($this->variables[$key])) {
@@ -594,6 +596,9 @@ class SessionHandler extends SingletonFactory {
        /**
         * Checks if the active user has the given permissions and throws a
         * PermissionDeniedException if that isn't the case.
+        * 
+        * @param       array<string>   $permissions    list of permissions where each one must pass
+        * @throws      PermissionDeniedException
         */
        public function checkPermissions(array $permissions) {
                foreach ($permissions as $permission) {
@@ -675,7 +680,7 @@ class SessionHandler extends SingletonFactory {
         * Stores a new user object in this session, e.g. a user was guest because not
         * logged in, after the login his old session is used to store his full data.
         * 
-        * @param       \wcf\data\userUser              $user
+        * @param       \wcf\data\user\User             $user
         * @param       boolean                         $hideSession    if true, database won't be updated
         */
        public function changeUser(User $user, $hideSession = false) {
@@ -696,6 +701,7 @@ class SessionHandler extends SingletonFactory {
                        
                        if (!$hideSession) {
                                // update session
+                               /** @var \wcf\data\DatabaseObjectEditor $sessionEditor */
                                $sessionEditor = new $this->sessionEditorClassName($this->session);
                                
                                $this->register('__changeSessionID', true);
@@ -755,7 +761,7 @@ class SessionHandler extends SingletonFactory {
         * @param       \wcf\data\user\User     $user
         */
        protected function changeUserVirtual(User $user) {
-               $sessionTable = call_user_func(array($this->sessionClassName, 'getDatabaseTableName'));
+               /** @var \wcf\data\DatabaseObjectEditor $sessionEditor */
                
                switch ($user->userID) {
                        //
@@ -880,6 +886,7 @@ class SessionHandler extends SingletonFactory {
                }
                
                // update session
+               /** @var \wcf\data\DatabaseObjectEditor $sessionEditor */
                $sessionEditor = new $this->sessionEditorClassName($this->session);
                $sessionEditor->update($data);
                
@@ -896,6 +903,7 @@ class SessionHandler extends SingletonFactory {
                $this->disableUpdate();
                
                // update last activity time
+               /** @var \wcf\data\DatabaseObjectEditor $sessionEditor */
                $sessionEditor = new $this->sessionEditorClassName($this->session);
                $sessionEditor->update(array(
                        'lastActivityTime' => TIME_NOW
@@ -927,6 +935,7 @@ class SessionHandler extends SingletonFactory {
                $this->changeUser(new User(null));
                
                // 2nd: Actually remove session
+               /** @var \wcf\data\DatabaseObjectEditor $sessionEditor */
                $sessionEditor = new $this->sessionEditorClassName($this->session);
                $sessionEditor->delete();
                
@@ -979,12 +988,12 @@ class SessionHandler extends SingletonFactory {
         */
        public static function resetSessions(array $userIDs = array()) {
                if (!empty($userIDs)) {
-                       UserStorageHandler::getInstance()->reset($userIDs, 'groupIDs', 1);
-                       UserStorageHandler::getInstance()->reset($userIDs, 'languageIDs', 1);
+                       UserStorageHandler::getInstance()->reset($userIDs, 'groupIDs');
+                       UserStorageHandler::getInstance()->reset($userIDs, 'languageIDs');
                }
                else {
-                       UserStorageHandler::getInstance()->resetAll('groupIDs', 1);
-                       UserStorageHandler::getInstance()->resetAll('languageIDs', 1);
+                       UserStorageHandler::getInstance()->resetAll('groupIDs');
+                       UserStorageHandler::getInstance()->resetAll('languageIDs');
                }
        }