-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[8.x] Adds class handling for Blade echo statements #37478
Merged
taylorotwell
merged 21 commits into
laravel:8.x
from
lukeraymonddowning:echo-handler-fixed
May 28, 2021
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a017c4d
Adds start of blade class handlers.
lukeraymonddowning 3186d83
Adds basis for blade class handlers.
lukeraymonddowning c344cf1
Adds facade method.
lukeraymonddowning 1723b0f
Adds a bugfix for non-object values.
lukeraymonddowning bd09b44
Fixes tests and improves speed.
lukeraymonddowning e63eb1c
Removes static property.
lukeraymonddowning 712e7b8
Fixed styling
lukeraymonddowning 1b88972
Refactors test
lukeraymonddowning 92c78ee
Adds echo handler to escaped echos.
lukeraymonddowning 81368db
Adds support for escaped echos.
lukeraymonddowning beff2ae
Small refactor
lukeraymonddowning 9687ea2
Removes new lines from echo handling.
lukeraymonddowning 648931d
Style fix
lukeraymonddowning 5382398
Bugfix
lukeraymonddowning c9fc16b
Test fixes
lukeraymonddowning 5194080
Simplifies logic.
lukeraymonddowning abe72fb
Fixes docblock
lukeraymonddowning 39a60b6
Adds a test that actually executes the logic to test that it works co…
lukeraymonddowning 9cafc2c
Style fixes
lukeraymonddowning 2871099
Renames `handle` method
lukeraymonddowning 74be67c
formatting
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\View\Blade; | ||
|
||
use Exception; | ||
use Illuminate\Support\Fluent; | ||
use Illuminate\Support\Str; | ||
|
||
class BladeEchoHandlerTest extends AbstractBladeTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->compiler->handle(Fluent::class, function ($object) { | ||
return 'Hello World'; | ||
}); | ||
} | ||
|
||
public function testBladeHandlersCanBeAddedForAGivenClass() | ||
{ | ||
$this->assertSame('Hello World', $this->compiler->echoHandlers[Fluent::class](new Fluent())); | ||
} | ||
|
||
public function testBladeHandlerCanInterceptRegularEchos() | ||
{ | ||
$this->assertSame( | ||
"<?php echo e(is_object(\$exampleObject) && isset(app('blade.compiler')->echoHandlers[get_class(\$exampleObject)]) ? call_user_func_array(app('blade.compiler')->echoHandlers[get_class(\$exampleObject)], [\$exampleObject]) : \$exampleObject); ?>", | ||
$this->compiler->compileString('{{$exampleObject}}') | ||
); | ||
} | ||
|
||
public function testBladeHandlerCanInterceptRawEchos() | ||
{ | ||
$this->assertSame( | ||
"<?php echo is_object(\$exampleObject) && isset(app('blade.compiler')->echoHandlers[get_class(\$exampleObject)]) ? call_user_func_array(app('blade.compiler')->echoHandlers[get_class(\$exampleObject)], [\$exampleObject]) : \$exampleObject; ?>", | ||
$this->compiler->compileString('{!!$exampleObject!!}') | ||
); | ||
} | ||
|
||
public function testBladeHandlerCanInterceptEscapedEchos() | ||
{ | ||
$this->assertSame( | ||
"<?php echo e(is_object(\$exampleObject) && isset(app('blade.compiler')->echoHandlers[get_class(\$exampleObject)]) ? call_user_func_array(app('blade.compiler')->echoHandlers[get_class(\$exampleObject)], [\$exampleObject]) : \$exampleObject); ?>", | ||
$this->compiler->compileString('{{{$exampleObject}}}') | ||
); | ||
} | ||
|
||
public function testWhitespaceIsPreservedCorrectly() | ||
{ | ||
$this->assertSame( | ||
"<?php echo e(is_object(\$exampleObject) && isset(app('blade.compiler')->echoHandlers[get_class(\$exampleObject)]) ? call_user_func_array(app('blade.compiler')->echoHandlers[get_class(\$exampleObject)], [\$exampleObject]) : \$exampleObject); ?>\n\n", | ||
$this->compiler->compileString("{{\$exampleObject}}\n") | ||
); | ||
} | ||
|
||
public function testHandlerLogicWorksCorrectly() | ||
{ | ||
$this->expectExceptionMessage('The fluent object has been successfully handled!'); | ||
|
||
$this->compiler->handle(Fluent::class, function ($object) { | ||
throw new Exception('The fluent object has been successfully handled!'); | ||
}); | ||
|
||
app()->singleton('blade.compiler', function () { | ||
return $this->compiler; | ||
}); | ||
|
||
$exampleObject = new Fluent(); | ||
|
||
eval( | ||
Str::of($this->compiler->compileString('{{$exampleObject}}')) | ||
->after('<?php') | ||
->beforeLast('?>') | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if this should be
addEchoHandler
.handle
is so generic and might be confused with a job'shandle
method. The former is a registration of a resolver while the later is an actual resolver.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I've renamed.