Skip to content

Commit

Permalink
Merge pull request #6 from dicoding-dev/feature/backport-some-blade-d…
Browse files Browse the repository at this point in the history
…irectives

Backport some blade directives
  • Loading branch information
rizqyhi authored Aug 15, 2023
2 parents 33c59bf + f9a43a7 commit af060a6
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 121 deletions.
12 changes: 7 additions & 5 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,16 @@ public function startExceptionHandling()
/**
* Get or check the current application environment.
*
* @param mixed
* @return string
* @param string|array
* @return string|bool
*/
public function environment()
public function environment(...$environments)
{
if (count(func_get_args()) > 0)
if (count($environments) > 0)
{
return in_array($this['env'], func_get_args());
$environments = is_array($environments[0]) ? $environments[0] : $environments;

return in_array($this['env'], $environments, true);
}

return $this['env'];
Expand Down
135 changes: 135 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Illuminate\View\Compilers;

use Closure;
use Illuminate\Support\Str;

class BladeCompiler extends Compiler implements CompilerInterface {

Expand Down Expand Up @@ -620,6 +621,126 @@ protected function compileEndpush($expression)
return "<?php \$__env->appendSection(); ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileSelected($expression)
{
return "<?php if{$expression}: echo 'selected'; endif; ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileChecked($expression)
{
return "<?php if{$expression}: echo 'checked'; endif; ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileDisabled($expression)
{
return "<?php if{$expression}: echo 'disabled'; endif; ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileClass($expression)
{
return "class=\"<?php echo e(\Illuminate\Support\Arr::toCssClasses{$expression}); ?>\"";
}

/**
* @param string $expression
* @return string
*/
protected function compileJson($expression)
{
$safeEncodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
$data = $this->stripParentheses($expression);

return "<?php echo json_encode({$data}, {$safeEncodingOptions}, 512) ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileEnv($expression)
{
return "<?php if (app()->environment{$expression}): ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileEndenv($expression)
{
return '<?php endif; ?>';
}

/**
* @param string $expression
* @return string
*/
protected function compileProduction($expression)
{
return "<?php if (app()->environment('production')): ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileEndproduction($expression)
{
return '<?php endif; ?>';
}

/**
* @param string $expression
* @return string
*/
protected function compileAuth($expression)
{
return "<?php if (\Auth::check()): ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileEndauth($expression)
{
return '<?php endif; ?>';
}

/**
* @param string $expression
* @return string
*/
protected function compileGuest($expression)
{
return "<?php if (\Auth::guest()): ?>";
}

/**
* @param string $expression
* @return string
*/
protected function compileEndguest($expression)
{
return '<?php endif; ?>';
}

/**
* Register a custom Blade compiler.
*
Expand Down Expand Up @@ -724,4 +845,18 @@ protected function getTags($escaped = false)
return array_map('stripcslashes', $tags);
}

/**
* Strip the parentheses from the given expression.
*
* @param string $expression
* @return string
*/
public function stripParentheses($expression)
{
if (Str::startsWith($expression, '(')) {
$expression = substr($expression, 1, -1);
}

return $expression;
}
}
16 changes: 16 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ public function testHandleRespectsCatchArgument()
);
}


public function testEnvironment()
{
$app = new Application;
$app['env'] = 'foo';

$this->assertSame('foo', $app->environment());

$this->assertTrue($app->environment('foo'));
$this->assertTrue($app->environment('foo', 'bar'));
$this->assertTrue($app->environment(['foo', 'bar']));

$this->assertFalse($app->environment('qux'));
$this->assertFalse($app->environment('qux', 'bar'));
$this->assertFalse($app->environment(['qux', 'bar']));
}
}

class ApplicationCustomExceptionHandlerStub extends Illuminate\Foundation\Application {
Expand Down
Loading

0 comments on commit af060a6

Please sign in to comment.