Пример #1
0
 /**
  * Cli method
  *
  * @param CLImate $climate
  *
  * @throws \Rad\Core\Exception\MissingBundleException
  */
 public function cliMethod(CLImate $climate)
 {
     $sqlDir = VAR_DIR . DS . 'cake_orm' . DS . 'sql';
     if (!is_dir($sqlDir)) {
         mkdir($sqlDir, 0777, true);
     }
     $sql = [];
     foreach (Bundles::getLoaded() as $bundle) {
         $climate->backgroundLightGray()->info(sprintf('Bundle %s ...', $bundle));
         $path = Bundles::getPath($bundle);
         $sqlFilename = Inflection::underscore($bundle) . '.sql';
         $schemaPath = $path . DS . 'Resource' . DS . 'config' . DS . 'schema.xml';
         if (!is_file($schemaPath)) {
             $climate->lightRed(sprintf('File "%s" does not exists.', $schemaPath));
             $climate->br(2);
             continue;
         }
         $domDocument = new DOMDocument();
         $domDocument->load($schemaPath);
         $xpath = new DOMXPath($domDocument);
         $tables = $xpath->query('/database/table');
         /** @var DOMElement $table */
         foreach ($tables as $table) {
             $schemaTable = new Table($table->getAttribute('name'));
             $this->prepareTableRegistry($table, $bundle);
             $this->prepareColumn($table, $xpath, $schemaTable);
             $this->prepareForeignConstraint($table, $xpath, $schemaTable);
             $this->prepareUniqueConstraint($table, $xpath, $schemaTable);
             $this->preparePrimaryConstraint($table, $xpath, $schemaTable);
             $this->prepareIndex($table, $xpath, $schemaTable);
             $sql[$sqlDir . DS . $sqlFilename][] = $schemaTable->createSql(ConnectionManager::get('default'));
         }
         foreach ($sql as $file => $tablesSql) {
             $tmpSql = '';
             foreach ($tablesSql as $tableSql) {
                 $tmpSql .= implode(";\n", $tableSql);
                 $tmpSql .= "\n\n";
             }
             file_put_contents($file, $tmpSql);
             $climate->lightGray(sprintf('Create SQL file "%s".', $file));
         }
         $sql = [];
         $climate->info('Dump table registry config ...');
         $this->dumpTableRegistry($climate, $bundle);
         $climate->info('Dump model classes ...');
         $this->dumpModelClasses($climate, $bundle);
         $climate->br(2);
     }
 }