-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb71b2c
commit 59cc1c9
Showing
6 changed files
with
314 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# 认证 | ||
|
||
Blade支持在模板文件中判断用户是否登录,具体使用的指令为`@auth`和`@guest`,它们的用法如下: | ||
|
||
|
||
```php | ||
@auth('guardname') | ||
@elseauth('guardname') | ||
@endauth | ||
|
||
@guest('guardname') | ||
@elseguest('guardname') | ||
@endguest | ||
``` | ||
|
||
对应的实现为: | ||
|
||
|
||
```php | ||
//src/View/Compilers/Concerns/CompilesConditionals.php | ||
|
||
/** | ||
* Compile the if-auth statements into valid PHP. | ||
* | ||
* @param string|null $guard | ||
* @return string | ||
*/ | ||
protected function compileAuth($guard = null) | ||
{ | ||
$guard = is_null($guard) ? '()' : $guard; | ||
|
||
return "<?php if(auth()->guard{$guard}->check()): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the else-auth statements into valid PHP. | ||
* | ||
* @param string|null $guard | ||
* @return string | ||
*/ | ||
protected function compileElseAuth($guard = null) | ||
{ | ||
$guard = is_null($guard) ? '()' : $guard; | ||
|
||
return "<?php elseif(auth()->guard{$guard}->check()): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the end-auth statements into valid PHP. | ||
* | ||
* @return string | ||
*/ | ||
protected function compileEndAuth() | ||
{ | ||
return '<?php endif; ?>'; | ||
} | ||
|
||
/** | ||
* Compile the if-guest statements into valid PHP. | ||
* | ||
* @param string|null $guard | ||
* @return string | ||
*/ | ||
protected function compileGuest($guard = null) | ||
{ | ||
$guard = is_null($guard) ? '()' : $guard; | ||
|
||
return "<?php if(auth()->guard{$guard}->guest()): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the else-guest statements into valid PHP. | ||
* | ||
* @param string|null $guard | ||
* @return string | ||
*/ | ||
protected function compileElseGuest($guard = null) | ||
{ | ||
$guard = is_null($guard) ? '()' : $guard; | ||
|
||
return "<?php elseif(auth()->guard{$guard}->guest()): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the end-guest statements into valid PHP. | ||
* | ||
* @return string | ||
*/ | ||
protected function compileEndGuest() | ||
{ | ||
return '<?php endif; ?>'; | ||
} | ||
``` | ||
|
||
这里的实现非常简单,就不再啰嗦了。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 助手函数 | ||
|
||
Blade支持一些助手函数指令:`@csrf`,`@dd($foo)`,`@dump($bar)`,`@method('DELETE')` | ||
|
||
它们对应的用法可以参考文档,而对应的实现也很简单,都是在对应的助手函数基础上封装而成的。 | ||
|
||
```php | ||
//src/View/Compilers/Concerns/CompilesHelpers.php | ||
|
||
/** | ||
* Compile the CSRF statements into valid PHP. | ||
* | ||
* @return string | ||
*/ | ||
protected function compileCsrf() | ||
{ | ||
return '<?php echo csrf_field(); ?>'; | ||
} | ||
|
||
/** | ||
* Compile the "dd" statements into valid PHP. | ||
* | ||
* @param string $arguments | ||
* @return string | ||
*/ | ||
protected function compileDd($arguments) | ||
{ | ||
return "<?php dd{$arguments}; ?>"; | ||
} | ||
|
||
/** | ||
* Compile the "dump" statements into valid PHP. | ||
* | ||
* @param string $arguments | ||
* @return string | ||
*/ | ||
protected function compileDump($arguments) | ||
{ | ||
return "<?php dump{$arguments}; ?>"; | ||
} | ||
|
||
/** | ||
* Compile the method statements into valid PHP. | ||
* | ||
* @param string $method | ||
* @return string | ||
*/ | ||
protected function compileMethod($method) | ||
{ | ||
return "<?php echo method_field{$method}; ?>"; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# isset | ||
|
||
为了方便在模板中判断变量是否存在,Blade提供了`@isset`指令,用法如下: | ||
|
||
```php | ||
@isset($foo) | ||
@else | ||
@endisset | ||
``` | ||
|
||
实现如下: | ||
|
||
|
||
```php | ||
//src/View/Compilers/Concerns/CompilesConditionals.php | ||
|
||
/** | ||
* Compile the if-isset statements into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileIsset($expression) | ||
{ | ||
return "<?php if(isset{$expression}): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the end-isset statements into valid PHP. | ||
* | ||
* @return string | ||
*/ | ||
protected function compileEndIsset() | ||
{ | ||
return '<?php endif; ?>'; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# JSON | ||
|
||
Blade支持`@json`指令,将PHP数组转换为JSON字符串,用法如下: | ||
|
||
```php | ||
@json(['foo' => 'bar']) | ||
``` | ||
|
||
对应的实现如下: | ||
|
||
|
||
```php | ||
//src/View/Compilers/Concerns/CompilersJson.php | ||
|
||
/** | ||
* Compile the JSON statement into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileJson($expression) | ||
{ | ||
$parts = explode(',', $this->stripParentheses($expression)); | ||
|
||
$options = isset($parts[1]) ? trim($parts[1]) : $this->encodingOptions; | ||
|
||
$depth = isset($parts[2]) ? trim($parts[2]) : 512; | ||
|
||
return "<?php echo json_encode($parts[0], $options, $depth) ?>"; | ||
} | ||
``` | ||
|
||
从`compileJson`可以看出文档未记录的用法: | ||
|
||
```php | ||
@json(['foo' => 'bar'], JSON_HEX_TAG, 512) | ||
``` | ||
|
||
后面两个参数对应的是`json_encode`函数后面的两个参数 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# 权限 | ||
|
||
Blade支持在模板文件中判断用户是否具有某些权限,所用到的指令有`@can`和`@cannot`,用法如下: | ||
|
||
```php | ||
@can('update', $post) | ||
@elsecan('create', App\Post::class) | ||
@endcan | ||
|
||
@cannot('update', $post) | ||
@elsecannot('create', App\Post::class) | ||
@endcannot | ||
``` | ||
|
||
具体的实现如下: | ||
|
||
```php | ||
//src/View/Compilers/Concerns/CompilesAuthorizations.php | ||
|
||
/** | ||
* Compile the can statements into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileCan($expression) | ||
{ | ||
return "<?php if (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->check{$expression}): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the cannot statements into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileCannot($expression) | ||
{ | ||
return "<?php if (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->denies{$expression}): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the else-can statements into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileElsecan($expression) | ||
{ | ||
return "<?php elseif (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->check{$expression}): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the else-cannot statements into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileElsecannot($expression) | ||
{ | ||
return "<?php elseif (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->denies{$expression}): ?>"; | ||
} | ||
|
||
/** | ||
* Compile the end-can statements into valid PHP. | ||
* | ||
* @return string | ||
*/ | ||
protected function compileEndcan() | ||
{ | ||
return '<?php endif; ?>'; | ||
} | ||
|
||
/** | ||
* Compile the end-cannot statements into valid PHP. | ||
* | ||
* @return string | ||
*/ | ||
protected function compileEndcannot() | ||
{ | ||
return '<?php endif; ?>'; | ||
} | ||
``` | ||
|
||
具体的逻辑都放到了`Gate`这个类里面,这里先不作分析。 |