/** * Cria uma nova referencia de conexao com o banco * * @author Hugo Ferreira da Silva * @link http://www.hufersil.com.br/ * @param string $connectionName Nome da conexao * @param Lumine_Configuration $config Objeto de configuracao * @return void */ public function create($connectionName, Lumine_Configuration $config) { if ($this->getConnection($connectionName) != false) { Lumine_Log::warning('Ja existe uma conexao com este nome: ' . $connectionName); } else { Lumine_Log::debug('Armazenando conexao: ' . $connectionName); $connObj = $this->getConnectionClass($config->options['dialect']); if ($connObj == false) { Lumine_Log::error('Dialeto nao implementado: ' . $config->options['dialect']); return; } $connObj->setDatabase($config->options['database']); $connObj->setHost($config->options['host']); $connObj->setPort($config->options['port']); $connObj->setUser($config->options['user']); $connObj->setPassword($config->options['password']); if (isset($config->options['options'])) { $connObj->setOptions($config->options['options']); } if ($config->getOption('charset') != '') { $connObj->setCharset($config->getOption('charset')); } $config->setConnection($connObj); $this->connections[$connectionName] = $config; } }
/** * retona a classe criada * * @author Hugo Ferreira da Silva * @link http://www.hufersil.com.br * @param string $tablename * @param Lumine_Configuration $cfg * @return Lumine_Factory */ public static function create($tablename, Lumine_Configuration $cfg) { $pkg = $cfg->getProperty('package'); Lumine_Log::debug('Recuperando campos da tabela ' . $tablename); $fields = $cfg->getConnection()->describe($tablename); $obj = new Lumine_Factory($pkg, $tablename); foreach ($fields as $item) { list($name, $type_native, $type, $length, $primary, $notnull, $default, $autoincrement) = $item; $options = array('primary' => $primary, 'notnull' => $notnull, 'autoincrement' => $autoincrement); // para o pg, ainda tem o nome da sequence if (!empty($item[8]['sequence'])) { $options['sequence'] = $item[8]['sequence']; } // se tiver um valor padrao if (!empty($default)) { $options['default'] = $default; } // nome do membro $memberName = $name; // se for para usar camel case if ($cfg->getOption('camel_case') == true) { $memberName = Lumine_Util::camelCase($memberName); } $obj->metadata()->addField($memberName, $name, $type, $length, $options); } return $obj; }
/** * Gera os arquivos * @author Hugo Ferreira da Silva * @link http://www.hufersil.com.br/ * @param boolean $overwrite Forca a sobrescrita nos arquivos * @return void */ private function generateFiles($overwrite) { Lumine_Log::debug('Gerando arquivos direto na pasta'); $fullpath = $this->cfg->getProperty('class_path') . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $this->cfg->getProperty('package')); $sufix = $this->cfg->getOption('class_sufix'); if (!empty($sufix)) { $sufix = '.' . $sufix; } $dummy = new Lumine_Reverse_ClassTemplate(); $end = $dummy->getEndDelim(); if (!file_exists($fullpath) && $this->cfg->getOption('create_paths') == 1) { mkdir($fullpath, 0777, true) or die('Não foi possivel criar o diretorio: ' . $fullpath); } reset($this->files); foreach ($this->files as $classname => $content) { $filename = $fullpath . DIRECTORY_SEPARATOR . $classname . $sufix . '.php'; if (file_exists($filename) && empty($overwrite)) { $fp = fopen($filename, 'r'); $old_content = fread($fp, filesize($filename)); fclose($fp); $start = strpos($old_content, $end) + strlen($end); $customized = substr($old_content, $start); $top = substr($content, 0, strpos($content, $end)); $content = $top . $end . $customized; } $fp = @fopen($filename, 'w'); if ($fp) { fwrite($fp, $content); fclose($fp); chmod($filename, 0777); Lumine_Log::debug('Arquivo para a classe ' . $classname . ' gerado com sucesso'); } else { Lumine_Log::error('O PHP nao tem direito de escrita na pasta "' . $fullpath . '". Verifique se o diretario existe e se o PHP tem direito de escrita.'); exit; } } //// cria os dtos if ($this->cfg->getOption('create_dtos')) { reset($this->dtos); // pasta raiz dos DTO's $path = $this->cfg->getProperty('class_path') . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $this->cfg->getProperty('package')) . DIRECTORY_SEPARATOR . 'dto'; // para cada DTO foreach ($this->dtos as $obj) { $fullpath = $path . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $obj->getPackage()); // se o diretorio nao existe, tenta criar if (!is_dir($fullpath) && $this->cfg->getOption('create_paths') == 1) { mkdir($fullpath, 0777, true) or die('Não foi possivel criar o diretorio: ' . $fullpath); } $filename = $fullpath . DIRECTORY_SEPARATOR . $obj->getClassname() . $sufix . '.php'; file_put_contents($filename, $obj->getContent()); } } // models foreach ($this->models as $item) { Lumine_Log::debug('Criando Model ' . $item->getClassname()); $filename = $item->getFullFileName(); if (!is_dir(dirname($filename)) && $this->cfg->getOption('create_paths') == 1) { $path = dirname($filename); mkdir($path, 0777, true) or die('Não foi possivel criar o diretorio: ' . $path); } else { if (!is_dir(dirname($filename))) { $path = dirname($filename); Lumine_Log::error('Nao eh possivel gravar em ' . $path . '. Verifique se a pasta existe e se ha permissao de gravacao'); } } $content = $item->getContent(); file_put_contents($filename, $content); chmod($filename, 0777); } // copia o arquivo de contexto if ($this->cfg->getOption('model_context') == 1) { $contextFile = LUMINE_INCLUDE_PATH . '/lib/Templates/ApplicationContext.php'; if (file_exists($contextFile)) { $path = $this->cfg->getProperty('class_path') . DIRECTORY_SEPARATOR . $this->cfg->getOption('model_context_path') . DIRECTORY_SEPARATOR; if (!is_dir($path)) { if ($this->cfg->getOption('create_paths') == 1) { mkdir($path, 0777, true) or die('Não foi possivel criar o diretorio ' . $path); } else { Lumine_Log::error('Nao foi possivel gravar o contexto na pasta ' . $path . '. Verifique se a pasta existe.'); } } $destino = $path . 'Lumine_ApplicationContext.php'; // so copiamos se o arquivo nao existir if (!file_exists($destino)) { Lumine_Log::debug('Copiando arquivo de contexto: ' . $destino); copy($contextFile, $destino); chmod($path . 'Lumine_ApplicationContext.php', 0777); // ja existe, nao copaimos mas avisamos } else { Lumine_Log::debug('O arquivo "' . $destino . '" ja existe'); } } } // escreve os controles $path = $this->cfg->getProperty('class_path'); $path .= DIRECTORY_SEPARATOR . 'controls' . DIRECTORY_SEPARATOR; if (!file_exists($path) && $this->cfg->getOption('create_paths') == 1) { mkdir($path, 0777, true) or die('Nao foi possivel criar o diretorio: ' . $path); } foreach ($this->controls as $classname => $content) { $filename = $path . $classname . '.php'; $fp = @fopen($filename, 'w'); if (!$fp) { Lumine_Log::error('O PHP nao tem direito de escrita para gerar o arquivo "' . $filename . '". Verifique se o diretorio existe e se o PHP tem direito de escrita.'); exit; } else { fwrite($fp, $content); fclose($fp); Lumine_Log::debug('Arquivo de controle "' . $filename . '" gerado com sucesso.'); } } // copia os demais arquivos if (!empty($this->controls) && $this->cfg->getOption('create_controls') != '') { $class = 'Lumine_Form_' . $this->cfg->getOption('create_controls'); $ref = new ReflectionClass($class); $instance = $ref->newInstance(null); $instance->copyFiles($path); } // escreve o arquivo de configuracao $filename = $this->cfg->getProperty('class_path') . DIRECTORY_SEPARATOR . 'lumine-conf.php'; $fp = @fopen($filename, 'w'); if (!$fp) { Lumine_Log::error('O PHP nao tem direito de escrita para gerar o arquivo "' . $filename . '". Verifique se o diretorio existe e se o PHP tem direito de escrita.'); exit; } fwrite($fp, $this->config); fclose($fp); Lumine_Log::debug('Arquivo "' . $filename . '" gerado com sucesso.'); }