<commit_before><?php
namespace goodizer\helpers;

use yii\db\ActiveRecord;
use yii\db\Connection;

/**
 * Created by PhpStorm.
 * User: Администратор
 * Date: 04.11.2015
 * Time: 9:37
 */
class DbSyncHelper
{
    /**
     * @var Connection
     */
    private $db;

    /**
     * @var string[]
     */
    public $nameSpaces;

    /**
     * @var string[]
     */
    public $tableNames;

    /**
     * Important - modelClass must have a public method "attributeTypes"
     * Example:
     *
     * public function attributeTypes()
     * {
     *      return [
     *          'id' => Schema::TYPE_PK,
     *          'owner_id' => Schema::TYPE_INTEGER,
     *          'name' => Schema::TYPE_STRING,
     *          'description' => Schema::TYPE_TEXT,
     *          'status' => Schema::TYPE_SMALLINT,
     *          'updated_at' => Schema::TYPE_TIMESTAMP,
     *          'created_at' => Schema::TYPE_DATETIME,
     *      ];
     * }
     *
     * Example of use:
     *
     * (new DbSync([
     *     'common\models\', // if namespace equivalent to path
     *     'Path to directory' => 'someName\models\',
     * ]))->run();
     *
     *
     * @param array $nameSpaces
     */
    public function __construct(array $nameSpaces)
    {
        foreach ($nameSpaces as $key => $nameSpace) {
            $this->nameSpaces[$key] = trim($nameSpace, '\\') . '\\';
        }

        $this->db = \Yii::$app->getDb();
        $this->tableNames = $this->db->getSchema()->getTableNames();
    }

    public function run()
    {
        $command = $this->db->createCommand();
        $changed = false;

        foreach ($this->nameSpaces as $key => $nameSpace) {
            if(is_integer($key)) {
                $alias = '@' . str_replace('\\', '/', $nameSpace);
                $path = \Yii::getAlias($alias);
            } else {
                $path = $key;
            }

            if(!is_dir($path)) {
                echo 'Directory not exist' . PHP_EOL;
                echo 'Path - "' . $path . '"' . PHP_EOL;
                echo 'Namespace - "' . $nameSpace . '"' . PHP_EOL . PHP_EOL;
                break;
            }

            foreach (glob($path . '*.php') as $file) {
                $info = pathinfo($file);
                $modelCls = $nameSpace . $info['filename'];

                /**
                 * @var $model ActiveRecord
                 */
                $model = new $modelCls();<commit_msg>Change access type of $db property to public;<commit_after><?php
namespace goodizer\helpers;

use yii\db\ActiveRecord;
use yii\db\Connection;

/**
 * Created by PhpStorm.
 * User: Администратор
 * Date: 04.11.2015
 * Time: 9:37
 */
class DbSyncHelper
{
    /**
     * @var Connection
     */
    public $db;

    /**
     * @var string[]
     */
    public $nameSpaces;

    /**
     * @var string[]
     */
    public $tableNames;

    /**
     * Important - modelClass must have a public method "attributeTypes"
     * Example:
     *
     * public function attributeTypes()
     * {
     *      return [
     *          'id' => Schema::TYPE_PK,
     *          'owner_id' => Schema::TYPE_INTEGER,
     *          'name' => Schema::TYPE_STRING,
     *          'description' => Schema::TYPE_TEXT,
     *          'status' => Schema::TYPE_SMALLINT,
     *          'updated_at' => Schema::TYPE_TIMESTAMP,
     *          'created_at' => Schema::TYPE_DATETIME,
     *      ];
     * }
     *
     * Example of use:
     *
     * (new DbSync([
     *     'common\models\', // if namespace equivalent to path
     *     'Path to directory' => 'someName\models\',
     * ]))->run();
     *
     *
     * @param array $nameSpaces
     */
    public function __construct(array $nameSpaces)
    {
        foreach ($nameSpaces as $key => $nameSpace) {
            $this->nameSpaces[$key] = trim($nameSpace, '\\') . '\\';
        }

        if($this->db === null) {
            $this->db = \Yii::$app->getDb();
        }

        $this->tableNames = $this->db->getSchema()->getTableNames();
    }

    public function run()
    {
        $command = $this->db->createCommand();
        $changed = false;

        foreach ($this->nameSpaces as $key => $nameSpace) {
            if(is_integer($key)) {
                $alias = '@' . str_replace('\\', '/', $nameSpace);
                $path = \Yii::getAlias($alias);
            } else {
                $path = $key;
            }

            if(!is_dir($path)) {
                echo 'Directory not exist' . PHP_EOL;
                echo 'Path - "' . $path . '"' . PHP_EOL;
                echo 'Namespace - "' . $nameSpace . '"' . PHP_EOL . PHP_EOL;
                break;
            }

            foreach (glob($path . '*.php') as $file) {
                $info = pathinfo($file);
                $modelCls = $nameSpace . $info['filename'];

                /**
                 * @var $model ActiveRecord
                 */
                $model = new $modelCls();
{'fraction_non_alphanumeric': 0.16090559935314333, 'fraction_numerical': 0.004447139680614514, 'mean_word_length': 1.5104008117706749, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, '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': '715273', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1512, 'n_words': 296}