Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // for each module could be created folder "configs" with definitions for DI Container
- // It is hask for encapsulating DI container at least to the module.
- /**
- * @copyright © Iterios
- * YII2
- * Date: 11.06.18
- * Time: 17:32
- */
- namespace app\common\base;
- use yii\di\Container;
- abstract class Module extends \yii\base\Module
- {
- protected $consoleNamespace = '';
- /**
- * @var Container
- */
- public $container;
- /**
- * @throws \ReflectionException
- */
- public function init()
- {
- parent::init();
- if ($this->consoleNamespace !== '' && \Yii::$app instanceof \yii\console\Application) {
- $this->controllerNamespace = $this->consoleNamespace;
- }
- $this->setContainer(\Yii::$container);
- $this->configure();
- }
- /**
- * @param Container $container
- */
- public function setContainer(Container $container)
- {
- $this->container = $container;
- }
- /**
- * @return Container
- */
- public function getContainer() : Container
- {
- return $this->container;
- }
- /**
- * @param array $config
- * @throws \ReflectionException
- */
- protected function configure($config = [])
- {
- $config = (empty($config)) ? $this->getConfig() : $config;
- if(empty($config)) {
- return;
- }
- if(isset($config['module'])) {
- \Yii::configure($this,$config['module']);
- }
- if(isset($config['definitions']) && is_array($config['definitions']) && !empty($config['definitions'])) {
- $definitions = $config['definitions'];
- $this->container->setDefinitions($definitions);
- }
- }
- /**
- * @return mixed|null
- * @throws \ReflectionException
- */
- protected function getConfig()
- {
- $class = new \ReflectionClass($this);
- if(file_exists(dirname($class->getFileName()) . '/configs/config.php')) {
- return require_once dirname($class->getFileName()) . '/configs/config.php';
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement