*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use ReflectionMethod;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\DomainObject\AbstractValueObject;
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* Copies from TYPO3 core, and modified to our needs.
*
* This class is a backport of the corresponding class of TYPO3 Flow.
* All credits go to the TYPO3 Flow team.
*
* A debugging utility class
*/
class DebuggerUtility
{
const PLAINTEXT_INDENT = ' ';
const HTML_INDENT = ' ';
/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
*/
protected static $renderedObjects;
/**
* Hardcoded list of Extbase class names (regex) which should not be displayed during debugging
*
* @var array
*/
protected static $blacklistedClassNames = [
'PHPUnit_Framework_MockObject_InvocationMocker',
ReflectionService::class,
ObjectManager::class,
DataMapper::class,
PersistenceManager::class,
QueryObjectModelFactory::class,
ContentObjectRenderer::class
];
/**
* Hardcoded list of property names (regex) which should not be displayed during debugging
*
* @var array
*/
protected static $blacklistedPropertyNames = ['warning'];
/**
* Is set to TRUE once the CSS file is included in the current page to prevent double inclusions of the CSS file.
*
* @var bool
*/
protected static $stylesheetEchoed = false;
/**
* Defines the max recursion depth of the dump, set to 8 due to common memory limits
*
* @var int
*/
protected static $maxDepth = 8;
/**
* Clear the state of the debugger
*/
protected static function clearState(): void
{
self::$renderedObjects = new ObjectStorage();
}
/**
* Renders a dump of the given value
*
* @param mixed $value
* @param int $level
* @param bool $plainText
* @param bool $ansiColors
* @return string
*/
protected static function renderDump($value, int $level, bool $plainText, bool $ansiColors): string
{
$dump = '';
if (is_string($value)) {
$croppedValue = strlen($value) > 2000 ? substr($value, 0, 2000) . '...' : $value;
if ($plainText) {
$dump = self::ansiEscapeWrap('"' . implode(PHP_EOL . str_repeat(self::PLAINTEXT_INDENT, $level + 1), str_split($croppedValue, 76)) . '"', '33', $ansiColors) . ' (' . strlen($value) . ' chars)';
} else {
$lines = str_split($croppedValue, 76);
$lines = array_map(static function (string $line): string {
return htmlspecialchars($line, ENT_COMPAT);
}, $lines);
$dump = sprintf('\'%s\' (%s chars)', implode('
' . str_repeat(self::HTML_INDENT, $level + 1), $lines), strlen($value));
}
} elseif (is_numeric($value)) {
$dump = sprintf('%s (%s)', self::ansiEscapeWrap((string)$value, '35', $ansiColors), gettype($value));
} elseif (is_bool($value)) {
$dump = $value ? self::ansiEscapeWrap('TRUE', '32', $ansiColors) : self::ansiEscapeWrap('FALSE', '32', $ansiColors);
} elseif ($value === null || is_resource($value)) {
$dump = gettype($value);
} elseif (is_array($value)) {
$dump = self::renderArray($value, $level + 1, $plainText, $ansiColors);
} elseif (is_object($value)) {
if ($value instanceof \Closure) {
$dump = self::renderClosure($value, $level + 1, $plainText, $ansiColors);
} else {
$dump = self::renderObject($value, $level + 1, $plainText, $ansiColors);
}
}
return $dump;
}
/**
* Renders a dump of the given array
*
* @param array $array
* @param int $level
* @param bool $plainText
* @param bool $ansiColors
* @return string
*/
protected static function renderArray(array $array, int $level, bool $plainText = false, bool $ansiColors = false): string
{
$content = '';
$count = count($array);
if ($plainText) {
$header = self::ansiEscapeWrap('array', '36', $ansiColors);
} else {
$header = 'array';
}
$header .= $count > 0 ? '(' . $count . ' item' . ($count > 1 ? 's' : '') . ')' : '(empty)';
if ($level >= self::$maxDepth) {
if ($plainText) {
$header .= ' ' . self::ansiEscapeWrap('max depth', '47;30', $ansiColors);
} else {
$header .= 'max depth';
}
} else {
$content = self::renderCollection($array, $level, $plainText, $ansiColors);
if (!$plainText) {
$header = ($level > 1 && $count > 0 ? '