Skip to content

Commit

Permalink
add @log directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashot Gharakeshishyan committed Aug 30, 2024
1 parent b9cf7d3 commit a77244b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
Concerns\CompilesStyles,
Concerns\CompilesTranslations,
Concerns\CompilesUseStatements,
Concerns\CompilesLog,
ReflectsClosures;

/**
Expand Down
28 changes: 28 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesLog
{
/**
* Compile the extends statements into valid PHP.
*
* @param string $expression
* @return string
*/
public function compileLog(string $expression)
{
$expression = $this->stripParentheses($expression);
$parts = array_map('trim', explode(',', $expression));
$message = isset($parts[0]) ? trim($parts[0], "'\"") : '';

$method = isset($parts[1]) ? trim($parts[1], "'\"") : 'info';
$resolvedClass = app()->make('log');

if (!method_exists($resolvedClass, $method)) {
$method = 'info';
}

return "<?php Log::{$method}('{$message}'); ?>";
}
}
19 changes: 19 additions & 0 deletions tests/Testing/BladeLogTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Tests\Testing;

use PHPUnit\Framework\TestCase;

class BladeLogTest extends TestCase
{
public function testLogAreCompiled()
{
$this->assertSame("<?php Log::info('User authenticated.'); ?>", $this->compiler->compileString('@log(\'User authenticated.\')'));
$this->assertSame("<?php Log::error('Error occurred.'); ?>", $this->compiler->compileString('@log(\'Error occurred.\', \'error\')'));
$this->assertSame("<?php Log::debug('Debug occurred.'); ?>", $this->compiler->compileString('@log(\'Debug occurred.\', \'debug\')'));
$this->assertSame("<?php Log::alert('Alert occurred.'); ?>", $this->compiler->compileString('@log(\'Alert occurred.\', \'alert\')'));
$this->assertSame("<?php Log::critical('Critical occurred.'); ?>", $this->compiler->compileString('@log(\'Critical occurred.\', \'critical\')'));
$this->assertSame("<?php Log::notice('Notice occurred.'); ?>", $this->compiler->compileString('@log(\'Notice occurred.\', \'notice\')'));
$this->assertSame("<?php Log::emergency('Emergency occurred.'); ?>", $this->compiler->compileString('@log(\'Emergency occurred.\', \'emergency\')'));
}
}

0 comments on commit a77244b

Please sign in to comment.