Skip to content

Commit af060a6

Browse files
authored
Merge pull request #6 from dicoding-dev/feature/backport-some-blade-directives
Backport some blade directives
2 parents 33c59bf + f9a43a7 commit af060a6

File tree

4 files changed

+390
-121
lines changed

4 files changed

+390
-121
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,16 @@ public function startExceptionHandling()
227227
/**
228228
* Get or check the current application environment.
229229
*
230-
* @param mixed
231-
* @return string
230+
* @param string|array
231+
* @return string|bool
232232
*/
233-
public function environment()
233+
public function environment(...$environments)
234234
{
235-
if (count(func_get_args()) > 0)
235+
if (count($environments) > 0)
236236
{
237-
return in_array($this['env'], func_get_args());
237+
$environments = is_array($environments[0]) ? $environments[0] : $environments;
238+
239+
return in_array($this['env'], $environments, true);
238240
}
239241

240242
return $this['env'];

src/Illuminate/View/Compilers/BladeCompiler.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Illuminate\View\Compilers;
22

33
use Closure;
4+
use Illuminate\Support\Str;
45

56
class BladeCompiler extends Compiler implements CompilerInterface {
67

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

624+
/**
625+
* @param string $expression
626+
* @return string
627+
*/
628+
protected function compileSelected($expression)
629+
{
630+
return "<?php if{$expression}: echo 'selected'; endif; ?>";
631+
}
632+
633+
/**
634+
* @param string $expression
635+
* @return string
636+
*/
637+
protected function compileChecked($expression)
638+
{
639+
return "<?php if{$expression}: echo 'checked'; endif; ?>";
640+
}
641+
642+
/**
643+
* @param string $expression
644+
* @return string
645+
*/
646+
protected function compileDisabled($expression)
647+
{
648+
return "<?php if{$expression}: echo 'disabled'; endif; ?>";
649+
}
650+
651+
/**
652+
* @param string $expression
653+
* @return string
654+
*/
655+
protected function compileClass($expression)
656+
{
657+
return "class=\"<?php echo e(\Illuminate\Support\Arr::toCssClasses{$expression}); ?>\"";
658+
}
659+
660+
/**
661+
* @param string $expression
662+
* @return string
663+
*/
664+
protected function compileJson($expression)
665+
{
666+
$safeEncodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
667+
$data = $this->stripParentheses($expression);
668+
669+
return "<?php echo json_encode({$data}, {$safeEncodingOptions}, 512) ?>";
670+
}
671+
672+
/**
673+
* @param string $expression
674+
* @return string
675+
*/
676+
protected function compileEnv($expression)
677+
{
678+
return "<?php if (app()->environment{$expression}): ?>";
679+
}
680+
681+
/**
682+
* @param string $expression
683+
* @return string
684+
*/
685+
protected function compileEndenv($expression)
686+
{
687+
return '<?php endif; ?>';
688+
}
689+
690+
/**
691+
* @param string $expression
692+
* @return string
693+
*/
694+
protected function compileProduction($expression)
695+
{
696+
return "<?php if (app()->environment('production')): ?>";
697+
}
698+
699+
/**
700+
* @param string $expression
701+
* @return string
702+
*/
703+
protected function compileEndproduction($expression)
704+
{
705+
return '<?php endif; ?>';
706+
}
707+
708+
/**
709+
* @param string $expression
710+
* @return string
711+
*/
712+
protected function compileAuth($expression)
713+
{
714+
return "<?php if (\Auth::check()): ?>";
715+
}
716+
717+
/**
718+
* @param string $expression
719+
* @return string
720+
*/
721+
protected function compileEndauth($expression)
722+
{
723+
return '<?php endif; ?>';
724+
}
725+
726+
/**
727+
* @param string $expression
728+
* @return string
729+
*/
730+
protected function compileGuest($expression)
731+
{
732+
return "<?php if (\Auth::guest()): ?>";
733+
}
734+
735+
/**
736+
* @param string $expression
737+
* @return string
738+
*/
739+
protected function compileEndguest($expression)
740+
{
741+
return '<?php endif; ?>';
742+
}
743+
623744
/**
624745
* Register a custom Blade compiler.
625746
*
@@ -724,4 +845,18 @@ protected function getTags($escaped = false)
724845
return array_map('stripcslashes', $tags);
725846
}
726847

848+
/**
849+
* Strip the parentheses from the given expression.
850+
*
851+
* @param string $expression
852+
* @return string
853+
*/
854+
public function stripParentheses($expression)
855+
{
856+
if (Str::startsWith($expression, '(')) {
857+
$expression = substr($expression, 1, -1);
858+
}
859+
860+
return $expression;
861+
}
727862
}

tests/Foundation/FoundationApplicationTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ public function testHandleRespectsCatchArgument()
143143
);
144144
}
145145

146+
147+
public function testEnvironment()
148+
{
149+
$app = new Application;
150+
$app['env'] = 'foo';
151+
152+
$this->assertSame('foo', $app->environment());
153+
154+
$this->assertTrue($app->environment('foo'));
155+
$this->assertTrue($app->environment('foo', 'bar'));
156+
$this->assertTrue($app->environment(['foo', 'bar']));
157+
158+
$this->assertFalse($app->environment('qux'));
159+
$this->assertFalse($app->environment('qux', 'bar'));
160+
$this->assertFalse($app->environment(['qux', 'bar']));
161+
}
146162
}
147163

148164
class ApplicationCustomExceptionHandlerStub extends Illuminate\Foundation\Application {

0 commit comments

Comments
 (0)