From b4b9709090e2fb5919b025fa2e5a194e1013fe07 Mon Sep 17 00:00:00 2001
From: "Johannes M. Schmitt" <schmittjoh@gmail.com>
Date: Sun, 3 Mar 2013 11:01:43 +0100
Subject: [PATCH] adds a new output format

---
 .../Tools/Console/Command/RunDqlCommand.php   | 24 ++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php
index 1189b8194..c8cc50e83 100644
--- a/lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php
+++ b/lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php
@@ -25,6 +25,7 @@ use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use Doctrine\Common\Util\Debug;
+use JMS\Serializer\SerializerBuilder;
 
 /**
  * Command to execute DQL queries in a given EntityManager.
@@ -64,6 +65,11 @@ class RunDqlCommand extends Command
             new InputOption(
                 'depth', null, InputOption::VALUE_REQUIRED,
                 'Dumping depth of Entity graph.', 7
+            ),
+            new InputOption(
+                'format', null, InputOption::VALUE_REQUIRED,
+                'The output format of the result. Available formats: doctrine-debug (default), jms-serializer-json.',
+                'doctrine-debug'
             )
         ))
         ->setHelp(<<<EOT
@@ -118,6 +124,22 @@ EOT
 
         $resultSet = $query->execute(array(), constant($hydrationMode));
 
-        Debug::dump($resultSet, $input->getOption('depth'));
+        switch ($input->getOption('format')) {
+            case 'doctrine-debug':
+                ob_start();
+                Debug::dump($resultSet, $input->getOption('depth'));
+                $message = ob_get_clean();
+
+                $output->write($message);
+                break;
+
+            case 'jms-serializer-json':
+                $serializer = SerializerBuilder::create()->build();
+                $output->write($serializer->serialize($resultSet, 'json'));
+                break;
+
+            default:
+                throw new \RuntimeException(sprintf('Unknown output format "%s"; available formats: doctrine-debug, jms-serializer-json', $input->getOption('format')));
+        }
     }
 }