From: Alexander Ebert Date: Fri, 20 Nov 2015 16:50:00 +0000 (+0100) Subject: Overhauled some classes using DI X-Git-Tag: 3.0.0_Beta_1~2030^2~239 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=a935240bf6d19f34a5f511817adcaef9a18a7fc3;p=GitHub%2FWoltLab%2FWCF.git Overhauled some classes using DI --- diff --git a/wcfsetup/install/files/lib/system/WCF.class.php b/wcfsetup/install/files/lib/system/WCF.class.php index dd4209d835..17bbb79dbc 100644 --- a/wcfsetup/install/files/lib/system/WCF.class.php +++ b/wcfsetup/install/files/lib/system/WCF.class.php @@ -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()); } diff --git a/wcfsetup/install/files/lib/system/WCFACP.class.php b/wcfsetup/install/files/lib/system/WCFACP.class.php index 9513e839b6..6eab8d5e53 100644 --- a/wcfsetup/install/files/lib/system/WCFACP.class.php +++ b/wcfsetup/install/files/lib/system/WCFACP.class.php @@ -1,5 +1,6 @@ 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); } /** diff --git a/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php b/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php index 619d54d3e1..cd1eda6277 100644 --- a/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php +++ b/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php @@ -1,5 +1,6 @@ + * @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(); diff --git a/wcfsetup/install/files/lib/system/request/LinkHandler.class.php b/wcfsetup/install/files/lib/system/request/LinkHandler.class.php index 585ead8002..dbdfef6f37 100644 --- a/wcfsetup/install/files/lib/system/request/LinkHandler.class.php +++ b/wcfsetup/install/files/lib/system/request/LinkHandler.class.php @@ -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(); } } diff --git a/wcfsetup/install/files/lib/system/request/RequestHandler.class.php b/wcfsetup/install/files/lib/system/request/RequestHandler.class.php index 137f16c171..0e880d784b 100644 --- a/wcfsetup/install/files/lib/system/request/RequestHandler.class.php +++ b/wcfsetup/install/files/lib/system/request/RequestHandler.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php b/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php index 7371b22ffe..aa11ef2fbd 100644 --- a/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php +++ b/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php @@ -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(); } /** diff --git a/wcfsetup/install/files/lib/system/session/SessionFactory.class.php b/wcfsetup/install/files/lib/system/session/SessionFactory.class.php index 493514677e..943695bbb7 100644 --- a/wcfsetup/install/files/lib/system/session/SessionFactory.class.php +++ b/wcfsetup/install/files/lib/system/session/SessionFactory.class.php @@ -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(); } diff --git a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php index fd0f7828f2..3440a6366a 100644 --- a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php +++ b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php @@ -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 $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'); } }