Skip to content

Commit

Permalink
新增模板编译内容
Browse files Browse the repository at this point in the history
  • Loading branch information
leunggamciu committed Apr 15, 2018
1 parent cb71b2c commit 59cc1c9
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
- [输出](src/view/blade/echo.md)
- [PHP](src/view/blade/php.md)
- [条件语句](src/view/blade/conditional.md)
- [isset](src/view/blade/isset.md)
- [权限](src/view/blade/permission.md)
- [认证](src/view/blade/authentication.md)
- [Include](src/view/blade/include.md)
- [继承](src/view/blade/inherit.md)
- [Stack](src/view/blade/stack.md)
- [组件](src/view/blade/component.md)
- [组件](src/view/blade/component.md)
- [json](src/view/blade/json.md)
- [助手函数](src/view/blade/helpers.md)
95 changes: 95 additions & 0 deletions src/view/blade/authentication.md
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; ?>';
}
```

这里的实现非常简单,就不再啰嗦了。
52 changes: 52 additions & 0 deletions src/view/blade/helpers.md
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}; ?>";
}
```
37 changes: 37 additions & 0 deletions src/view/blade/isset.md
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; ?>';
}
```
39 changes: 39 additions & 0 deletions src/view/blade/json.md
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`函数后面的两个参数
85 changes: 85 additions & 0 deletions src/view/blade/permission.md
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`这个类里面,这里先不作分析。

0 comments on commit 59cc1c9

Please sign in to comment.