<commit_before><?php

/**
 * @copyright Copyright &copy; <NAME>, communityii, 2014 - 2015
 * @package communityii/yii2-user
 * @version 1.0.0
 *
 * @author derekisbusy https://github.com/derekisbusy
 * @author kartik-v https://github.com/kartik-v
 */

namespace comyii\common\base;

use Closure;
use kartik\base\Module as BaseModule;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\UnknownPropertyException;
use yii\di\Container;

/**
 * Class Module the base module used in communityii packages.
 * 
 * @package comyii\user\components
 */
class Module extends BaseModule
{

    /**
     * Use magic settings for __set() method. Uses `static` property to
     * store properties that do not exist in the module.
     */
    const MAGIC_PROPERTIES = false;

    /**
     * @var array|null list of components to initial on app bootstrap.
     */
    public $bootstrap;

    /**
     * @var array|null list of components to initialize before module controller.
     * These components are only loaded when the controller is under the user module.
     */
    public $init;

    /**
     * @var array default component definitions indexed by their IDs
     */
    protected $_defaults = [];

    /**
     * @var array|null settings defined in the configs. These settings are merged with
     * defaults.
     */
    protected $_config;

    /**
     * @var array|null custom settings defined in the config. These settings are used if
     * the settings do not exist in the module class. For example in module config:
     * 
     * ```php
     *      [
     *          'components' => [],
     *          'static' => [
     *              'myCustomVar' => 'test',
     *          ],
     *      ]
     * ```
     * 
     * The custom property can now be accessed as `Yii::$app->getModule('user')->myCustomVar`
     * 
     * This is similar to components except that static properties are set by reference and
     * don't have to implement a class or exist in the module.
     */
    protected $_static;

    /**
     * @var Container the dependency injection (DI) container used by [[createObject()]].
     * You may use [[Container::set()]] to set up the needed dependencies of classes and
     * their initial property values.
     * @see createObject()
     * @see Container
     */
    public static $container;

    /**
     * @var array|null Components listed here will be passed to `Yii::$container` and use
     * the main applications dependency injection (DI) container.
     *  
     */
    public $yii;
    /**
     * Initialize the module
     */
    public function init()
    {
        static::$container = new Container();
        $this->_msgCat = 'user';
        
        parent::init();
//        $this->setConfig();
    }

    /**
     * This method is overridden to support accessing components and custom settings like reading properties.
     * 
     * @param string $name component, property or static name
     * @return mixed
     */
    public function __get($name)
    {
        if (($has = $this->getProperty($name))) {
            return $has;
        }
        if (($config = $this->getConfig($name)) && !empty($config)) {
            var_dump($config);
            $this->set($name, $config);
            return $this->get($name);
        }
        return Component::__get($name);
    }
    
    protected function getConfig($name)
    {
        $config = [];
        // default definitions
        if (isset($this->_defaults[$name])) {
            $config = $this->_defaults[$name];
        }
        // config components
        if (isset($this->_config[$name])) {
            $config = array_replace_recursive($config, $this->_config[$name]);
        }
        return $config;
    }
    
    public function getProperty($name)
    {
        if (property_exists($this, $name)) {
            return $this->{$name};
        }
        if ($this->has($name, true)) {
            return $this->get($name);
        }
        if ($this->hasStatic($name)) {
            return $this->getStatic($name);
        }
    }

    /**
     * Sets the value of a component property.
     * This method will check in the following order and act accordingly:
     *
     *  - a property defined by a setter: set the property value
     *  - an event in the format of "on xyz": attach the handler to the event "xyz"
     *  - a behavior in the format of "as xyz": attach the behavior named as "xyz"
     *  - a property of a behavior: set the behavior property value
     *  - a element defined in `_static` property array
     *  - if `static::MAGIC_PROPERTIES` is `true`, add item to `_static` property array
     * 
     * @param type $name
     * @param type $value
     * @throws UnknownPropertyException
     */
    public function __set($name, $value)
    {
        try {
            parent::__set($name, $value);
        } catch (UnknownPropertyException $e) {
            if (isset($this->_static[$name]) || static::MAGIC_PROPERTIES) {
                if (static::MAGIC_PROPERTIES) {
                    Yii::trace('magic property set (' . $name .'=' .$value .')', __CLASS__);
                }
                $this->_static[$name] = $value;
            } else {
                throw $e;
            }
        }
    }

    /**
     * Returns the component instance with the specified ID.
     *
     * @param string $id component ID (e.g. `db`).
     * @param boolean $throwException whether to throw an exception if `$id` is not registered with the locator before.
     * @return object|null the component of the specified ID. If `$throwException` is false and `$id`
     * is not registered before, null will be returned.
     * @throws InvalidConfigException if `$id` refers to a nonexistent component ID
     * @see has()
     * @see set()
     */
    public function get($id, $throwException = true)
    {
        if (isset($this->_components[$id])) {
            return $this->_components[$id];
        }

        if (isset($this->_definitions[$id])) {
            $definition = $this->_definitions[$id];
            if (is_object($definition) && !$definition instanceof Closure) {
                return $this->_components[$id] = $definition;
            } else {
                if (isset($this->yii[$id])) {
                    return $this->_components[$id] = Yii::$app->get($id);
                }
                return $this->_components[$id] = self::createObject($definition);
            }
        } elseif ($throwException) {
            throw new InvalidConfigException("Unknown component ID: $id");
        } else {
            return null;
        }
    }

    public function hasStatic($name)
    {
        return isset($this->_static[$name]);
    }

    public function getStatic($name)
    {
        return isset($this->_static[$name]) ? $this->_static[$name] : null;
    }

    public function setStatic($value)
    {<commit_msg>Fixed __get method<commit_after><?php

/**
 * @copyright Copyright &copy; <NAME>, communityii, 2014 - 2015
 * @package communityii/yii2-user
 * @version 1.0.0
 *
 * @author derekisbusy https://github.com/derekisbusy
 * @author kartik-v https://github.com/kartik-v
 */

namespace comyii\common\base;

use Closure;
use kartik\base\Module as BaseModule;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\UnknownPropertyException;
use yii\di\Container;

/**
 * Class Module the base module used in communityii packages.
 * 
 * @package comyii\user\components
 */
class Module extends BaseModule
{

    /**
     * Use magic settings for __set() method. Uses `static` property to
     * store properties that do not exist in the module.
     */
    const MAGIC_PROPERTIES = false;

    /**
     * @var array|null list of components to initial on app bootstrap.
     */
    public $bootstrap;

    /**
     * @var array|null list of components to initialize before module controller.
     * These components are only loaded when the controller is under the user module.
     */
    public $init;

    /**
     * @var array default component definitions indexed by their IDs
     */
    protected $_defaults = [];

    /**
     * @var array|null settings defined in the configs. These settings are merged with
     * defaults.
     */
    protected $_config;

    /**
     * @var array|null custom settings defined in the config. These settings are used if
     * the settings do not exist in the module class. For example in module config:
     * 
     * ```php
     *      [
     *          'components' => [],
     *          'static' => [
     *              'myCustomVar' => 'test',
     *          ],
     *      ]
     * ```
     * 
     * The custom property can now be accessed as `Yii::$app->getModule('user')->myCustomVar`
     * 
     * This is similar to components except that static properties are set by reference and
     * don't have to implement a class or exist in the module.
     */
    protected $_static;

    /**
     * @var Container the dependency injection (DI) container used by [[createObject()]].
     * You may use [[Container::set()]] to set up the needed dependencies of classes and
     * their initial property values.
     * @see createObject()
     * @see Container
     */
    public static $container;

    /**
     * @var array|null Components listed here will be passed to `Yii::$container` and use
     * the main applications dependency injection (DI) container.
     *  
     */
    public $yii;
    /**
     * Initialize the module
     */
    public function init()
    {
        static::$container = new Container();
        $this->_msgCat = 'user';
        
        parent::init();
//        $this->setConfig();
    }

    /**
     * This method is overridden to support accessing components and custom settings like reading properties.
     * 
     * @param string $name component, property or static name
     * @return mixed
     */
    public function __get($name)
    {
        if (($has = $this->getProperty($name))) {
            return $has;
        }
        
        if (($config = $this->getConfig($name)) && !empty($config)) {
            $this->set($name, $config);
            return $this->get($name);
        }
        return parent::__get($name);
    }
    
    protected function getConfig($name)
    {
        $config = [];
        // default definitions
        if (isset($this->_defaults[$name])) {
            $config = $this->_defaults[$name];
        }
        // config components
        if (isset($this->_config[$name])) {
            $config = array_replace_recursive($config, $this->_config[$name]);
        }
        return $config;
    }
    
    public function getProperty($name)
    {
        if (property_exists($this, $name)) {
            return $this->{$name};
        }
        if ($this->has($name, true)) {
            return $this->get($name);
        }
        if ($this->hasStatic($name)) {
            return $this->getStatic($name);
        }
    }

    /**
     * Sets the value of a component property.
     * This method will check in the following order and act accordingly:
     *
     *  - a property defined by a setter: set the property value
     *  - an event in the format of "on xyz": attach the handler to the event "xyz"
     *  - a behavior in the format of "as xyz": attach the behavior named as "xyz"
     *  - a property of a behavior: set the behavior property value
     *  - a element defined in `_static` property array
     *  - if `static::MAGIC_PROPERTIES` is `true`, add item to `_static` property array
     * 
     * @param type $name
     * @param type $value
     * @throws UnknownPropertyException
     */
    public function __set($name, $value)
    {
        try {
            parent::__set($name, $value);
        } catch (UnknownPropertyException $e) {
            if (isset($this->_static[$name]) || static::MAGIC_PROPERTIES) {
                if (static::MAGIC_PROPERTIES) {
                    Yii::trace('magic property set (' . $name .'=' .$value .')', __CLASS__);
                }
                $this->_static[$name] = $value;
            } else {
                throw $e;
            }
        }
    }

    /**
     * Returns the component instance with the specified ID.
     *
     * @param string $id component ID (e.g. `db`).
     * @param boolean $throwException whether to throw an exception if `$id` is not registered with the locator before.
     * @return object|null the component of the specified ID. If `$throwException` is false and `$id`
     * is not registered before, null will be returned.
     * @throws InvalidConfigException if `$id` refers to a nonexistent component ID
     * @see has()
     * @see set()
     */
    public function get($id, $throwException = true)
    {
        if (isset($this->_components[$id])) {
            return $this->_components[$id];
        }
        if (isset($this->_definitions[$id])) {
            $definition = $this->_definitions[$id];
            if (is_object($definition) && !$definition instanceof Closure) {
                return $this->_components[$id] = $definition;
            } else {
                if (isset($this->yii[$id])) {
                    return $this->_components[$id] = Yii::$app->get($id);
                }
                return $this->_components[$id] = self::createObject($definition);
            }
        } elseif ($throwException) {
            throw new InvalidConfigException("Unknown component ID: $id");
        } else {
            return null;
        }
    }

    public function hasStatic($name)
    {
        return isset($this->_static[$name]);
    }

    public function getStatic($name)
    {
        return isset($this->_static[$name]) ? $this->_static[$name] : null;
    }

    public function setStatic($value)
    {
{'fraction_non_alphanumeric': 0.12334227117907094, 'fraction_numerical': 0.00173925646785999, 'mean_word_length': 2.0721282279608193, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 73, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7437294', 'n_tokens_mistral': 3843, 'n_tokens_neox': 3757, 'n_words': 1187}