/**
  * Call a Phing task.
  *
  * @param string $taskName   A Propel task name.
  * @param array  $properties An array of properties to pass to Phing.
  */
 protected function callPhing($taskName, $properties = array())
 {
     $kernel = $this->getApplication()->getKernel();
     if (isset($properties['propel.schema.dir'])) {
         $this->cacheDir = $properties['propel.schema.dir'];
     } else {
         $this->cacheDir = $kernel->getCacheDir() . '/propel';
         $filesystem = new Filesystem();
         $filesystem->remove($this->cacheDir);
         $filesystem->mkdir($this->cacheDir);
     }
     $this->copySchemas($kernel, $this->cacheDir);
     // build.properties
     $this->createBuildPropertiesFile($kernel, $this->cacheDir . '/build.properties');
     // buildtime-conf.xml
     $this->createBuildTimeFile($this->cacheDir . '/buildtime-conf.xml');
     // Verbosity
     $bufferPhingOutput = $this->getContainer()->getParameter('kernel.debug');
     // Phing arguments
     $args = $this->getPhingArguments($kernel, $this->cacheDir, $properties);
     // Add any arbitrary arguments last
     foreach ($this->additionalPhingArgs as $arg) {
         if (in_array($arg, array('verbose', 'debug'))) {
             $bufferPhingOutput = false;
         }
         $args[] = '-' . $arg;
     }
     $args[] = $taskName;
     // Enable output buffering
     Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w')));
     Phing::setErrorStream(new \OutputStream(fopen('php://output', 'w')));
     Phing::startup();
     Phing::setProperty('phing.home', getenv('PHING_HOME'));
     ob_start();
     $phing = new Phing();
     $returnStatus = true;
     // optimistic way
     try {
         $phing->execute($args);
         $phing->runBuild();
         $this->buffer = ob_get_contents();
         // Guess errors
         if (strstr($this->buffer, 'failed. Aborting.') || strstr($this->buffer, 'Failed to execute') || strstr($this->buffer, 'failed for the following reason:')) {
             $returnStatus = false;
         }
     } catch (\Exception $e) {
         $returnStatus = false;
     }
     if ($bufferPhingOutput) {
         ob_end_clean();
     } else {
         ob_end_flush();
     }
     return $returnStatus;
 }
Esempio n. 2
0
 /**
  * Call a Phing task.
  *
  * @param string $taskName  A Propel task name.
  * @param array $properties An array of properties to pass to Phing.
  */
 protected function callPhing($taskName, $properties = array())
 {
     $kernel = $this->getApplication()->getKernel();
     $filesystem = new Filesystem();
     if (isset($properties['propel.schema.dir'])) {
         $this->tmpDir = $properties['propel.schema.dir'];
     } else {
         $this->tmpDir = sys_get_temp_dir() . '/propel-gen';
         $filesystem->remove($this->tmpDir);
         $filesystem->mkdir($this->tmpDir);
     }
     foreach ($kernel->getBundles() as $bundle) {
         if (is_dir($dir = $bundle->getPath() . '/Resources/config')) {
             $finder = new Finder();
             $schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
             $parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
             $length = count(explode('\\', $bundle->getNamespace())) * -1;
             $prefix = implode('.', array_slice($parts, 1, $length));
             foreach ($schemas as $schema) {
                 $tempSchema = md5($schema) . '_' . $schema->getBaseName();
                 $this->tempSchemas[$tempSchema] = array('bundle' => $bundle->getName(), 'basename' => $schema->getBaseName(), 'path' => $schema->getPathname());
                 $file = $this->tmpDir . DIRECTORY_SEPARATOR . $tempSchema;
                 $filesystem->copy((string) $schema, $file);
                 // the package needs to be set absolute
                 // besides, the automated namespace to package conversion has not taken place yet
                 // so it needs to be done manually
                 $database = simplexml_load_file($file);
                 if (isset($database['package'])) {
                     $database['package'] = $prefix . '.' . $database['package'];
                 } elseif (isset($database['namespace'])) {
                     $database['package'] = $prefix . '.' . str_replace('\\', '.', $database['namespace']);
                 } else {
                     throw new \RuntimeException(sprintf('Please define a `package` attribute or a `namespace` attribute for schema `%s`', $schema->getBaseName()));
                 }
                 foreach ($database->table as $table) {
                     if (isset($table['package'])) {
                         $table['package'] = $prefix . '.' . $table['package'];
                     } elseif (isset($table['namespace'])) {
                         $table['package'] = $prefix . '.' . str_replace('\\', '.', $table['namespace']);
                     } else {
                         $table['package'] = $database['package'];
                     }
                 }
                 file_put_contents($file, $database->asXML());
             }
         }
     }
     // build.properties
     $this->buildPropertiesFile = $kernel->getRootDir() . '/config/propel.ini';
     $filesystem->touch($this->buildPropertiesFile);
     $filesystem->copy($this->buildPropertiesFile, $this->tmpDir . '/build.properties');
     // Required by the Phing task
     $this->createBuildTimeFile($this->tmpDir . '/buildtime-conf.xml');
     $args = array();
     $properties = array_merge(array('propel.database' => 'mysql', 'project.dir' => $this->tmpDir, 'propel.output.dir' => $kernel->getRootDir() . '/propel', 'propel.php.dir' => '/', 'propel.packageObjectModel' => true), $properties);
     $properties = array_merge($properties, $this->getContainer()->get('propel.build_properties')->getProperties());
     foreach ($properties as $key => $value) {
         $args[] = "-D{$key}={$value}";
     }
     // Build file
     $args[] = '-f';
     $args[] = realpath($this->getContainer()->getParameter('propel.path') . '/generator/build.xml');
     $bufferPhingOutput = !$this->getContainer()->getParameter('kernel.debug');
     // Add any arbitrary arguments last
     foreach ($this->additionalPhingArgs as $arg) {
         if (in_array($arg, array('verbose', 'debug'))) {
             $bufferPhingOutput = false;
         }
         $args[] = '-' . $arg;
     }
     $args[] = $taskName;
     // enable output buffering
     Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w')));
     Phing::setErrorStream(new \OutputStream(fopen('php://output', 'w')));
     Phing::startup();
     Phing::setProperty('phing.home', getenv('PHING_HOME'));
     ob_start();
     $phing = new Phing();
     $returnStatus = true;
     // optimistic way
     try {
         $phing->execute($args);
         $phing->runBuild();
         $this->buffer = ob_get_contents();
         $returnStatus = false !== preg_match('#failed. Aborting.#', $this->buffer);
     } catch (Exception $e) {
         $returnStatus = false;
     }
     if ($bufferPhingOutput) {
         ob_end_clean();
     } else {
         ob_end_flush();
     }
     chdir($kernel->getRootDir());
     return $returnStatus;
 }