Exemplo n.º 1
0
 public function __construct(Table $table)
 {
     $this->table = $table;
     foreach ($this->default as $key => $value) {
         if (method_exists($this, $method = 'token' . C::camelhead($key))) {
             $this->{$method}();
         }
     }
     foreach (C::config('fixed') as $key => $value) {
         $this->default[$key] = $value;
     }
     foreach (C::config('path') as $key => $value) {
         $path = 'path_' . $key;
         $this->{$path} = C::fixslashes($value);
     }
     $this->run();
 }
Exemplo n.º 2
0
    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();
    }
Exemplo n.º 3
0
 public function __construct(Table $table)
 {
     parent::__construct($table);
     $eol = C::eol();
     $t = array(2 => C::tab(2, 4), 3 => C::tab(3, 4), 4 => C::tab(4, 4), 22 => C::tab(2, 2), 32 => C::tab(3, 2), 42 => C::tab(4, 2));
     $column_header = $column_select = $pk = $schema = $rel = $fields_form = $namespace_list = $option_list = $date_field = '';
     foreach ($table->column as $key => $column) {
         $column_select .= '{table}.' . $column->Field . ',' . $eol;
         $column_header .= $t[42] . '<th>{{ @fields.' . $column->Field . ' }}</th>' . $eol;
         $fields_form .= $this->form($column) . $eol;
         $schema .= str_replace(array('field', 'label', 'EOL', 'T4', 'T3'), array($column->Field, $column->label, $eol, $t[4], $t[3]), "'field'=>array(EOLT4'label',EOLT4" . $this->filter($column) . "," . 'EOLT4' . $column->defaultContent . "EOLT4),EOLT3");
         if ($column->isReferenced) {
             $namespace_list .= 'use {#model_namespace#}\\' . C::camelhead($column->referencedTable) . ';' . $eol;
             $option_list .= $eol . $t[2] . '$moe->set(\'' . C::camelcase($column->referencedTable) . '\', ' . C::camelhead($column->referencedTable) . '::instance()->optionList());';
         }
         !$column->isDate || ($date_field .= $t[3] . '$' . $column->Field . ' = $moe->get(\'POST.' . $column->Field . '\');' . $eol . $t[3] . 'krsort($' . $column->Field . ');' . $eol . $t[3] . '$moe->set(\'POST.' . $column->Field . '\', implode(\'-\', $' . $column->Field . '));' . $eol);
         !$column->isPrimaryKey() || ($pk .= "'{$column->Field}', ");
     }
     if ($table->relation) {
         $this->use_relation = $eol . $t[3] . '->useRelation(\'all\')';
         $db = C::get('db');
         foreach ($table->relation as $rel) {
             foreach ($db->table[$rel['table']]->column as $column) {
                 $tname = $db->table[$rel['table']]->name;
                 $column_select .= $tname . '.' . $column->Field . ' as ' . $tname . '_' . $column->Field . ',' . $eol;
                 $column_header .= $t[42] . '<th>' . $column->label . '</th>' . $eol;
             }
         }
         $db = null;
         unset($db);
     }
     $this->controller = 'Crud' . $table->model;
     $this->model = $table->model;
     $this->table_name = $table->name;
     $this->label = $table->label;
     $this->column_header = trim($column_header);
     $this->namespace_list = ltrim($namespace_list);
     $this->option_list = rtrim($option_list);
     $this->fields_form = ltrim($fields_form);
     $this->date_field = ltrim($date_field);
     $this->column_select = trim($column_select, ',' . $eol);
     $this->schema = rtrim($schema);
     $this->pk = trim($pk, ' ,');
     $this->primary_key = reset($table->primary_key);
     foreach ($table->relation as $fil1 => $tab) {
         $this->rel .= str_replace(array('{tab}', '{fil1}', '{fil2}', '{EOL}', '{S3}'), array($tab['table'], $fil1, $tab['field'], $eol, $t[3]), "'{tab}'=>'join {join} on {join}.{fil2} = {table}.{fil1}',{EOL}{S3}");
     }
     $this->rel = trim($this->rel);
     foreach (C::config('namespaces') ?: array() as $key => $value) {
         $key2 = $key . '_quoted';
         $this->{$key} = $this->replaceToken($value);
         $this->{$key2} = addslashes($this->{$key});
     }
     $cff = array();
     foreach (C::config('files') ?: array() as $key => $value) {
         C::ext($value['name']) !== 'ini' || array_push($cff, C::name($this->replaceToken($value['name'])));
     }
     ksort($cff);
     $this->config_files = "'" . implode("'," . C::eol() . C::tab(1, 4) . "'", $cff) . "'";
     $this->table = $table;
 }
Exemplo n.º 4
0
 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/';
 }