From 4d655cbe35078709dbe37a5c92f206a540a8fe1f Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Fri, 28 Jul 2023 12:53:31 +0300 Subject: [PATCH] create `MatchesWebApplicationNotation` --- src/console/ConsoleApplication.php | 1 + src/console/MatchesWebApplicationNotation.php | 216 ++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 src/console/MatchesWebApplicationNotation.php diff --git a/src/console/ConsoleApplication.php b/src/console/ConsoleApplication.php index 984bcbd..5c6e659 100644 --- a/src/console/ConsoleApplication.php +++ b/src/console/ConsoleApplication.php @@ -14,6 +14,7 @@ class ConsoleApplication extends \CConsoleApplication { use ResolvesComponentViaDI; use ResolvesCommandRunnerViaDI; + use MatchesWebApplicationNotation; /** * @var string|null namespace that should be used when loading commands. diff --git a/src/console/MatchesWebApplicationNotation.php b/src/console/MatchesWebApplicationNotation.php new file mode 100644 index 0000000..e70be31 --- /dev/null +++ b/src/console/MatchesWebApplicationNotation.php @@ -0,0 +1,216 @@ + + * @since 1.0 + */ +trait MatchesWebApplicationNotation +{ + /** + * @var \CTheme|string current theme. + */ + private $_theme; + /** + * @var string view path. + */ + private $_viewPath; + /** + * @var string system view path. + */ + private $_systemViewPath; + /** + * @var string layout view path. + */ + private $_layoutPath; + + /** + * @return \IAuthManager the authorization manager component + */ + public function getAuthManager() + { + return $this->getComponent('authManager'); + } + + /** + * @return \CAssetManager the asset manager component + */ + public function getAssetManager() + { + return $this->getComponent('assetManager'); + } + + /** + * @return \CHttpSession the session component + */ + public function getSession() + { + return $this->getComponent('session'); + } + + /** + * @return \CWebUser the user session information + */ + public function getUser() + { + return $this->getComponent('user'); + } + + /** + * Returns the view renderer. + * If this component is registered and enabled, the default view rendering logic defined + * in {@see \CBaseController} will be replaced by this renderer. + * + * @return \IViewRenderer the view renderer. + */ + public function getViewRenderer() + { + return $this->getComponent('viewRenderer'); + } + + /** + * Returns the client script manager. + * + * @return \CClientScript the client script manager + */ + public function getClientScript() + { + return $this->getComponent('clientScript'); + } + + /** + * Returns the widget factory. + * + * @return \IWidgetFactory the widget factory + */ + public function getWidgetFactory() + { + return $this->getComponent('widgetFactory'); + } + + /** + * @return \CThemeManager the theme manager. + */ + public function getThemeManager() + { + return $this->getComponent('themeManager'); + } + + /** + * @return \CTheme the theme used currently. Null if no theme is being used. + */ + public function getTheme() + { + if (is_string($this->_theme)) { + $this->_theme = $this->getThemeManager()->getTheme($this->_theme); + } + + return $this->_theme; + } + + /** + * @param string $value the theme name + */ + public function setTheme($value) + { + $this->_theme = $value; + } + + /** + * @return string the root directory of view files. Defaults to 'protected/views'. + */ + public function getViewPath() + { + if ($this->_viewPath !== null) { + return $this->_viewPath; + } + + return $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views'; + } + + /** + * @param string $path the root directory of view files. + * @throws \CException if the directory does not exist. + */ + public function setViewPath($path) + { + if (($this->_viewPath = realpath($path)) === false || !is_dir($this->_viewPath)) { + throw new CException( + Yii::t('yii', 'The view path "{path}" is not a valid directory.', ['{path}' => $path]) + ); + } + } + + /** + * @return string the root directory of system view files. Defaults to 'protected/views/system'. + */ + public function getSystemViewPath() + { + if ($this->_systemViewPath !== null) { + return $this->_systemViewPath; + } + + return $this->_systemViewPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'system'; + } + + /** + * @param string $path the root directory of system view files. + * @throws CException if the directory does not exist. + */ + public function setSystemViewPath($path) + { + if (($this->_systemViewPath = realpath($path)) === false || !is_dir($this->_systemViewPath)) { + throw new CException( + Yii::t('yii', 'The system view path "{path}" is not a valid directory.', ['{path}' => $path]) + ); + } + } + + /** + * @return string the root directory of layout files. Defaults to 'protected/views/layouts'. + */ + public function getLayoutPath() + { + if ($this->_layoutPath !== null) { + return $this->_layoutPath; + } + + return $this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts'; + } + + /** + * @param string $path the root directory of layout files. + * @throws CException if the directory does not exist. + */ + public function setLayoutPath($path) + { + if (($this->_layoutPath = realpath($path)) === false || !is_dir($this->_layoutPath)) { + throw new CException( + Yii::t('yii', 'The layout path "{path}" is not a valid directory.', ['{path}' => $path]) + ); + } + } +} \ No newline at end of file