您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuGenerator.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace Luticate\Generator;
  3. use Luticate\Utils\DataAccess\AbstractDbDataAccess;
  4. use Luticate\Utils\DataAccess\PgSqlDataAccess;
  5. use PDO;
  6. use Twig_Autoloader;
  7. use Twig_Environment;
  8. use Twig_Loader_Filesystem;
  9. use Luticate\Utils\Business\LuStringUtils;
  10. class LuGenerator {
  11. private $_pdo;
  12. private $_config = array("dbo" =>
  13. array(
  14. "namespace" => 'App\Dbo',
  15. "folder" => '../app/Dbo'
  16. ),
  17. "sp" =>
  18. array(
  19. "namespace" => 'App\DataAccess\SP',
  20. "folder" => '../app/DataAccess/SP'
  21. ),
  22. "dataaccess" =>
  23. array(
  24. "namespace" => 'App\DataAccess',
  25. "folder" => '../app/DataAccess'
  26. ),
  27. "business" =>
  28. array(
  29. "namespace" => 'App\Business',
  30. "folder" => '../app/Business'
  31. ),
  32. "controller" =>
  33. array(
  34. "namespace" => 'App\Controller',
  35. "folder" => '../app/Controller'
  36. ),
  37. "mode" => 0775,
  38. "ignore" => array(
  39. "tables" => array(),
  40. "sp" => array(),
  41. "controllers" => array()
  42. )
  43. );
  44. /**
  45. * @return array
  46. */
  47. public function getConfig()
  48. {
  49. return $this->_config;
  50. }
  51. /**
  52. * @param array $config
  53. */
  54. public function setConfig($config)
  55. {
  56. $this->_config = $config;
  57. }
  58. public function __construct($database)
  59. {
  60. $dsn = $database["driver"] . ":dbname=" . $database["database"] . ";host="
  61. . $database["host"] . ";port=" . $database["port"];
  62. $this->_pdo = new PDO($dsn, $database["username"], $database["password"]);
  63. }
  64. protected function printError($message)
  65. {
  66. echo $message . "\n";
  67. var_dump($this->_pdo->errorInfo());
  68. return null;
  69. }
  70. protected function buildTwigVars($vars)
  71. {
  72. foreach ($vars as $key => $value)
  73. {
  74. if (is_array($value))
  75. $vars[$key] = $this->buildTwigVars($value);
  76. else if (is_string($value))
  77. {
  78. $vars[$key] = array(
  79. "as_it" => $value,
  80. "camel_upper" => LuStringUtils::snakeToCamelCase($value, true),
  81. "camel_lower" => LuStringUtils::snakeToCamelCase($value, false)
  82. );
  83. }
  84. }
  85. return $vars;
  86. }
  87. protected function buildTwig($templateFile, $destFile, $vars)
  88. {
  89. $vars["dbo_namespace"] = $this->_config["dbo"]["namespace"];
  90. $vars["sp_namespace"] = $this->_config["sp"]["namespace"];
  91. $vars["dataaccess_namespace"] = $this->_config["dataaccess"]["namespace"];
  92. $vars["business_namespace"] = $this->_config["business"]["namespace"];
  93. $vars["controller_namespace"] = $this->_config["controller"]["namespace"];
  94. $twig_vars = $this->buildTwigVars($vars);
  95. Twig_Autoloader::register();
  96. $loader = new Twig_Loader_Filesystem(__DIR__ );
  97. $twig = new Twig_Environment($loader, array());
  98. $template = $twig->loadTemplate('templates/' . $templateFile . '.twig');
  99. $content = $template->render($twig_vars);
  100. file_put_contents($destFile, $content);
  101. }
  102. public function sqlTypesToPhpTypes($array)
  103. {
  104. $db = $this->getDbDataAccess();
  105. foreach ($array as $key => $value)
  106. {
  107. $array[$key]["data_type"] = array(
  108. "sql" => $array[$key]["data_type"],
  109. "php" => $db->sqlTypeToPhpType($array[$key]["data_type"])
  110. );
  111. }
  112. return $array;
  113. }
  114. public function generateDbo($name, $columns, $file, $fileArray)
  115. {
  116. $vars = array(
  117. "dbo_name" => $name,
  118. "columns" => $columns
  119. );
  120. $this->buildTwig('dbo.php', $file, $vars);
  121. $this->buildTwig('dbo_array.php', $fileArray, $vars);
  122. }
  123. public function generateSp($sp, $file, $spFile, $dboName, $dboFile, $dboFileArray)
  124. {
  125. file_put_contents($spFile, $sp["sp"]["prosrc"]);
  126. $args = [];
  127. $db = $this->getDbDataAccess();
  128. foreach ($sp['args']['out'] as $arg) {
  129. $args[] = [
  130. "name" => $arg["name"],
  131. "data_type" => [
  132. "php" => $db->sqlTypeToPhpType($arg["data_type"]),
  133. "sql" => $arg["data_type"]
  134. ]
  135. ];
  136. }
  137. $this->generateDbo($dboName, $args, $dboFile, $dboFileArray);
  138. $vars = [
  139. "args" => $sp["args"],
  140. "sp" => $sp["sp"],
  141. "dboName" => $dboName
  142. ];
  143. $this->buildTwig('sp.php', $file, $vars);
  144. }
  145. public function generateDataAccess($table_name, $connection_name, $dataAccessName, $dboName, $file)
  146. {
  147. if (file_exists($file))
  148. return;
  149. $vars = array(
  150. "data_access_name" => $dataAccessName,
  151. "connection_name" => $connection_name,
  152. "table_name" => $table_name,
  153. "dbo_name" => $dboName
  154. );
  155. $this->buildTwig('dataaccess.php', $file, $vars);
  156. }
  157. public function generateBusiness($businessName, $dataAccessName, $dboName, $file)
  158. {
  159. if (file_exists($file))
  160. return;
  161. $vars = array(
  162. "business_name" => $businessName,
  163. "data_access_name" => $dataAccessName,
  164. "dbo_name" => $dboName
  165. );
  166. $this->buildTwig('business.php', $file, $vars);
  167. }
  168. public function generateController($controllerName, $businessName, $dboName, $file)
  169. {
  170. if (file_exists($file))
  171. return;
  172. $vars = array(
  173. "controller_name" => $controllerName,
  174. "business_name" => $businessName,
  175. "dbo_name" => $dboName
  176. );
  177. $this->buildTwig('controller.php', $file, $vars);
  178. }
  179. public function mkdir($dir, $dir_mode)
  180. {
  181. if (!file_exists($dir))
  182. mkdir($dir, $dir_mode, true);
  183. }
  184. public function matchIgnore($ignore, $name)
  185. {
  186. foreach ($this->_config["ignore"][$ignore] as $reg)
  187. {
  188. if (preg_match($reg, $name))
  189. return true;
  190. }
  191. return false;
  192. }
  193. public function run()
  194. {
  195. $dbo_dir = $this->_config["dbo"]["folder"] . "/";
  196. $sp_dir = $this->_config["sp"]["folder"] . "/";
  197. $sp_src_dir = $this->_config["sp"]["folder"] . "/src/";
  198. $manager_dir = $this->_config["dataaccess"]["folder"] . "/";
  199. $business_dir = $this->_config["business"]["folder"] . "/";
  200. $controller_dir = $this->_config["controller"]["folder"] . "/";
  201. $connection_name = getenv("DB_CONNECTION_NAME");
  202. $mode = $this->_config["mode"];
  203. umask(0000);
  204. $this->mkdir($dbo_dir, $mode);
  205. $this->mkdir($sp_dir, $mode);
  206. $this->mkdir($sp_src_dir, $mode);
  207. $this->mkdir($manager_dir, $mode);
  208. $this->mkdir($business_dir, $mode);
  209. $this->mkdir($controller_dir, $mode);
  210. $tables = $this->getTables();
  211. if (!is_null($tables)) {
  212. echo "Found " . count($tables) . " tables\n";
  213. foreach ($tables as $table_name => $columns) {
  214. if ($this->matchIgnore("tables", $table_name)) {
  215. echo "Table $table_name ignored\n";
  216. continue;
  217. }
  218. $columns = $this->sqlTypesToPhpTypes($columns);
  219. echo "Found " . count($columns) . " columns in table " . $table_name . "\n";
  220. $baseName = LuStringUtils::snakeToCamelCase($table_name, true);
  221. $dboName = $baseName . "Dbo";
  222. $dataAccessName = $baseName . "DataAccess";
  223. $businessName = $baseName . "Business";
  224. $controllerName = $baseName . "Controller";
  225. $this->generateDbo($dboName, $columns, $dbo_dir . $dboName . ".php", $dbo_dir . $dboName . "Array.php");
  226. $this->generateDataAccess($table_name, $connection_name, $dataAccessName, $dboName,
  227. $manager_dir . $dataAccessName . ".php");
  228. $this->generateBusiness($businessName, $dataAccessName, $dboName,
  229. $business_dir . $businessName . ".php");
  230. if ($this->matchIgnore("controllers", $controllerName)) {
  231. echo "Controller $controllerName ignored\n";
  232. continue;
  233. }
  234. $this->generateController($controllerName, $businessName, $dboName,
  235. $controller_dir . $controllerName . ".php");
  236. }
  237. }
  238. else {
  239. $this->printError("Failed to get tables");
  240. }
  241. $sps = $this->getStoredProcedures();
  242. if (!is_null($sps)) {
  243. echo "Found " . count($sps) . " stored procedures\n";
  244. foreach ($sps as $sp_name => $sp)
  245. {
  246. if ($this->matchIgnore("sp", $sp_name)) {
  247. echo "Stored procedure $sp_name ignored\n";
  248. continue;
  249. }
  250. $args = $sp["args"];
  251. echo "Found " . count($args['in']) . " input and " .
  252. count($args['out']) . " output arguments in stored procedure " . $sp_name . "\n";
  253. $args["in"] = $this->sqlTypesToPhpTypes($args["in"]);
  254. $args["out"] = $this->sqlTypesToPhpTypes($args["out"]);
  255. $sp_model_name = LuStringUtils::snakeToCamelCase($sp_name, true);
  256. $dboName = $sp_model_name . "Dbo";
  257. $this->generateSp($sp, $sp_dir . $sp_model_name . ".php", $sp_src_dir . $sp_name . ".sql", $dboName,
  258. $dbo_dir . $dboName . ".php", $dbo_dir . $dboName . "Array.php");
  259. }
  260. }
  261. else {
  262. $this->printError("Failed to get stored procedures");
  263. }
  264. }
  265. public function getStoredProcedures()
  266. {
  267. return $this->getDbDataAccess()->getStoredProceduresFull($this->_pdo);
  268. }
  269. public function getTables()
  270. {
  271. return $this->getDbDataAccess()->getTablesFull($this->_pdo);
  272. }
  273. /**
  274. * @return AbstractDbDataAccess|null
  275. */
  276. public function getDbDataAccess()
  277. {
  278. $driver = $this->_pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  279. if ($driver == "pgsql") {
  280. return new PgSqlDataAccess();
  281. }
  282. return null;
  283. }
  284. }