public function __construct(array $opt) { C::start('Trying construct PDO...', 0, 1); try { C::start('Constructing PDO...', 1); $dsn = 'mysql:dbname=' . $opt['name'] . ';host=' . $opt['host']; $pdo = new PDO($dsn, $opt['username'], $opt['password']); C::finish(); C::start('Reading table relation...', 1); $query = $pdo->query(<<<SQL SELECT `TABLE_NAME`, `COLUMN_NAME`, `REFERENCED_TABLE_NAME`, `REFERENCED_COLUMN_NAME` FROM `information_schema`.`KEY_COLUMN_USAGE` WHERE `CONSTRAINT_SCHEMA` = '{$opt['name']}' AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL AND `REFERENCED_TABLE_NAME` IS NOT NULL AND `REFERENCED_COLUMN_NAME` IS NOT NULL SQL ); while ($row = $query->fetch(PDO::FETCH_ASSOC)) { isset($this->relation[$row['TABLE_NAME']]) || ($this->relation[$row['TABLE_NAME']] = array()); $this->relation[$row['TABLE_NAME']][$row['COLUMN_NAME']] = array('table' => $row['REFERENCED_TABLE_NAME'], 'field' => $row['REFERENCED_COLUMN_NAME']); in_array($row['REFERENCED_TABLE_NAME'], $this->refTable) || array_push($this->refTable, $row['REFERENCED_TABLE_NAME']); } C::finish(); C::start('Reading table list...', 1, 1); $query = $pdo->query('show tables'); while ($table = $query->fetchColumn()) { C::start('Constructing table: ' . $table . '...', 2); $this->table[$table] = new Table($table, $pdo->query('show columns from ' . $table)->fetchAll(PDO::FETCH_ASSOC), isset($this->relation[$table]) ? $this->relation[$table] : array(), in_array($table, $this->refTable)); C::finish(); } C::finish(); $pdo = null; } catch (PDOException $e) { $msg = $e->getMessage(); C::error('Connection failed: ' . ltrim(substr($msg, strrpos($msg, ']') + 1))); } count($this->table) || C::error('No tables in database ' . $opt['name']); C::finish(); }
public function __construct($dir) { $c = C::config(); C::start('Setting project directory to: ' . $dir . '...'); if (file_exists(realpath($dir))) { $dir = realpath($dir); } else { !mkdir($dir, 0755, true) || ($dir = realpath($dir)); } $this->tar = C::fixslashes($dir); is_dir($this->tar) || C::error('invalid dir'); C::finish(); if ($c['server']) { C::start('Setting project link: ...'); isset($c['server']['host'], $c['server']['path']) || C::error('invalid server config'); $this->rlink = C::fixslashes($c['server']['host']) . str_replace(C::fixslashes($c['server']['path']), '', $this->tar); C::finish(); } C::start('Checking config...'); isset($c['generator']['template']) || C::error('no generator.template'); isset($c['database']['name']) || C::error('no database.name'); isset($c['database']['host']) || C::error('no database.host'); isset($c['database']['username']) || C::error('no database.username'); isset($c['database']['password']) || C::error('no database.password'); isset($c['fixed']) || C::error('no fixed'); isset($c['files']) || C::error('no files'); C::finish(); $gen_temp = C::config('generator')['template']; !is_array($gen_temp) || ($gen_temp = end($gen_temp)); C::start('Setting template directory to: ' . $gen_temp . '...'); file_exists($gen_temp) || C::error('fail'); $this->dir = C::fixslashes($gen_temp); C::finish(); C::start('Checking lookup path...'); $paths = C::config('path') ?: array(); foreach ($paths as $key => $value) { $paths[$key] = C::fixslashes($value); } $this->path = $paths; C::config('path', $paths); C::finish(); C::start('Checking files...', 0, 1); ob_start(); $ok = true; $nss = array(); foreach (C::config('files') as $key => $value) { C::start('Checking: ' . $key . '...'); isset($value['name']) || C::error('no key name'); $msg = 'OK'; $path = isset($value['path']) ? C::fixslashes($value['path']) : ''; $e = C::ext($value['name']); $path || !isset($paths[$e]) || ($path = $paths[$e]); if (isset($value['template'])) { $ok = file_exists($this->dir . $value['template']); $ok || ($msg = 'file not exists'); } elseif (isset($value['copyConfig'])) { $ok = isset($c[$key]); $ok || ($msg = 'no ' . $key . ' in config'); } !$path || ($nss[$key . '_namespace'] = rtrim(strtr($path, '/', '\\'), '\\')); C::finish($msg); if ($ok) { continue; } else { break; } } C::config('namespaces', $nss); C::shiftAll(ob_get_clean(), 1); $ok || C::error('Failed'); C::finish(); if (isset($c['composer'], $c['composer']['path'])) { C::start('Checking composer...'); exec($c['composer']['path'] . ' -v', $tmp); isset($tmp[7]) || C::error('Composer was not installed'); $this->cmp = $c['composer']['path']; !isset($c['composer']['command']) || ($this->cmpCmd = $c['composer']['command']); C::finish(); } if (isset($c['generator']['token'])) { $this->token = $c['generator']['token']; } $this->str = $this->dir . $this->str; if (isset($c['generator']['structure'])) { C::start('Checking structure...'); if (file_exists($c['generator']['structure'])) { $this->str = $c['generator']['structure']; } elseif (file_exists($this->dir . $c['generator']['structure'])) { $this->str = $this->dir . $c['generator']['structure']; } else { C::error('Structure doesn\'t exists'); } C::finish(); } C::start('Construct hole database...', 0, 1); ob_start(); C::set('db', new Database($c['database'])); C::shiftAll(ob_get_clean(), 1); C::finish(); $this->tempDir = C::fixslashes(sys_get_temp_dir()) . 'crudgen/'; }