Commit db5a48e5 authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Removed xdebug builtin support from Debug::dump(). Added run-dql CLI...

[2.0] Removed xdebug builtin support from Debug::dump(). Added run-dql CLI Task. Updated tasks to wrap Exceptions thrown and diplay nicely. Fixed bug with CLI that ws incorrectly splitting strings into an array instead of consider them raw.
parent 7aabee5e
...@@ -56,66 +56,58 @@ final class Debug ...@@ -56,66 +56,58 @@ final class Debug
ini_set('html_errors', 'On'); ini_set('html_errors', 'On');
if (extension_loaded('xdebug')) { if (extension_loaded('xdebug')) {
$dump = self::_dumpWithXDebug($var, $maxDepth); ini_set('xdebug.var_display_max_depth', $maxDepth);
} else { }
$dump = self::_dumpWithReflection($var, $maxDepth);
$var = self::_dumpWithReflection($var, $maxDepth++);
ob_start(); ob_start();
var_dump($dump); var_dump($var);
$dump = ob_get_contents(); $dump = ob_get_contents();
ob_end_clean(); ob_end_clean();
}
echo strip_tags(html_entity_decode($dump)); echo strip_tags(html_entity_decode($dump));
ini_set('html_errors', 'Off'); ini_set('html_errors', 'Off');
} }
private static function _dumpWithReflection($var, $maxDepth)
private static function _dumpWithXDebug($var, $maxDepth)
{ {
ini_set('xdebug.var_display_max_depth', $maxDepth); $return = null;
$isObj = is_object($var);
ob_start(); if ($isObj && in_array('Doctrine\Common\Collections\Collection', class_implements($var))) {
var_dump($var); $var = $var->toArray();
$dump = ob_get_contents();
ob_end_clean();
return $dump;
} }
private static function _dumpWithReflection($var, $maxDepth) if ($maxDepth) {
{ if (is_array($var)) {
$disallowedDumpableClasses = array( $return = array();
'Doctrine\ORM\EntityManager',
);
foreach ($var as $k => $v) {
$return[$k] = self::_dumpWithReflection($v, $maxDepth - 1);
}
} else if ($isObj) {
$reflClass = new \ReflectionClass(get_class($var)); $reflClass = new \ReflectionClass(get_class($var));
$arr = array(); $return = new \stdclass();
$return->{'__CLASS__'} = get_class($var);
foreach ($reflClass->getProperties() as $reflProperty) { foreach ($reflClass->getProperties() as $reflProperty) {
$reflProperty->setAccessible(true); $reflProperty->setAccessible(true);
$value = $reflProperty->getValue($var);
if ($maxDepth) { $name = $reflProperty->getName();
if (is_object($value)) { $value = $reflProperty->getValue($var);
if (in_array('Doctrine\Common\Collections\Collection', class_implements($value))) {
$value = $value->toArray();
foreach ($value as $k => $v) { $return->$name = self::_dumpWithReflection($value, $maxDepth - 1);
$value[$k] = self::_dumpWithReflection($v, $maxDepth - 1);
}
} else if (in_array(get_class($value), $disallowedDumpableClasses)) {
$value = get_class($value);
}
} }
} else { } else {
$value = is_object($value) ? get_class($value) : $value; $return = $var;
} }
} else {
$arr[$reflProperty->getName()] = $value; $return = is_object($var) ? get_class($var)
: (is_array($var) ? 'Array(' . count($var) . ')' : $var);
} }
return $arr; return $return;
} }
} }
...@@ -87,7 +87,8 @@ class Cli ...@@ -87,7 +87,8 @@ class Cli
'help' => $ns . '\HelpTask', 'help' => $ns . '\HelpTask',
'version' => $ns . '\VersionTask', 'version' => $ns . '\VersionTask',
'schema-tool' => $ns . '\SchemaToolTask', 'schema-tool' => $ns . '\SchemaToolTask',
'run-sql' => $ns . '\RunSqlTask' 'run-sql' => $ns . '\RunSqlTask',
'run-dql' => $ns . '\RunDqlTask',
)); ));
} }
...@@ -151,10 +152,7 @@ class Cli ...@@ -151,10 +152,7 @@ class Cli
$processedArgs = $this->_processArguments($args); $processedArgs = $this->_processArguments($args);
try { try {
$this->_printer->writeln( $this->_printer->writeln('Doctrine Command Line Interface', 'HEADER');
'Doctrine Command Line Interface',
'HEADER'
);
// Handle possible multiple tasks on a single command // Handle possible multiple tasks on a single command
foreach($processedArgs as $taskData) { foreach($processedArgs as $taskData) {
...@@ -170,7 +168,10 @@ class Cli ...@@ -170,7 +168,10 @@ class Cli
$task->setPrinter($this->_printer); $task->setPrinter($this->_printer);
$task->setArguments($taskArguments); $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 $task->extendedHelp(); // User explicitly asked for help option
} else if ($this->_isTaskValid($task)) { } else if ($this->_isTaskValid($task)) {
$task->run(); $task->run();
...@@ -237,6 +238,8 @@ class Cli ...@@ -237,6 +238,8 @@ class Cli
*/ */
private function _processArguments($args = array()) private function _processArguments($args = array())
{ {
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$regex = '/\s*[,]?\s*"([^"]*)"|\s*[,]?\s*([^,]*)/i';
$preparedArgs = array(); $preparedArgs = array();
$out = & $preparedArgs; $out = & $preparedArgs;
...@@ -252,16 +255,22 @@ class Cli ...@@ -252,16 +255,22 @@ class Cli
// --bar=baz // --bar=baz
} else { } else {
$key = substr($arg, 2, $eqPos - 2); $key = substr($arg, 2, $eqPos - 2);
$value = explode(',', substr($arg, $eqPos + 1)); $value = substr($arg, $eqPos + 1);
$out[$key] = (count($value) > 1) ? $value : $value[0]; $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 // -k=value -abc
} else if (substr($arg, 0, 1) == '-'){ } else if (substr($arg, 0, 1) == '-'){
// -k=value // -k=value
if (substr($arg, 2, 1) == '='){ if (substr($arg, 2, 1) == '='){
$key = substr($arg, 1, 1); $key = substr($arg, 1, 1);
$value = explode(',', substr($arg, 3)); $value = substr($arg, 3);
$out[$key] = (count($value) > 1) ? $value : $value[0]; $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 // -abc
} else { } else {
$chars = str_split(substr($arg, 1)); $chars = str_split(substr($arg, 1));
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
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 <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
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=<DQL>', 'REQ_ARG')
->writeln("\tThe DQL to execute.")
->write(PHP_EOL)
->write('--depth=<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=<DQL>', 'REQ_ARG')
->writeln(' --depth=<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
<?php <?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Cli\Tasks; 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 * Task for executing arbitrary SQL that can come from a file or directly from
* the command line. * the command line.
* *
* @author robo * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/ */
class RunSqlTask extends AbstractTask class RunSqlTask extends AbstractTask
{ {
...@@ -30,7 +57,10 @@ class RunSqlTask extends AbstractTask ...@@ -30,7 +57,10 @@ class RunSqlTask extends AbstractTask
->write(PHP_EOL) ->write(PHP_EOL)
->write('--file=<path>', 'REQ_ARG') ->write('--file=<path>', 'REQ_ARG')
->writeln("\tThe path to the file with the SQL to execute.") ->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=<DEPTH>', 'OPT_ARG')
->writeln("\tDumping depth of ResultSet graph.");
} }
/** /**
...@@ -44,7 +74,8 @@ class RunSqlTask extends AbstractTask ...@@ -44,7 +74,8 @@ class RunSqlTask extends AbstractTask
private function _writeSynopsis($printer) private function _writeSynopsis($printer)
{ {
$printer->write('run-sql', 'KEYWORD') $printer->write('run-sql', 'KEYWORD')
->writeln(' (--file=<path> | --sql=<SQL>)', 'REQ_ARG'); ->write(' (--file=<path> | --sql=<SQL>)', 'REQ_ARG')
->writeln(' --depth=<DEPTH>', 'OPT_ARG');
} }
/** /**
...@@ -78,15 +109,23 @@ class RunSqlTask extends AbstractTask ...@@ -78,15 +109,23 @@ class RunSqlTask extends AbstractTask
{ {
$args = $this->getArguments(); $args = $this->getArguments();
try {
if (isset($args['file'])) { if (isset($args['file'])) {
//TODO //TODO
} else if (isset($args['sql'])) { } else if (isset($args['sql'])) {
if (preg_match('/^select/i', $args['sql'])) { if (preg_match('/^select/i', $args['sql'])) {
$stmt = $this->_em->getConnection()->execute($args['sql']); $stmt = $this->_em->getConnection()->execute($args['sql']);
var_dump($stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC)); $resultSet = $stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC);
} else { } else {
var_dump($this->_em->getConnection()->executeUpdate($args['sql'])); $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
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
namespace Doctrine\ORM\Tools\Cli\Tasks; namespace Doctrine\ORM\Tools\Cli\Tasks;
use Doctrine\ORM\Tools\SchemaTool, use Doctrine\Common\DoctrineException,
Doctrine\ORM\Tools\SchemaTool,
Doctrine\Common\Annotations\AnnotationReader, Doctrine\Common\Annotations\AnnotationReader,
Doctrine\ORM\Mapping\Driver\AnnotationDriver, Doctrine\ORM\Mapping\Driver\AnnotationDriver,
Doctrine\ORM\Mapping\Driver\XmlDriver, Doctrine\ORM\Mapping\Driver\XmlDriver,
...@@ -175,7 +176,7 @@ class SchemaToolTask extends AbstractTask ...@@ -175,7 +176,7 @@ class SchemaToolTask extends AbstractTask
$tool->createSchema($classes); $tool->createSchema($classes);
$printer->writeln('Database schema created successfully.', 'INFO'); $printer->writeln('Database schema created successfully.', 'INFO');
} catch (\Exception $ex) { } catch (\Exception $ex) {
$printer->writeln($ex->getMessage(), 'ERROR'); throw new DoctrineException($ex);
} }
} }
} else if ($isDrop) { } else if ($isDrop) {
...@@ -190,7 +191,7 @@ class SchemaToolTask extends AbstractTask ...@@ -190,7 +191,7 @@ class SchemaToolTask extends AbstractTask
$tool->dropSchema($classes); $tool->dropSchema($classes);
$printer->writeln('Database schema dropped successfully.', 'INFO'); $printer->writeln('Database schema dropped successfully.', 'INFO');
} catch (\Exception $ex) { } catch (\Exception $ex) {
$printer->writeln($ex->getMessage(), 'ERROR'); throw new DoctrineException($ex);
} }
} }
} else if ($isUpdate) { } else if ($isUpdate) {
...@@ -207,7 +208,7 @@ class SchemaToolTask extends AbstractTask ...@@ -207,7 +208,7 @@ class SchemaToolTask extends AbstractTask
$tool->updateSchema($classes); $tool->updateSchema($classes);
$printer->writeln('Database schema updated successfully.', 'INFO'); $printer->writeln('Database schema updated successfully.', 'INFO');
} catch (\Exception $ex) { } catch (\Exception $ex) {
$printer->writeln($ex->getMessage(), 'ERROR'); throw new DoctrineException($ex);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment