Skip to content

[10.x] Show source content on last line when CLI dump output is greater than display height #47514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Illuminate/Foundation/Console/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ public function dumpWithSource(Data $data)
$output = (string) $this->dump($data, true);
$lines = explode("\n", $output);

$lines[0] .= $this->getDumpSourceContent();
$content = $this->getDumpSourceContent();
$lines[0] .= $content;

if (count($lines) > $this->getDisplayHeight()) {
// get index of last non-empty line
$index = array_key_last(array_filter($lines));

$lines[$index] .= $content;
}

$this->output->write(implode("\n", $lines));

Expand Down Expand Up @@ -131,4 +139,14 @@ protected function supportsColors(): bool
{
return $this->output->isDecorated();
}

/**
* Gets the height of the display.
*/
protected function getDisplayHeight(): int
{
$height = explode(' ', @exec('stty size 2>/dev/null'))[0];

return (int) ($height ?: 50);
}
}
35 changes: 35 additions & 0 deletions tests/Foundation/Console/CliDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Foundation\Console;

use Illuminate\Foundation\Console\CliDumper;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
Expand Down Expand Up @@ -208,6 +209,40 @@ public function testUnresolvableLine()
$this->assertSame($expected, $output);
}

public function testSourceContentOnLastLineWhenOutputGreaterThanDisplayHeight()
{
$output = new BufferedOutput();
$mock = m::mock(CliDumper::class.'[getDisplayHeight]', [
$output,
'/my-work-directory',
'/my-work-directory/storage/framework/views',
])->shouldAllowMockingProtectedMethods()
->shouldReceive('getDisplayHeight')->andReturn(5)
->getMock();

$cloner = tap(new VarCloner())->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);

$mock->dumpWithSource($cloner->cloneVar(['string', 1, 1.1, ['string', 1, 1.1]]));

$output = $output->fetch();

$expected = <<<'EOF'
array:4 [ // app/routes/console.php:18
0 => "string"
1 => 1
2 => 1.1
3 => array:3 [
0 => "string"
1 => 1
2 => 1.1
]
] // app/routes/console.php:18

EOF;

$this->assertSame($expected, $output);
}

protected function dump($value)
{
$output = new BufferedOutput();
Expand Down