/**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     try {
         $args = $this->preparePhingArgs($input, $output);
         $phingClasspath = $this->getContainer()->getParameter('rhapsody_phing.phing_classpath');
         $phingHome = $this->getContainer()->getParameter('rhapsody_phing.phing_home');
         if ($output->getVerbosity() == OutputInterface::VERBOSITY_VERBOSE) {
             $commandline = 'phing ' . implode(' ', $args);
             $output->writeln('Executing Phing with: ' . $commandline);
         }
         //require_once('phing/Phing.php');
         \Phing::startup();
         \Phing::setProperty('rhapsody_phing.home', $phingHome);
         \Phing::fire($args);
         \Phing::shutdown();
     } catch (ConfigurationException $x) {
         Phing::printMessage($x);
         exit(-1);
     } catch (Exception $x) {
         exit(1);
     }
 }
예제 #2
0
파일: phing.php 프로젝트: umesecke/phing
    ini_set('include_path', PHP_CLASSPATH);
} else {
    if (!defined('PHP_CLASSPATH')) {
        define('PHP_CLASSPATH', get_include_path());
    }
}
require_once 'phing/Phing.php';
try {
    /* Setup Phing environment */
    Phing::startup();
    // Set phing.home property to the value from environment
    // (this may be NULL, but that's not a big problem.)
    Phing::setProperty('phing.home', getenv('PHING_HOME'));
    // Grab and clean up the CLI arguments
    $args = isset($argv) ? $argv : $_SERVER['argv'];
    // $_SERVER['argv'] seems to not work (sometimes?) when argv is registered
    array_shift($args);
    // 1st arg is script name, so drop it
    // Invoke the commandline entry point
    Phing::fire($args);
    // Invoke any shutdown routines.
    Phing::shutdown();
} catch (ConfigurationException $x) {
    Phing::printMessage($x);
    exit(-1);
    // This was convention previously for configuration errors.
} catch (Exception $x) {
    // Assume the message was already printed as part of the build and
    // exit with non-0 error code.
    exit(1);
}
예제 #3
0
 /**
  * Run propel phing commands
  *
  * @param string $cmd	phing target
  * @param array $argv arguments
  * @return string
  */
 public static function propelGen($cmd = '', $argv = array())
 {
     $autoloader = App::getInstance()->autoloader;
     $generatorBase = dirname(dirname(dirname($autoloader->findFile('AbstractPropelDataModelTask'))));
     $buildXml = $generatorBase . '/build.xml';
     $projectPath = \Curry\App::getInstance()['projectPath'] . '/propel';
     $argv[] = '-logger';
     $argv[] = 'phing.listener.AnsiColorLogger';
     $argv[] = '-f';
     $argv[] = $buildXml;
     $argv[] = '-Dproject.dir=' . $projectPath;
     if ($cmd) {
         $argv[] = $cmd;
     }
     $cwd = getcwd();
     $stream = fopen("php://temp", 'r+');
     $outputStream = new OutputStream($stream);
     Phing::setOutputStream($outputStream);
     Phing::setErrorStream($outputStream);
     Phing::startup();
     Phing::fire($argv);
     rewind($stream);
     $content = stream_get_contents($stream);
     Phing::shutdown();
     chdir($cwd);
     if (extension_loaded('apc')) {
         @apc_clear_cache();
     }
     return $content;
 }
  protected function getModels($databaseManager, $verbose = false)
  {
    Phing::startup(); // required to locate behavior classes...
    Phing::shutdown(); // restores error_reporting
    $schemas = sfFinder::type('file')
      ->name('*schema.xml')
      ->follow_link()
      ->in(sfConfig::get('sf_config_dir'));
    if (!$schemas)
    {
      throw new sfCommandException('You must create a schema.yml or schema.xml file.');
    }
    $ads = array();

    foreach ($schemas as $schema)
    {
      if ($verbose)
      {
        $this->logSection('schema', sprintf('  Parsing schema "%s"', $schema), null, 'COMMENT');
      }
      $dom = new DomDocument('1.0', 'UTF-8');
      $dom->load($schema);
      //$this->includeExternalSchemas($dom, sfConfig::get('sf_config_dir'));
      $xmlParser = new XmlToAppData(new DefaultPlatform(), '');
      $generatorConfig = $this->getGeneratorConfig();
      $generatorConfig->setBuildConnections($this->getConnections($databaseManager));
      $xmlParser->setGeneratorConfig($generatorConfig);

      $ad = $xmlParser->parseString($dom->saveXML(), $schema);
      $ads[] = $ad;
      $nbTables = $ad->getDatabase(null, false)->countTables();
      if ($verbose)
      {
        $this->logSection('schema', sprintf('  %d tables processed successfully', $nbTables), null, 'COMMENT');
      }
    }
    if (count($ads)>1) {
      $ad = array_shift($ads);
      $ad->joinAppDatas($ads);
      //$ad = $this->joinDataModels($ads);
      //$this->dataModels = array($ad);
    } else {
      $ad = $ads[0];
    }
    $ad->doFinalInitialization();
    return $ad;
  }
  /**
   * @see sfTask
   */
  protected function execute($arguments = array(), $options = array())
  {
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connections = $this->getConnections($databaseManager);

    $this->logSection('propel', 'Reading databases structure...');
    $ad = new AppData();
    $totalNbTables = 0;
    foreach ($connections as $name => $params)
    {
      if ($options['verbose'])
      {
        $this->logSection('propel', sprintf('  Connecting to database "%s" using DSN "%s"', $name, $params['dsn']), null, 'COMMENT');
      }
      $pdo = $databaseManager->getDatabase($name)->getConnection();
      $database = new Database($name);
      $platform = $this->getPlatform($databaseManager, $name);
      $database->setPlatform($platform);
      $database->setDefaultIdMethod(IDMethod::NATIVE);
      $parser = $this->getParser($databaseManager, $name, $pdo);
      $parser->setMigrationTable($options['migration-table']);
      $parser->setPlatform($platform);
      $nbTables = $parser->parse($database);
      $ad->addDatabase($database);
      $totalNbTables += $nbTables;
      if ($options['verbose'])
      {
        $this->logSection('propel', sprintf('  %d tables imported from database "%s"', $nbTables, $name), null, 'COMMENT');
      }
    }
    if ($totalNbTables) {
      $this->logSection('propel', sprintf('%d tables imported from databases.', $totalNbTables));
    } else {
      $this->logSection('propel', 'Database is empty');
    }

    $this->logSection('propel', 'Loading XML schema files...');
    Phing::startup(); // required to locate behavior classes...
    Phing::shutdown(); // restores error_reporting
    $this->schemaToXML(self::DO_NOT_CHECK_SCHEMA, 'generated-');
    $this->copyXmlSchemaFromPlugins('generated-');
    $appData = $this->getModels($databaseManager, $options['verbose']);
    $this->logSection('propel', sprintf('%d tables defined in the schema files.', $appData->countTables()));
    $this->cleanup($options['verbose']);

    if ($excludePatterns = $appData->getGeneratorConfig()->getBuildProperty('migrationExcludes'))
    {
      $excludePatterns = array_map('trim', explode(',', $excludePatterns));
      $excludePatterns = array_map(array('sfGlobToRegex', 'glob_to_regex'), $excludePatterns);

      foreach (array_merge($ad->getDatabases(), $appData->getDatabases()) as $database)
      {
        foreach ($database->getTables() as $table)
        {
          foreach ($excludePatterns as $pattern)
          {
            if (preg_match($pattern, $table->getName()))
            {
              $table->setSkipSql(true);
            }
          }
        }
      }
    }

    $this->logSection('propel', 'Comparing databases and schemas...');
    $manager = new PropelMigrationManager();
    $manager->setConnections($connections);
    $migrationsUp = array();
    $migrationsDown = array();
    foreach ($ad->getDatabases() as $database) {
      $name = $database->getName();
      if ($options['verbose'])
      {
        $this->logSection('propel', sprintf('  Comparing database "%s"', $name), null, 'COMMENT');
      }

      if (!$appData->hasDatabase($name)) {
        // FIXME: tables present in database but not in XML
        continue;
      }
      $databaseDiff = PropelDatabaseComparator::computeDiff($database, $appData->getDatabase($name));

      if (!$databaseDiff) {
        if($options['verbose']) {
          $this->logSection('propel', sprintf('  Same XML and database structures for datasource "%s" - no diff to generate', $name), null, 'COMMENT');
        }
        continue;
      }

      $this->logSection('propel', sprintf('Structure of database was modified in datasource "%s": %s', $name, $databaseDiff->getDescription()));
      if ($options['verbose'])
      {
        $this->logBlock($databaseDiff, 'COMMENT');
      }
      $platform = $this->getPlatform($databaseManager, $name);
      $migrationsUp[$name] = $platform->getModifyDatabaseDDL($databaseDiff);
      $migrationsDown[$name] = $platform->getModifyDatabaseDDL($databaseDiff->getReverseDiff());
    }

    if (!$migrationsUp)
    {
      $this->logSection('propel', 'Same XML and database structures for all datasources - no diff to generate');
      return;
    }

    $timestamp = time();
    $migrationDirectory = sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . $options['migration-dir'];
    $migrationFileName = $manager->getMigrationFileName($timestamp);
    $migrationFilePath = $migrationDirectory . DIRECTORY_SEPARATOR . $migrationFileName;
    if (
      $options['ask-confirmation']
      &&
      !$this->askConfirmation(array(
      sprintf('Migration class will be generated in %s', $migrationFilePath),
      'Are you sure you want to proceed? (Y/n)'
    ), 'QUESTION_LARGE', true))
    {
      $this->logSection('propel', 'Task aborted.');
      return 1;
    }
    $this->getFilesystem()->mkdirs($migrationDirectory);
    $migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp);
    file_put_contents($migrationFilePath, $migrationClassBody);
    $this->logSection('propel', sprintf('"%s" file successfully created in %s', $migrationFileName, $migrationDirectory));

    if ($editorCmd = $options['editor-cmd'])
    {
      $this->logSection('propel', sprintf('Using "%s" as text editor', $editorCmd));
      shell_exec($editorCmd . ' ' . escapeshellarg($migrationFilePath));
    }
    else
    {
      $this->logSection('propel', '  Please review the generated SQL statements, and add data migration code if necessary.');
      $this->logSection('propel', '  Once the migration class is valid, call the "propel:migrate" task to execute it.');
    }
  }