Skip to content
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
merged 21 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 19 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
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @method static void withDoubleEncoding()
* @method static void withoutComponentTags()
* @method static void withoutDoubleEncoding()
* @method static void handle(string $className, callable $handler)
*
* @see \Illuminate\View\Compilers\BladeCompiler
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ class BladeCompiler extends Compiler implements CompilerInterface
*/
protected $classComponentNamespaces = [];

/**
* The array of handlers objects should be passed through.
*
* @var array
*/
public $echoHandlers = [];

/**
* Indicates if component tags should be compiled.
*
Expand Down Expand Up @@ -752,4 +759,14 @@ public function withoutComponentTags()
{
$this->compilesComponentTags = false;
}

/**
* Add a handler to be executed before echoing a given class.
*
* @return void
*/
public function handle(string $className, callable $handler)
Copy link
Member

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's handle method. The former is a registration of a resolver while the later is an actual resolver.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I've renamed.

{
$this->echoHandlers[$className] = $handler;
}
}
23 changes: 20 additions & 3 deletions src/Illuminate/View/Compilers/Concerns/CompilesEchos.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ protected function compileRawEchos($value)
$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

return $matches[1] ? substr($matches[0], 1) : "<?php echo {$matches[2]}; ?>{$whitespace}";
return $matches[1]
? substr($matches[0], 1)
: "<?php echo {$this->applyEchoHandlerFor($matches[2])}; ?>{$whitespace}";
};

return preg_replace_callback($pattern, $callback, $value);
Expand All @@ -65,7 +67,7 @@ protected function compileRegularEchos($value)
$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

$wrapped = sprintf($this->echoFormat, $matches[2]);
$wrapped = sprintf($this->echoFormat, $this->applyEchoHandlerFor($matches[2]));

return $matches[1] ? substr($matches[0], 1) : "<?php echo {$wrapped}; ?>{$whitespace}";
};
Expand All @@ -86,9 +88,24 @@ protected function compileEscapedEchos($value)
$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

return $matches[1] ? $matches[0] : "<?php echo e({$matches[2]}); ?>{$whitespace}";
return $matches[1]
? $matches[0]
: "<?php echo e({$this->applyEchoHandlerFor($matches[2])}); ?>{$whitespace}";
};

return preg_replace_callback($pattern, $callback, $value);
}

/**
* Wrap the value in a handler if applicable.
*
* @param string $value
* @return string
*/
protected function applyEchoHandlerFor($value)
{
return empty($this->echoHandlers)
? $value
: "is_object($value) && isset(app('blade.compiler')->echoHandlers[get_class($value)]) ? call_user_func_array(app('blade.compiler')->echoHandlers[get_class($value)], [$value]) : $value";
}
}
77 changes: 77 additions & 0 deletions tests/View/Blade/BladeEchoHandlerTest.php
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('?>')
);
}
}