/** * Store result in singleton to prevent multiple db requests with multiple calls * * @param bool $fromSingleton * * @return static */ public static function getCurrentUser($fromSingleton = true) { if (!$fromSingleton) { return static::findOne(Yii::$app->user->id); } $user = Singleton::getData('__currentUser'); if (!$user) { $user = static::findOne(Yii::$app->user->id); Singleton::setData('__currentUser', $user); } return $user; }
/** * Parses the given request and returns the corresponding route and parameters. * @param UrlManager $manager the URL manager * @param Request $request the request component * @return array|boolean the parsing result. The route and the parameters are returned as an array. * If false, it means this rule cannot be used to parse this path info. */ public function parseRequest($manager, $request) { // If it's base url - call main page if ($request->getPathInfo() === '') { $mainPage = $this->getMainPage(); if ($mainPage !== false) { Singleton::setData('content_template_id', $mainPage['content_template_id']); if ($mainPage['type'] == ContentPage::TYPE_TEXT) { return ['/content/default/view', ['slug' => $mainPage['slug']]]; } elseif ($mainPage['type'] == ContentPage::TYPE_INTERNAL_LINK) { return ['/' . ltrim($mainPage['slug'], '/'), []]; } } } $path = rtrim($request->getPathInfo(), '/'); $parts = explode('/', $path); $languagePart = null; // Multilingual support - remove language from parts if (isset(Yii::$app->params['mlConfig']['languages'])) { if (array_key_exists($parts[0], Yii::$app->params['mlConfig']['languages'])) { $languagePart = array_shift($parts); } } $slug = count($parts) == 1 && $parts[0] != '' ? $parts[0] : end($parts); if (isset($this->getAllPages()[$slug])) { if (isset($this->getAllPages()[$slug])) { $page = $this->getAllPages()[$slug]; Singleton::setData('content_template_id', $page['content_template_id']); return ['content/default/view', ['slug' => $slug, '_language' => $languagePart]]; } } elseif (count($parts) > 1) { $slug = implode('/', $parts); if (isset($this->getAllPages()[$slug])) { $page = $this->getAllPages()[$slug]; Singleton::setData('content_template_id', $page['content_template_id']); } } return false; // this rule does not apply }
/** * @return array */ private function _getI18NAttributes() { $singletonKey = '_mlAttributes_' . get_class($this); $mlAttributes = Singleton::getData($singletonKey); if ($mlAttributes === false) { $mlAttributes = []; $languages = Yii::$app->params['mlConfig']['languages']; // unset($languages[Yii::$app->params['mlConfig']['default_language']]); foreach ($languages as $languageCode => $languageName) { foreach ($this->_i18n_attributes as $attribute) { $mlAttributes[] = $attribute . '_' . $languageCode; } } Singleton::setData($singletonKey, $mlAttributes); } return $mlAttributes; }
/** * @return array */ public function mlGetAttributes() { $singletonKey = '_mlAttributes_' . get_class($this->owner); $mlAttributes = Singleton::getData($singletonKey); if ($mlAttributes === false) { $mlAttributes = []; $languages = $this->mlConfig['languages']; unset($languages[$this->mlConfig['default_language']]); foreach ($languages as $languageCode => $languageName) { foreach ($this->mlConfig['attributes'] as $attribute) { $mlAttributes[] = $attribute . '_' . $languageCode; } } Singleton::setData($singletonKey, $mlAttributes); } return $mlAttributes; }