1
0
mirror of synced 2025-01-29 19:41:45 +03:00

[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.

This commit is contained in:
guilhermeblanco 2009-09-08 20:13:09 +00:00
parent 7aabee5efc
commit db5a48e5f4
5 changed files with 227 additions and 72 deletions

View File

@ -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;
}
}

View File

@ -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));

View File

@ -0,0 +1,114 @@
<?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);
}
}
}

View File

@ -1,13 +1,40 @@
<?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 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 <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class RunSqlTask extends AbstractTask
{
@ -30,7 +57,10 @@ class RunSqlTask extends AbstractTask
->write(PHP_EOL)
->write('--file=<path>', '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=<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=<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
{
$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);
}
}
}

View File

@ -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);
}
}
}