luya\console\commands\ImportController::actionIndex PHP Method

actionIndex() public method

Run the import process.
public actionIndex ( ) : number
return number
    public function actionIndex()
    {
        try {
            $queue = [];
            $this->verbosePrint('Run import index', __METHOD__);
            foreach (Yii::$app->getModules() as $id => $module) {
                if ($module instanceof \luya\base\Module) {
                    $this->verbosePrint('collect module importers from module: ' . $id, __METHOD__);
                    $response = $module->import($this);
                    if (is_array($response)) {
                        // importer returns an array with class names
                        foreach ($response as $class) {
                            $this->verbosePrint("add object '{$class}' to queue list", __METHOD__);
                            $obj = new $class($this);
                            $prio = $obj->queueListPosition;
                            while (true) {
                                if (!array_key_exists($prio, $queue)) {
                                    break;
                                }
                                ++$prio;
                            }
                            $queue[$prio] = $obj;
                        }
                    }
                }
            }
            ksort($queue);
            foreach ($queue as $pos => $object) {
                $this->verbosePrint("run object '" . $object->className() . " on pos {$pos}.", __METHOD__);
                $objectResponse = $object->run();
                $this->verbosePrint("run object response: " . var_export($objectResponse, true), __METHOD__);
            }
            if (Yii::$app->hasModule('admin')) {
                Config::set('last_import_timestamp', time());
                Yii::$app->db->createCommand()->update('admin_user', ['force_reload' => 1])->execute();
            }
            $this->verbosePrint('importer finished, get log output: ' . var_export($this->getLog(), true), __METHOD__);
            foreach ($this->getLog() as $section => $value) {
                $this->outputInfo(PHP_EOL . $section . ":");
                foreach ($value as $k => $v) {
                    if (is_array($v)) {
                        foreach ($v as $kk => $kv) {
                            if (is_array($kv)) {
                                $this->output(" - {$kk}: " . print_r($kv, true));
                            } else {
                                $this->output(" - {$kk}: {$kv}");
                            }
                        }
                    } else {
                        $this->output(" - " . $v);
                    }
                }
            }
            return $this->outputSuccess("Importer run successfull.");
        } catch (Exception $err) {
            return $this->outputError(sprintf("Exception while importing: '%s' in file '%s' on line '%s'.", $err->getMessage(), $err->getFile(), $err->getLine()));
        }
    }