/** * 复制目录下的所有目录和文件到另外一个目录 * * @param string $fromdir 源文文件目录 * @param string $todir 目标文件目录 * @param boolean $autocoverageold 是否覆盖已有文件,true覆盖,false跳过 * @param string $storage 物理存储组,不传则为默认 * @return array($dook,$doerror) */ public static function copy_dir($fromdir, $todir, $autocoverageold = true, $storage = 'default') { $fromdir = rtrim($fromdir, '\\/') . DS; $todir = rtrim($todir, '\\/') . DS; if ($fromdir == $todir) { return array(0, 0); } $info1 = File::check_and_get_path($fromdir); $info2 = File::check_and_get_path($todir); if (File::can_do_run($storage)) { if (!is_dir($fromdir)) { return array(0, 0); } # 完成数 $donum = array(0, 0); if (!is_dir($todir)) { # 创建目标目录 File::create_dir($todir, false, $storage); } # 列出目录中当前级别的目录和文件 $files = glob($fromdir . '*'); foreach ($files as $file) { # 目标文件 $tofile = $todir . basename($file); if (is_dir($file)) { # 如果当前是目录,则移动目录 # 移动目录 $donum2 = File::copy_dir($file, $tofile, $autocoverageold, $storage); if ($donum2) { $donum[0] += $donum2[0]; $donum[1] += $donum2[1]; } } else { # 文件 if ($autocoverageold && file_exists($tofile)) { //覆盖已有文件 @unlink($tofile); } if (@copy($file, $tofile)) { $donum[0]++; } else { $donum[1]++; } } } return $donum; } else { return File::call_http_host($storage, 'file/copy_dir', $info1[0], $info1[1], $info2[0], $info2[1], $autocoverageold); } }
/** * Generate MySQL Documentation for HTML. * * Usage (from command line): * * php oil refine mydoc:html <table_schema> <output_dir = "app/tmp/"> */ public static function html($table_schema = null, $dir = null) { if (empty($table_schema)) { static::help(); exit; } empty($dir) and $dir = APPPATH . 'tmp' . DS; $dir = rtrim($dir, DS) . DS . 'mydoc' . DS; /** * connect to db */ $ret = static::connect($table_schema); /** * delete and create mydoc dir */ if (file_exists($dir)) { if (!\Cli::option('f') and !\Cli::option('force')) { \Cli::write(realpath($dir) . ' already exist, please use -f option to force delete and generate.', 'red'); exit; } $ret = \File::delete_dir($dir); if ($ret === false) { \Cli::write("Could not delete directory \"{$dir}\"", 'red'); exit; } } $ret = mkdir($dir, 0777, true); if ($ret === false) { \Cli::write("Could not create directory \"{$dir}\"", 'red'); exit; } \File::copy_dir(__DIR__ . DS . '..' . DS . 'assets', $dir . 'assets'); /** * generate index.html */ $migration_table_name = \Config::get('migrations.table', 'migration'); $migration = array(); if (\DBUtil::table_exists($migration_table_name)) { $migration = \Db::select()->from($migration_table_name)->order_by('migration', 'desc')->limit(1)->execute()->as_array(); } $html = \View::forge('mydoc/index', array('migration' => $migration))->render(); \File::create($dir, 'index.html', $html); /** * get tables */ $tables = array_flip(\DB::list_tables()); /** * unset ignore tables */ foreach (\Config::get('mydoc.ignore_tables', array()) as $ignore_table_name) { if (isset($tables[$ignore_table_name])) { unset($tables[$ignore_table_name]); } } $ignore_table_regex = \Config::get('mydoc.ignore_table_regex'); foreach ($tables as $table_name => $tmp) { if (!empty($ignore_table_regex)) { if (preg_match($ignore_table_regex, $table_name)) { unset($tables[$table_name]); continue; } } $tables[$table_name] = array('indexes' => array(), 'foreign_keys' => array(), 'triggers' => array()); } /** * check table count */ if (count($tables) === 0) { \Cli::write("No tables in \"{$table_schema}\"", 'red'); exit; } /** * get foreign keys */ $sql = 'select distinct table_name, column_name, referenced_table_name, referenced_column_name from information_schema.key_column_usage where referenced_table_name is not null and referenced_column_name is not null and table_schema = :table_schema'; $foreign_keys = \Db::query($sql)->bind('table_schema', $table_schema)->execute()->as_array(); foreach ($foreign_keys as $foreign_key) { if (isset($tables[$foreign_key['table_name']])) { $tables[$foreign_key['table_name']]['foreign_keys'][$foreign_key['column_name']] = $foreign_key; } } /** * get indexes */ $sql = 'select distinct table_name, index_name, non_unique, column_name, comment from information_schema.statistics where table_schema = :table_schema'; $indexes = \Db::query($sql)->bind('table_schema', $table_schema)->execute()->as_array(); foreach ($indexes as $index) { if (isset($tables[$index['table_name']])) { $tables[$index['table_name']]['indexes'][$index['index_name']][$index['column_name']] = $index; } } /** * get triggers */ $sql = 'select distinct trigger_name, event_manipulation, event_object_table, action_statement, action_timing, definer from information_schema.triggers where trigger_schema = :trigger_schema'; $triggers = \Db::query($sql)->bind('trigger_schema', $table_schema)->execute()->as_array(); foreach ($triggers as $trigger) { if (isset($tables[$trigger['event_object_table']])) { $tables[$trigger['event_object_table']]['triggers'][] = $trigger; } } /** * generate tables.html */ $html = \View::forge('mydoc/tables', array('tables' => array_keys($tables)))->render(); \File::create($dir, 'tables.html', $html); /** * generate table_*.html */ foreach ($tables as $table_name => $infos) { $columns = \DB::list_columns($table_name); foreach ($columns as &$column) { // do we have a data_type defined? If not, use the generic type isset($column['data_type']) or $column['data_type'] = $column['type']; if ($column['data_type'] == 'enum') { $column['data_type'] .= "('" . implode("', '", $column['options']) . "')"; } $column['_length'] = null; foreach (array('length', 'character_maximum_length', 'display') as $idx) { // check if we have such a column, and filter out some default values if (isset($column[$idx]) and !in_array($column[$idx], array('65535', '16777215', '4294967295'))) { $column['_length'] = $column[$idx]; break; } } $column['_extras'] = array(); if (strpos(\Str::lower($column['key']), 'pri') !== false) { $column['_extras'][] = 'PK'; } if (strpos(\Str::lower($column['key']), 'uni') !== false) { $column['_extras'][] = 'UI'; } if (!empty($column['extra'])) { if (strpos($column['extra'], 'auto_increment') !== false) { $column['_extras'][] = 'AI'; } } $column['_foreign_key'] = null; if (!empty($infos['foreign_keys'])) { $foreign_key = \Arr::get($infos['foreign_keys'], $column['name'], array()); if (!empty($foreign_key)) { $column['_foreign_key'] = $foreign_key; $column['_extras'][] = 'FK'; } } if (!empty($column['_foreign_key'])) { $column['_parent_table_name'] = $column['_foreign_key']['referenced_table_name']; } else { $column['_foreign_key'] = array('referenced_table_name' => null, 'referenced_column_name' => null); if (0 < preg_match('/^.+_id$/', $column['name'])) { $parent_table_name = str_replace('_id', '', $column['name']); if (isset($tables[$parent_table_name = \Inflector::singularize($parent_table_name)])) { $column['_foreign_key'] = array('referenced_table_name' => $parent_table_name, 'referenced_column_name' => 'id'); } else { if (isset($tables[$parent_table_name = \Inflector::pluralize($parent_table_name)])) { $column['_foreign_key'] = array('referenced_table_name' => $parent_table_name, 'referenced_column_name' => 'id'); } } } } } $html = \View::forge('mydoc/table', array('table_name' => $table_name, 'columns' => $columns, 'infos' => $infos))->render(); \File::create($dir, 'table_' . $table_name . '.html', $html); } /** * generate indexes.html */ $html = \View::forge('mydoc/indexes', array('tables' => $tables))->render(); \File::create($dir, 'indexes.html', $html); /** * generate triggers.html */ $html = \View::forge('mydoc/triggers', array('tables' => $tables))->render(); \File::create($dir, 'triggers.html', $html); \Cli::write("Generated MySQL Documentation in \"{$dir}\"", 'green'); exit; }
public function copy_dir($path, $new_path) { return \File::copy_dir($path, $new_path, $this); }
/** * 内部调用移动目录 * */ public function action_copy_dir() { # 目录 $from_dir = $this->arguments[0]; $to_dir = $this->arguments[2]; if (!isset(File::$dir[$from_dir])) { # 目录不允许操作 $this->show_error('目录不允许操作'); } if (!isset(File::$dir[$to_dir])) { # 目录不允许操作 $this->show_error('目录不允许操作'); } # 目录 $the_from_dir = File::$dir[$from_dir] . $this->arguments[1]; $the_to_dir = File::$dir[$to_dir] . $this->arguments[3]; if (File::copy_dir($the_from_dir, $the_to_dir, $this->arguments[4])) { $this->show_success(); } else { # 记录错误日志 Core::log('system.error.dir.copy', array('form_dir' => $from_dir, 'to_dir' => $to_dir), LOG_ERR); $this->show_error('执行失败'); } }
/** * 内部调用移动目录 * */ public function action_copy_dir() { # 目录 $from_dir = $this->arguments[0]; $to_dir = $this->arguments[2]; if (!isset(File::$dir[$from_dir])) { # 目录不允许操作 $this->show_error('目录不允许操作'); } if (!isset(File::$dir[$to_dir])) { # 目录不允许操作 $this->show_error('目录不允许操作'); } # 目录 $the_from_dir = File::$dir[$from_dir] . $this->arguments[1]; $the_to_dir = File::$dir[$to_dir] . $this->arguments[3]; if (File::copy_dir($the_from_dir, $the_to_dir, $this->arguments[4])) { $this->show_success(); } else { # 记录错误日志 Core::log('copy dir(' . $from_dir . ')to dir(' . $to_dir . ') error.', 'error'); $this->show_error('执行失败'); } }
/** * Generate database documentation * * @param $dir string Documentation directory * @return mixed (true or error message) */ public function generate($dir, $force = false) { /** * delete and create dbdocs dir */ if (file_exists($dir)) { if ($force === false) { return "{$dir} already exist, please use -f option to force delete and generate."; } $ret = \File::delete_dir($dir); if ($ret === false) { return "Could not delete directory \"{$dir}\""; } } $ret = mkdir($dir, 0777, true); if ($ret === false) { return "Could not create directory \"{$dir}\""; } /** * copy assets */ \File::copy_dir(__DIR__ . DS . '..' . DS . 'assets', $dir . 'assets'); /** * get tables and views */ $tables = $this->get_tables(); $views = $this->get_views(); foreach ($tables as $table) { $this->set_fuel_relations($table); } /** * generate index.html */ $html = \ViewModel::forge('dbdocs/index')->set('information', $this->get_information())->set('description', $this->config['description'])->set('__tables', $tables)->set('__views', $views)->render(); \File::create($dir, 'index.html', $html); /** * generate indexes.html */ $html = \ViewModel::forge('dbdocs/indexes')->set('__tables', $tables)->render(); \File::create($dir, 'indexes.html', $html); /** * generate tables.html */ $html = \ViewModel::forge('dbdocs/tables')->set('__tables', $tables)->render(); \File::create($dir, 'tables.html', $html); /** * generate table_*.html */ foreach ($tables as $table) { /* @var $table \Doctrine\DBAL\Schema\Table */ $html = \ViewModel::forge('dbdocs/table')->set('__table', $table)->render(); \File::create($dir, 'table_' . $table->getName() . '.html', $html); } /** * generate views.html */ $html = \ViewModel::forge('dbdocs/views')->set('__views', $views)->render(); \File::create($dir, 'views.html', $html); /** * generate view_*.html */ foreach ($views as $view) { /* @var $view \Doctrine\DBAL\Schema\View */ $html = \ViewModel::forge('dbdocs/view')->set('__view', $view)->render(); \File::create($dir, 'view_' . $view->getName() . '.html', $html); } return true; }
/** * This will copy the template app into the app dir overwriting what is there. * Cannot be undone * @return bool */ public static function copyAppTemplate() { // classes if (is_dir(APPPATH . 'classes')) { \File::delete_dir(APPPATH . 'classes'); } \File::copy_dir(CMFPATH . 'templates/app/classes', APPPATH . 'classes'); // config if (is_dir(APPPATH . 'config')) { \File::delete_dir(APPPATH . 'config'); } \File::copy_dir(CMFPATH . 'templates/app/config', APPPATH . 'config'); // views if (is_dir(APPPATH . 'views')) { \File::delete_dir(APPPATH . 'views'); } \File::copy_dir(CMFPATH . 'templates/app/views', APPPATH . 'views'); return true; }
/** * 内部调用移动目录 * */ public function action_copy_dir() { # 目录 $from_dir = $this->arguments[0]; $to_dir = $this->arguments[2]; if (!isset(\File::$dir[$from_dir])) { # 目录不允许操作 static::show_error('目录不允许操作'); } if (!isset(\File::$dir[$to_dir])) { # 目录不允许操作 static::show_error('目录不允许操作'); } # 目录 $the_from_dir = \File::$dir[$from_dir] . $this->arguments[1]; $the_to_dir = \File::$dir[$to_dir] . $this->arguments[3]; if (\File::copy_dir($the_from_dir, $the_to_dir, $this->arguments[4])) { static::show_message('success', null, 1); } else { # 记录错误日志 \Core::log('copy dir(' . $from_dir . ')to dir(' . $to_dir . ') error.', 'error'); static::show_error('执行失败'); } }