<?php
 namespace TCMS\Gen;
 if(!function_exists('array_key_last')) {
 function array_key_last($array) {
 foreach($array as $k => $v) {
 }
 if(isset($k)) return $k;
 return null;
 }
 }
 function showHelpTextColor($cmd, $desc) {
 if(strlen($cmd) < 13) echo "\e[1;32m  $cmd\e[0m\t\t$desc\n";
 else echo "\e[1;32m  $cmd\e[0m\t$desc\n";
 }
 function showLogInfo($text) {
 echo "\e[1;34m$text\e[0m\n";
 }
 function showLogError($text) {
 echo "\e[1;31m$text\e[0m\n";
 }
 function showLogSuccess($text) {
 echo "\e[1;32m$text\e[0m\n";
 }
 function drawLine() {
 echo "-------------------------------------------------------\n";
 }
 function readAppId() {
 $appId = false;
 $fn = fopen(PUBLIC_PATH . 'index.php', "r");
 if($fn) {
 while (!feof($fn)) {
 $line = fgets($fn);
 $line = trim($line);
 $startIndex = strpos($line, "define('APP_ID',");
 if($startIndex !== false) {
 $startIndex += 18;
 $endIndex = strpos($line, ")", $startIndex);
 $appId = substr($line, $startIndex, $endIndex - $startIndex - 1);
 break;
 }
 }
 fclose($fn);
 }
 return $appId;
 }
 function readConfigFile($path) {
 $content = [];
 $fn = fopen($path, "r");
 if($fn) {
 while (!feof($fn)) {
 $line = fgets($fn);
 $content[] = $line;
 }
 fclose($fn);
 }
 return $content;
 }
 class TCMSGenerated {
 private $cmdSupport = [ 'make:module', 'make:extension', 'make:model', 'make:controller', 'make:migration', 'make:app', 'migration:update', 'migration:rollback', 'an-remove-model', 'cache:clear', 'publish', '-h', '--help', '-v', '--version' ];
 private $argSupport = [ 'name' => 'n', 'table' => 'tb', 'class' => 'cla', 'controller' => 'c', 'model' => 'm', 'help' => 'h', 'm-model' => 'mm', 'module' => 'mod', 'p-module' => 'pm', 'p-extension' => 'pe', 'extension' => 'e', 'type' => 't', 'private' => 'p', 'layout' => 'l', 'area' => 'a', 'app' => 'app' ];
 private $argNoneData = [ 'h', 'help', 'v', 'version', 'migration:update', 'private', 'p', 'layout', 'l', 'user-data' ];
 private function parseArgv($argv) {
 if(count($argv) < 2) {
 return false;
 }
 $cmd = false;
 $options = [];
 $isNeedGetData = false;
 $key = false;
 for($i = 1;
 $i < count($argv);
 $i++) {
 if(!$cmd) {
 if(in_array($argv[$i], $this->cmdSupport)) {
 $cmd = $argv[$i];
 if(!in_array($cmd, $this->argNoneData)) {
 $key = $cmd;
 $isNeedGetData = true;
 }
 continue;
 }
 }
 if($isNeedGetData) {
 $isInArgs = false;
 foreach($this->argSupport as $k => $v) {
 if('-' . $v == $argv[$i] || '--' . $k == $argv[$i]) {
 $isInArgs = true;
 break;
 }
 }
 $isNeedGetData = false;
 if(!$isInArgs) {
 $options[$key] = trim($argv[$i]);
 continue;
 }
 }
 if(strpos($argv[$i], "--") === 0) {
 $key = trim(substr($argv[$i], 2));
 if(array_key_exists($key, $this->argSupport)) {
 if(in_array($key, $this->argNoneData)) {
 $options[$key] = true;
 $key = false;
 }
 else {
 $isNeedGetData = true;
 }
 }
 continue;
 }
 if(strpos($argv[$i], "-") === 0) {
 $key = trim(substr($argv[$i], 1));
 if(in_array($key, $this->argSupport)) {
 foreach($this->argSupport as $k => $v) {
 if($v == $key) {
 $key = $k;
 break;
 }
 }
 if(in_array($key, $this->argNoneData)) {
 $options[$key] = true;
 $key = false;
 }
 else {
 $isNeedGetData = true;
 }
 }
 }
 }
 if(!$cmd) {
 return false;
 }
 return [$cmd, $options];
 }
 function showHelp() {
 drawLine();
 $this->showVersion();
 echo "\nFormat:\n";
 echo "  command [options] [arguments]\n\n";
 echo "Options:\n";
 showHelpTextColor("-h, --help", "Xem hướng dẫn");
 showHelpTextColor("-v, --version", "\tXem version");
 echo "\nCommands:\n";
 showHelpTextColor("make:app", "Tạo app");
 showHelpTextColor("make:module", "Tạo module");
 showHelpTextColor("make:extension", "Tạo extension");
 showHelpTextColor("make:model", "Tạo model");
 showHelpTextColor("make:controller", "Tạo controller");
 showHelpTextColor("make:migration", "Tạo migration");
 showHelpTextColor("migration:update", "Update migration");
 showHelpTextColor("migration:rollback", "Rollback migration");
 showHelpTextColor("cache:clear", "Xóa cache");
 showHelpTextColor("publish", "Publish ứng dụng");
 drawLine();
 }
 private function showVersion() {
 echo "TCMS Framework Version 1.0\n";
 }
 private function showCopyright() {
 showLogInfo("Copyright 2019 by Trần Văn Thỏa");
 }
 private function createModule($options, $isMakeController = false) {
 include_once(GEN_PATH . 'tcms.gen.module.php');
 $m = new \TCMS\Gen\TCMSGenModule();
 if($isMakeController) {
 $m->createController($options);
 }
 else {
 $m->createModule($options);
 }
 }
 private function creatApp($options) {
 include_once(GEN_PATH . 'tcms.gen.project.php');
 $m = new \TCMS\Gen\TCMSGenProject();
 $m->createApp($options);
 }
 private function createMigration($options) {
 include_once(GEN_PATH . 'tcms.gen.migration.php');
 $m = new \TCMS\Gen\TCMSGenMigration();
 $m->createMigration($options);
 }
 private function updateMigration($options) {
 include_once(GEN_PATH . 'tcms.gen.migration.php');
 $m = new \TCMS\Gen\TCMSGenMigration();
 $m->updateMigration($options);
 }
 private function rollBackMigration($options) {
 include_once(GEN_PATH . 'tcms.gen.migration.php');
 $m = new \TCMS\Gen\TCMSGenMigration();
 $m->rollBackMigration($options);
 }
 private function createExtension($options) {
 include_once(GEN_PATH . 'tcms.gen.extension.php');
 $m = new \TCMS\Gen\TCMSGenExtension();
 $m->createExtension($options);
 }
 private function createModel($options) {
 include_once(GEN_PATH . 'tcms.gen.model.php');
 $m = new \TCMS\Gen\TCMSGenModel();
 $m->createModel($options);
 }
 private function removeModel($options) {
 include_once(GEN_PATH . 'tcms.gen.model.php');
 $m = new \TCMS\Gen\TCMSGenModel();
 return $m->removeModel($options);
 }
 private function deleteDir($dirPath) {
 if (! is_dir($dirPath)) {
 return;
 }
 if (substr($dirPath, strlen($dirPath) - 1, 1) != DIRECTORY_SEPARATOR) {
 $dirPath .= DIRECTORY_SEPARATOR;
 }
 $files = glob($dirPath . '*', GLOB_MARK);
 foreach ($files as $file) {
 if (is_dir($file)) {
 $this->deleteDir($file);
 }
 else {
 unlink($file);
 }
 }
 @rmdir($dirPath);
 }
 private function clearCache($options) {
 $this->deleteDir(ROOT_PATH . 'data' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR);
 if(!file_exists(ROOT_PATH . 'data' . DIRECTORY_SEPARATOR . 'cache')) mkdir(ROOT_PATH . 'data' . DIRECTORY_SEPARATOR . 'cache');
 $this->deleteDir(ROOT_PATH . 'data' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR);
 if(!file_exists(ROOT_PATH . 'data' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'views')) mkdir(ROOT_PATH . 'data' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'views');
 showLogSuccess("Clear cache xong!");
 }
 private function publish($options) {
 include_once(GEN_PATH . 'tcms.gen.publish.php');
 $m = new \TCMS\Gen\TCMSPublish();
 $m->publish($options);
 }
 private function execCmd($cmd, $options) {
 switch($cmd) {
 case '-h': case '--help': $this->showHelp();
 break;
 case '-v': case '--version': echo "\n";
 $this->showVersion();
 $this->showCopyright();
 break;
 case 'make:module': $this->createModule($options);
 break;
 case 'make:app': $this->creatApp($options);
 break;
 case 'make:migration': $this->createMigration($options);
 break;
 case 'migration:update': $this->updateMigration($options);
 break;
 case 'migration:rollback': $this->rollBackMigration($options);
 break;
 case 'make:extension': $this->createExtension($options);
 break;
 case 'make:model': $this->createModel($options);
 break;
 case 'make:controller': $this->createModule($options, true);
 break;
 case 'an-remove-model': return $this->removeModel($options);
 break;
 case 'cache:clear': $this->clearCache($options);
 break;
 case 'publish': $this->publish($options);
 break;
 }
 }
 public function exec($argv) {
 $ret = $this->parseArgv($argv);
 if(!$ret) {
 showLogError("Cú pháp không hợp lệ, nhập --help để xem hướng dẫn");
 exit();
 }
 $this->execCmd($ret[0], $ret[1]);
 }
 }
