diff --git a/lib/Doctrine/Common/Util/Debug.php b/lib/Doctrine/Common/Util/Debug.php index 9dc536de6..36940b0fc 100644 --- a/lib/Doctrine/Common/Util/Debug.php +++ b/lib/Doctrine/Common/Util/Debug.php @@ -56,66 +56,58 @@ final class Debug ini_set('html_errors', 'On'); if (extension_loaded('xdebug')) { - $dump = self::_dumpWithXDebug($var, $maxDepth); - } else { - $dump = self::_dumpWithReflection($var, $maxDepth); - - ob_start(); - var_dump($dump); - $dump = ob_get_contents(); - ob_end_clean(); + ini_set('xdebug.var_display_max_depth', $maxDepth); } - echo strip_tags(html_entity_decode($dump)); - - ini_set('html_errors', 'Off'); - } - - - private static function _dumpWithXDebug($var, $maxDepth) - { - ini_set('xdebug.var_display_max_depth', $maxDepth); + $var = self::_dumpWithReflection($var, $maxDepth++); ob_start(); var_dump($var); $dump = ob_get_contents(); ob_end_clean(); - return $dump; + echo strip_tags(html_entity_decode($dump)); + + ini_set('html_errors', 'Off'); } private static function _dumpWithReflection($var, $maxDepth) { - $disallowedDumpableClasses = array( - 'Doctrine\ORM\EntityManager', - ); + $return = null; + $isObj = is_object($var); - $reflClass = new \ReflectionClass(get_class($var)); - $arr = array(); - - foreach ($reflClass->getProperties() as $reflProperty) { - $reflProperty->setAccessible(true); - $value = $reflProperty->getValue($var); - - if ($maxDepth) { - if (is_object($value)) { - if (in_array('Doctrine\Common\Collections\Collection', class_implements($value))) { - $value = $value->toArray(); - - foreach ($value as $k => $v) { - $value[$k] = self::_dumpWithReflection($v, $maxDepth - 1); - } - } else if (in_array(get_class($value), $disallowedDumpableClasses)) { - $value = get_class($value); - } + if ($isObj && in_array('Doctrine\Common\Collections\Collection', class_implements($var))) { + $var = $var->toArray(); + } + + if ($maxDepth) { + if (is_array($var)) { + $return = array(); + + foreach ($var as $k => $v) { + $return[$k] = self::_dumpWithReflection($v, $maxDepth - 1); + } + } else if ($isObj) { + $reflClass = new \ReflectionClass(get_class($var)); + $return = new \stdclass(); + $return->{'__CLASS__'} = get_class($var); + + foreach ($reflClass->getProperties() as $reflProperty) { + $reflProperty->setAccessible(true); + + $name = $reflProperty->getName(); + $value = $reflProperty->getValue($var); + + $return->$name = self::_dumpWithReflection($value, $maxDepth - 1); } } else { - $value = is_object($value) ? get_class($value) : $value; + $return = $var; } - - $arr[$reflProperty->getName()] = $value; + } else { + $return = is_object($var) ? get_class($var) + : (is_array($var) ? 'Array(' . count($var) . ')' : $var); } - - return $arr; + + return $return; } } diff --git a/lib/Doctrine/ORM/Tools/Cli.php b/lib/Doctrine/ORM/Tools/Cli.php index 08b3c38b8..7f0f383b6 100644 --- a/lib/Doctrine/ORM/Tools/Cli.php +++ b/lib/Doctrine/ORM/Tools/Cli.php @@ -84,10 +84,11 @@ class Cli $ns = 'Doctrine\ORM\Tools\Cli\Tasks'; $this->addTasks(array( - 'help' => $ns . '\HelpTask', - 'version' => $ns . '\VersionTask', + 'help' => $ns . '\HelpTask', + 'version' => $ns . '\VersionTask', 'schema-tool' => $ns . '\SchemaToolTask', - 'run-sql' => $ns . '\RunSqlTask' + 'run-sql' => $ns . '\RunSqlTask', + 'run-dql' => $ns . '\RunDqlTask', )); } @@ -151,10 +152,7 @@ class Cli $processedArgs = $this->_processArguments($args); try { - $this->_printer->writeln( - 'Doctrine Command Line Interface', - 'HEADER' - ); + $this->_printer->writeln('Doctrine Command Line Interface', 'HEADER'); // Handle possible multiple tasks on a single command foreach($processedArgs as $taskData) { @@ -170,7 +168,10 @@ class Cli $task->setPrinter($this->_printer); $task->setArguments($taskArguments); - if (isset($taskArguments['help']) && $taskArguments['help']) { + if ( + (isset($taskArguments['help']) && $taskArguments['help']) || + (isset($taskArguments['h']) && $taskArguments['h']) + ) { $task->extendedHelp(); // User explicitly asked for help option } else if ($this->_isTaskValid($task)) { $task->run(); @@ -237,6 +238,8 @@ class Cli */ private function _processArguments($args = array()) { + $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE; + $regex = '/\s*[,]?\s*"([^"]*)"|\s*[,]?\s*([^,]*)/i'; $preparedArgs = array(); $out = & $preparedArgs; @@ -252,16 +255,22 @@ class Cli // --bar=baz } else { $key = substr($arg, 2, $eqPos - 2); - $value = explode(',', substr($arg, $eqPos + 1)); - $out[$key] = (count($value) > 1) ? $value : $value[0]; + $value = substr($arg, $eqPos + 1); + $value = (strpos($value, ' ') !== false) ? $value + : array_values(array_filter(explode(',', $value), function ($v) { return trim($v) != ''; })); + $out[$key] = ( ! is_array($value) || (is_array($value) && count($value) > 1)) + ? $value : $value[0]; } // -k=value -abc } else if (substr($arg, 0, 1) == '-'){ // -k=value if (substr($arg, 2, 1) == '='){ $key = substr($arg, 1, 1); - $value = explode(',', substr($arg, 3)); - $out[$key] = (count($value) > 1) ? $value : $value[0]; + $value = substr($arg, 3); + $value = (strpos($value, ' ') !== false) ? $value + : array_values(array_filter(explode(',', $value), function ($v) { return trim($v) != ''; })); + $out[$key] = ( ! is_array($value) || (is_array($value) && count($value) > 1)) + ? $value : $value[0]; // -abc } else { $chars = str_split(substr($arg, 1)); diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/RunDqlTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/RunDqlTask.php new file mode 100644 index 000000000..9857d233f --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/RunDqlTask.php @@ -0,0 +1,114 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli\Tasks; + +use Doctrine\Common\DoctrineException, + Doctrine\Common\Util\Debug; + +/** + * Task for executing DQL in passed EntityManager. + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @version $Revision$ + * @author Guilherme Blanco + * @author Jonathan Wage + * @author Roman Borschel + */ +class RunDqlTask extends AbstractTask +{ + /** + * @inheritdoc + */ + public function extendedHelp() + { + $printer = $this->getPrinter(); + + $printer->write('Task: ')->writeln('run-dql', 'KEYWORD') + ->write('Synopsis: '); + $this->_writeSynopsis($printer); + + $printer->writeln('Description: Executes DQL in requested EntityManager.') + ->writeln('Options:') + ->write('--dql=', 'REQ_ARG') + ->writeln("\tThe DQL to execute.") + ->write(PHP_EOL) + ->write('--depth=', 'OPT_ARG') + ->writeln("\tDumping depth of Entities graph."); + } + + /** + * @inheritdoc + */ + public function basicHelp() + { + $this->_writeSynopsis($this->getPrinter()); + } + + private function _writeSynopsis($printer) + { + $printer->write('run-dql', 'KEYWORD') + ->write(' --dql=', 'REQ_ARG') + ->writeln(' --depth=', 'OPT_ARG'); + } + + /** + * @inheritdoc + */ + public function validate() + { + if ( ! parent::validate()) { + return false; + } + + $args = $this->getArguments(); + $printer = $this->getPrinter(); + + if ( ! isset($args['dql'])) { + $printer->writeln("Argument --dql must be defined.", 'ERROR'); + return false; + } + + return true; + } + + + /** + * Executes the task. + */ + public function run() + { + $args = $this->getArguments(); + + try { + $query = $this->_em->createQuery($args['dql']); + $resultSet = $query->getResult(); + + $maxDepth = isset($args['depth']) ? $args['depth'] : 7; + + Debug::dump($resultSet, $maxDepth); + } catch (\Exception $ex) { + throw new DoctrineException($ex); + } + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/RunSqlTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/RunSqlTask.php index c1cef2aa0..6a13d185f 100644 --- a/lib/Doctrine/ORM/Tools/Cli/Tasks/RunSqlTask.php +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/RunSqlTask.php @@ -1,13 +1,40 @@ . + */ namespace Doctrine\ORM\Tools\Cli\Tasks; +use Doctrine\Common\DoctrineException, + Doctrine\Common\Util\Debug; + /** * Task for executing arbitrary SQL that can come from a file or directly from * the command line. * - * @author robo - * @since 2.0 + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @version $Revision$ + * @author Guilherme Blanco + * @author Jonathan Wage + * @author Roman Borschel */ class RunSqlTask extends AbstractTask { @@ -30,7 +57,10 @@ class RunSqlTask extends AbstractTask ->write(PHP_EOL) ->write('--file=', 'REQ_ARG') ->writeln("\tThe path to the file with the SQL to execute.") - ->writeln("\t\tIf defined, --sql can not be requested on same task"); + ->writeln("\t\tIf defined, --sql can not be requested on same task") + ->write(PHP_EOL) + ->write('--depth=', 'OPT_ARG') + ->writeln("\tDumping depth of ResultSet graph."); } /** @@ -44,7 +74,8 @@ class RunSqlTask extends AbstractTask private function _writeSynopsis($printer) { $printer->write('run-sql', 'KEYWORD') - ->writeln(' (--file= | --sql=)', 'REQ_ARG'); + ->write(' (--file= | --sql=)', 'REQ_ARG') + ->writeln(' --depth=', 'OPT_ARG'); } /** @@ -78,15 +109,23 @@ class RunSqlTask extends AbstractTask { $args = $this->getArguments(); - if (isset($args['file'])) { - //TODO - } else if (isset($args['sql'])) { - if (preg_match('/^select/i', $args['sql'])) { - $stmt = $this->_em->getConnection()->execute($args['sql']); - var_dump($stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC)); - } else { - var_dump($this->_em->getConnection()->executeUpdate($args['sql'])); + try { + if (isset($args['file'])) { + //TODO + } else if (isset($args['sql'])) { + if (preg_match('/^select/i', $args['sql'])) { + $stmt = $this->_em->getConnection()->execute($args['sql']); + $resultSet = $stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC); + } else { + $resultSet = $this->_em->getConnection()->executeUpdate($args['sql']); + } + + $maxDepth = isset($args['depth']) ? $args['depth'] : 7; + + Debug::dump($resultSet, $maxDepth); } + } catch (\Exception $ex) { + throw new DoctrineException($ex); } } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/SchemaToolTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/SchemaToolTask.php index 4aa67b149..6b7675c16 100644 --- a/lib/Doctrine/ORM/Tools/Cli/Tasks/SchemaToolTask.php +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/SchemaToolTask.php @@ -2,7 +2,8 @@ namespace Doctrine\ORM\Tools\Cli\Tasks; -use Doctrine\ORM\Tools\SchemaTool, +use Doctrine\Common\DoctrineException, + Doctrine\ORM\Tools\SchemaTool, Doctrine\Common\Annotations\AnnotationReader, Doctrine\ORM\Mapping\Driver\AnnotationDriver, Doctrine\ORM\Mapping\Driver\XmlDriver, @@ -175,7 +176,7 @@ class SchemaToolTask extends AbstractTask $tool->createSchema($classes); $printer->writeln('Database schema created successfully.', 'INFO'); } catch (\Exception $ex) { - $printer->writeln($ex->getMessage(), 'ERROR'); + throw new DoctrineException($ex); } } } else if ($isDrop) { @@ -190,7 +191,7 @@ class SchemaToolTask extends AbstractTask $tool->dropSchema($classes); $printer->writeln('Database schema dropped successfully.', 'INFO'); } catch (\Exception $ex) { - $printer->writeln($ex->getMessage(), 'ERROR'); + throw new DoctrineException($ex); } } } else if ($isUpdate) { @@ -207,7 +208,7 @@ class SchemaToolTask extends AbstractTask $tool->updateSchema($classes); $printer->writeln('Database schema updated successfully.', 'INFO'); } catch (\Exception $ex) { - $printer->writeln($ex->getMessage(), 'ERROR'); + throw new DoctrineException($ex); } } }