Laravel dd 功能限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39011241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel dd function limitations
提问by vitr
I have an array of 320 arrays, while regular var_dump
shows me exactly 320 elements with all nested elements, Laravel's dd
helper truncates the nested element at index 147and all the further elements are truncated with no option to expand them, see the example below
我有一个包含 320 个数组的数组,而常规只var_dump
显示 320 个元素和所有嵌套元素,Laravel 的dd
助手会截断索引147处的嵌套元素,并且所有其他元素都被截断而无法展开它们,请参见下面的示例
146 => array:17 [▼
"total_unconfirmed_subscribers" => 0
"total_subscribers_subscribed_yesterday" => 0
"unique_list_id" => "24324"
"http_etag" => ""fbb6febfca8af5541541ea960aaedb""
"web_form_split_tests_collection_link" => "https://api.com/1.0/"
"subscribers_collection_link" => "https://api.com/1.0/"
"total_subscribers_subscribed_today" => 0
"id" => 23432
"total_subscribed_subscribers" => 0
"total_unsubscribed_subscribers" => 0
"campaigns_collection_link" => "https://api.com/1.0/"
"custom_fields_collection_link" => "https://api.com/1.0/accounts"
"self_link" => "https://api.com/1.0/accounts"
"total_subscribers" => 0
"resource_type_link" => "https://api.com/1.0/#list"
"web_forms_collection_link" => "https://api.com/"
"name" => "dccode"
]
147 => array:17 [▼
"total_unconfirmed_subscribers" => 0
…16
]
148 => array:17 [ …17]
149 => array:17 [ …17]
Why is it limited to 147 full records and how to increase the limit? The related topic Is Laravels' DD helper function working properly?doesn't actually explain the limits.
为什么限制为147个完整记录以及如何增加限制?相关主题Laravel 的 DD 助手功能是否正常工作?实际上并没有解释限制。
This is pretty consistent behavior, I've tested with Laravel 5.2 and php7 on
这是非常一致的行为,我已经在 Laravel 5.2 和 php7 上进行了测试
- Linux (Laravel Forge, DO droplet, Ubuntu)
- Mac (Laravel Valet)
- Windows (valet4windows)
- Linux(Laravel Forge、DO droplet、Ubuntu)
- Mac (Laravel Valet)
- 窗户 (valet4windows)
Everywhere got exactly the same cut on element #147. Using CLI php artisan tinker
outputs the same cut
元素#147 的所有地方都得到了完全相同的剪辑。使用 CLIphp artisan tinker
输出相同的剪辑
...
"name" => "dccode" ] 147 => array:17 [
"total_unconfirmed_subscribers" => 0
??a16 ] 148 => array:17 [ ??a17]
...
回答by sleepless
Prior to version 5.0 laravel's dd()
function looked as follows:
在 5.0 版本之前,laravel 的dd()
函数如下所示:
function dd()
{
array_map(function($x) { var_dump($x); }, func_get_args()); die;
}
Since 5.0 it looks like this:
从 5.0 开始,它看起来像这样:
function dd()
{
array_map(function ($x) {
(new Dumper)->dump($x);
}, func_get_args());
die(1);
}
The Dumper
is using symfony's VarCloner
which is extending the AbstractCloner
. This class has a $maxItems
attribute set to 2500. See: https://github.com/symfony/var-dumper/blob/master/Cloner/AbstractCloner.php#L125
该Dumper
是用symfony的的VarCloner
这是延长AbstractCloner
。该类的$maxItems
属性设置为 2500。参见:https: //github.com/symfony/var-dumper/blob/master/Cloner/AbstractCloner.php#L125
You have 17 items per array. Multiply it by 147 and you get 2499. That's why your array at key 147 is truncated after it's first item.
每个数组有 17 个项目。将它乘以 147 得到 2499。这就是为什么键 147 处的数组在第一项之后被截断的原因。
If you'd like to increase that you'd need to override laravel's Dumper
class (https://github.com/laravel/framework/blob/5.2/src/Illuminate/Support/Debug/Dumper.php):
如果你想增加你需要覆盖 laravel 的Dumper
类(https://github.com/laravel/framework/blob/5.2/src/Illuminate/Support/Debug/Dumper.php):
public function dump($value)
{
if (class_exists(CliDumper::class)) {
$dumper = 'cli' === PHP_SAPI ? new CliDumper : new HtmlDumper;
$cloner = new VarCloner();
$cloner->setMaxItems(5000);
$dumper->dump($cloner->cloneVar($value));
} else {
var_dump($value);
}
}
回答by Phantom1412
My suggestions for you is to create a custom helpers file in bootstrap folder
我对您的建议是在 bootstrap 文件夹中创建一个自定义帮助文件
1) follows the following answer to create a custom helper function for your Laravel application, if it is too complicated, you can skip the following step, use my function in step 2 as any normal function and simply call it >> https://stackoverflow.com/a/28290359/10539212
1) 按照下面的回答为你的 Laravel 应用程序创建一个自定义的辅助函数,如果它太复杂,你可以跳过下面的步骤,使用我在步骤 2 中的函数作为任何普通函数,只需调用它 >> https:// stackoverflow.com/a/28290359/10539212
2) I would like to give some credits to this guy, I follow his guide to create my own ddd function >> https://tighten.co/blog/a-better-dd-for-your-tdd
2)我想给这个人一些功劳,我按照他的指南来创建我自己的 ddd 函数 >> https://tighten.co/blog/a-better-dd-for-your-tdd
use Illuminate\Support\Debug\HtmlDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
function ddd()
{
$args = func_get_args();
$defaultStringLength = -1;
$defaultItemNumber = -1;
$defaultDepth = -1;
foreach ($args as $variable) {
$dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
$cloner = new VarCloner();
$cloner->setMaxString($defaultStringLength);
$cloner->setMaxItems($defaultItemNumber);
$dumper->dump($cloner->cloneVar($variable)->withMaxDepth($defaultDepth));
}
die(1);
}
-1 = no limit (easy to customize in this way)
-1 = 无限制(以这种方式易于自定义)
So, now when you use this ddd function, you can get the complete output of normal dd function. I think this approach is better than overwriting any existing function. But be careful, since now variables with any number of depth will be displayed completely, you may experience longer loading time. Hope it helps.
所以,现在当你使用这个 ddd 函数时,你可以得到普通 dd 函数的完整输出。我认为这种方法比覆盖任何现有功能要好。但要小心,因为现在任何深度的变量都将被完整显示,你可能会遇到更长的加载时间。希望能帮助到你。
回答by Lucas Martins
My suggestion is you add a handlerin the VarDumpercomponent.
我的建议是在VarDumper组件中添加一个处理程序。
In your AppServiceProvider.php:
在您的AppServiceProvider.php 中:
(before class
declaration)
(class
申报前)
use Symfony\Component\VarDumper\VarDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
(inside boot()
method)
(内部boot()
方法)
VarDumper::setHandler(function ($var) {
$cloner = new VarCloner();
$cloner->setMaxItems(-1); // Specifying -1 removes the limit
$dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
$dumper->dump($cloner->cloneVar($var));
});
According to Symfony's VarDumper component documentation:
setMaxItems()Configures the maximum number of items that will be cloned past the minimum nesting depth. Items are counted using a breadth-first algorithm so that lower level items have higher priority than deeply nested items. Specifying -1 removes the limit.
setMaxItems()配置将被克隆超过最小嵌套深度的最大项目数。项目使用广度优先算法进行计数,以便较低级别的项目比深度嵌套的项目具有更高的优先级。指定 -1 将取消限制。
And in the documentation you can see other methods for customizing the Cloners, Dumpers and Casters components.
在文档中,您可以看到自定义 Cloner、Dumpers 和 Casters 组件的其他方法。