Skip to content
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
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 static $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)
{
static::$echoHandlers[$className] = $handler;
}
}
13 changes: 13 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesEchos.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ protected function compileRawEchos($value)
$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

$matches[2] = $this->applyEchoHandlerFor($matches[2]);

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

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

$matches[2] = $this->applyEchoHandlerFor($matches[2]);

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

return $matches[1] ? substr($matches[0], 1) : "<?php echo {$wrapped}; ?>{$whitespace}";
Expand All @@ -91,4 +95,13 @@ protected function compileEscapedEchos($value)

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

protected function applyEchoHandlerFor($data)
{
$echoHandlerArray = static::class . "::\$echoHandlers";

return "is_object($data) && isset({$echoHandlerArray}[get_class($data)])
? call_user_func_array({$echoHandlerArray}[get_class($data)], [$data])
: $data";
}
}
41 changes: 41 additions & 0 deletions tests/View/Blade/BladeEchoHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Illuminate\Tests\View\Blade;

use Illuminate\Support\Fluent;

class BladeEchoHandlerTest extends AbstractBladeTestCase
{
public function testBladeHandlersCanBeAddedForAGivenClass()
{
$this->compiler->handle(Fluent::class, function($object) {
return "Hello World";
});

$this->assertSame('Hello World', $this->compiler::$echoHandlers[Fluent::class](new Fluent()));
}

public function testBladeHandlerCanInterceptEscapedEchos()
{
$echoHandlerArray = get_class($this->compiler) . "::\$echoHandlers";

$this->assertSame(
"<?php echo e(is_object(\$exampleObject) && isset({$echoHandlerArray}[get_class(\$exampleObject)])
? call_user_func_array({$echoHandlerArray}[get_class(\$exampleObject)], [\$exampleObject])
: \$exampleObject); ?>",
$this->compiler->compileString('{{$exampleObject}}')
);
}

public function testBladeHandlerCanInterceptRawEchos()
{
$echoHandlerArray = get_class($this->compiler) . "::\$echoHandlers";

$this->assertSame(
"<?php echo is_object(\$exampleObject) && isset({$echoHandlerArray}[get_class(\$exampleObject)])
? call_user_func_array({$echoHandlerArray}[get_class(\$exampleObject)], [\$exampleObject])
: \$exampleObject; ?>",
$this->compiler->compileString('{!!$exampleObject!!}')
);
}
}