<?php

namespace app\command;

use Exception;
use plugin\admin\app\model\Config;
use support\Request;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use support\think\Db;


class Language extends Command
{
    protected static $defaultName = 'Language:scan';
    protected static $defaultDescription = '自动完成多语言的文件提取';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addOption('file','f', InputArgument::OPTIONAL, '只是针对那个文件');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $file = $input->getOption('file');
        $file = base_path()."/app/api/controller/ServerController.php";
        $dir = new \RecursiveDirectoryIterator(base_path().'/app');
        $iterator = new \RecursiveIteratorIterator($dir);
        $phpFiles = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
        $fnlist = [];
        $result = [
            'common.php'=>""
        ];
        foreach ($phpFiles as $_file) {
            $_file = $_file[0];
            $fnlist[] = $_file;
            $key = 'common.php';
            if(false !==strpos($_file, base_path().'/app/api/controller')){
                $key = 'api/'.pathinfo($_file,PATHINFO_BASENAME);
            }
            if(false !==strpos($_file, base_path().'/app/controller')){
                $key = pathinfo($_file,PATHINFO_BASENAME);
            }
            $key = strtolower(str_replace('Controller','',$key));
            //cp($key);
            $res = $this->parseOneFile($_file);
            $result[$key]=$res;
        }
        //$res = $this->parseOneFile($file);
        //cp($result);
        $this->write2file($result);
        return 0;
    }
    function write2file($data=[]){
        $langs = ['zh','en'];
        foreach($data as $fn=>$arr){
            foreach($langs as $lang){
                $lang_path = base_path().'/resource/translations/'.$lang.'/';
                $_common_arr = require($lang_path.'common.php');
                $_arr = [];
                if(file_exists($lang_path.$fn)){
                    $_arr = require($lang_path.$fn);
                }
                foreach($arr as $ov){
                    if(!isset($_common_arr[$ov]) && !isset($_arr[$ov])){
                        $_arr[$ov]=$ov;
                    }
                    if(isset($_common_arr[$ov]) && isset($_arr[$ov])){
                        unset($_arr[$ov]);
                    }
                }
                file_put_contents($lang_path.$fn,'<?php'.PHP_EOL.'return '.var_export($_arr,true).';');
                //cp('写入文件:'.$lang_path.$fn);
            }
        }

    }
    function parseOneFile($fn){
        cp('解析文件:',$fn);
        if(file_exists($fn)){
            $content = file_get_contents($fn);
            $matchs = [];
            preg_match_all('/__\(([\'"])(.*?)\1\s*(?:,\s*\[.*?\])?\)/',$content,$matchs);
            //cp($matchs[2]);
            return $matchs[2];
        }else{
            cp('文件不存在:'.$fn);
        }
    }
}
