Skip to content

Commit ee8968d

Browse files
committed
feat: Merge branch '5.4' of https://github.com/laravel-china/laravel-docs into lc-5.4-dusk
# Conflicts: # dusk.md
2 parents 82743cb + 9a85c48 commit ee8968d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3308
-2954
lines changed

billing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Laravel Cashier 提供了直观、流畅的接口来接入 [Stripe's](https://st
9292

9393
'stripe' => [
9494
'model' => App\User::class,
95+
'key' => env('STRIPE_KEY'),
9596
'secret' => env('STRIPE_SECRET'),
9697
],
9798

collections.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
[union](#method-union)
112112
[unique](#method-unique)
113113
[values](#method-values)
114+
[when](#method-when)
114115
[where](#method-where)
115116
[whereStrict](#method-wherestrict)
116117
[whereIn](#method-wherein)
@@ -1386,6 +1387,21 @@
13861387
1 => ['product' => 'Desk', 'price' => 200],
13871388
]
13881389
*/
1390+
<a name="method-when"></a>
1391+
#### `when()` {#collection-method}
1392+
1393+
当第一个参数运算结果为 `true` 的时候,会执行第二个参数传入的闭包:
1394+
1395+
$collection = collect([1, 2, 3]);
1396+
1397+
$collection->when(true, function ($collection) {
1398+
return $collection->push(4);
1399+
});
1400+
1401+
$collection->all();
1402+
1403+
// [1, 2, 3, 4]
1404+
13891405
<a name="method-where"></a>
13901406
#### `where()` {#collection-method}
13911407

contracts.md

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
# Laravel 的接口契约
1+
# Laravel 接口契约 Contracts
22

3-
- [Introduction](#introduction)
3+
- [简介](#introduction)
44
- [Contracts Vs. Facades](#contracts-vs-facades)
5-
- [When To Use Contracts](#when-to-use-contracts)
6-
- [Loose Coupling](#loose-coupling)
7-
- [Simplicity](#simplicity)
8-
- [How To Use Contracts](#how-to-use-contracts)
9-
- [Contract Reference](#contract-reference)
5+
- [何时使用 contracts](#when-to-use-contracts)
6+
- [低耦合](#loose-coupling)
7+
- [简单性](#simplicity)
8+
- [如何使用 Contracts](#how-to-use-contracts)
9+
- [Contract 参考](#contract-reference)
1010

1111
<a name="introduction"></a>
12-
## Introduction
12+
## 简介
1313

14-
Laravel's Contracts are a set of interfaces that define the core services provided by the framework. For example, a `Illuminate\Contracts\Queue\Queue` contract defines the methods needed for queueing jobs, while the `Illuminate\Contracts\Mail\Mailer` contract defines the methods needed for sending e-mail.
14+
LaravelContracts 是一组定义了框架核心服务的接口。例如,`Illuminate\Contracts\Queue\Queue` contract 定义了队列任务所需的方法,而 `Illuminate\Contracts\Mail\Mailer` contract 定义了发送电子邮件所需的方法。
1515

16-
Each contract has a corresponding implementation provided by the framework. For example, Laravel provides a queue implementation with a variety of drivers, and a mailer implementation that is powered by [SwiftMailer](http://swiftmailer.org/).
16+
框架对每个 contract 都提供了相应的实现。例如,Laravel 提供了一个对各种驱动程序的队列实现,以及一个由 [SwiftMailer](http://swiftmailer.org/) 驱动的邮件实现。
1717

18-
All of the Laravel contracts live in [their own GitHub repository](https://github.com/illuminate/contracts). This provides a quick reference point for all available contracts, as well as a single, decoupled package that may be utilized by package developers.
18+
所有的 Laravel contracts 存放在他们 [单独的GitHub库](https://github.com/illuminate/contracts) 。除了提供给所有可用的 contracts 一个快速的参考,还可以单独作为一个低耦合的扩展包来让其他扩展包开发者使用。
1919

2020
<a name="contracts-vs-facades"></a>
2121
### Contracts Vs. Facades
2222

23-
Laravel's [facades](/docs/{{version}}/facades) and helper functions provide a simple way of utilizing Laravel's services without needing to type-hint and resolve contracts out of the service container. In most cases, each facade has an equivalent contract.
23+
Laravel[facades](/docs/{{version}}/facades) 和辅助函数提供了一种使用 Laravel 的服务的简单方法,而不需要类型提示和解析服务容器之外的 contracts 。 在大多数情况下,每个 facade 都有一个等效 contract
2424

25-
Unlike facades, which do not require you to require them in your class' constructor, contracts allow you to define explicit dependencies for your classes. Some developers prefer to explicitly define their dependencies in this way and therefore prefer to use contracts, while other developers enjoy the convenience of facades.
25+
不像 facades 那样,contracts 需要显式定义依赖关系。 一些开发人员更喜欢以这种方式显式定义它们的依赖关系,因此更喜欢使用 contracts,而其他开发人员喜欢 facades 的方便性。
2626

27-
> {tip} Most applications will be fine regardless of whether you prefer facades or contracts. However, if you are building a package, you should strongly consider using contracts since they will be easier to test in a package context.
27+
> {tip} 大多数应用程序不论使用 facades 还是 contracts 都无所谓。 然而,如果你正在构建一个扩展包,强烈地建议使用 contracts,因为它们将更容易在包上下文中测试。
2828
2929
<a name="when-to-use-contracts"></a>
30-
## When To Use Contracts
30+
## 何时使用 contracts
3131

32-
As discussed elsewhere, much of the decision to use contracts or facades will come down to personal taste and the tastes of your development team. Both contracts and facades can be used to create robust, well-tested Laravel applications. As long as you are keeping your class' responsibilities focused, you will notice very few practical differences between using contracts and facades.
32+
正如其他地方所讨论的,使用 contracts 或 facades 很大程度上归结于个人品味和开发团队的品味。 contracts 和 facades 都可用于创建稳健、经过良好测试的 Laravel 应用程序。 只要你一直能关注类的功能点,你会发现使用 contracts 和 facades 之间很少有实际差异。
33+
34+
但是,您可能仍有几个关于 contracts 的问题。例如,为什么使用接口?是不是使用接口更复杂?使用接口的原因如下:低耦合和简单性。
3335

34-
However, you may still have several questions regarding contracts. For example, why use interfaces at all? Isn't using interfaces more complicated? Let's distill the reasons for using interfaces to the following headings: loose coupling and simplicity.
3536

3637
<a name="loose-coupling"></a>
37-
### Loose Coupling
38+
### 低耦合
3839

39-
First, let's review some code that is tightly coupled to a cache implementation. Consider the following:
40+
首先,让我们回顾一些与缓存实现紧密耦合的代码。如下:
4041

4142
<?php
4243

@@ -45,12 +46,12 @@ First, let's review some code that is tightly coupled to a cache implementation.
4546
class Repository
4647
{
4748
/**
48-
* The cache instance.
49+
* 缓存实例。
4950
*/
5051
protected $cache;
5152

5253
/**
53-
* Create a new repository instance.
54+
* 创建一个仓库实例。
5455
*
5556
* @param \SomePackage\Cache\Memcached $cache
5657
* @return void
@@ -61,7 +62,7 @@ First, let's review some code that is tightly coupled to a cache implementation.
6162
}
6263

6364
/**
64-
* Retrieve an Order by ID.
65+
* 按照Id检索订单。
6566
*
6667
* @param int $id
6768
* @return Order
@@ -74,11 +75,11 @@ First, let's review some code that is tightly coupled to a cache implementation.
7475
}
7576
}
7677

77-
In this class, the code is tightly coupled to a given cache implementation. It is tightly coupled because we are depending on a concrete Cache class from a package vendor. If the API of that package changes our code must change as well.
78+
在这个类中,代码跟给定的缓存实现紧密耦合。因为我们依赖于一个具体的 Cache 类从一个软件包供应商。如果该包的 API 更改,我们的代码也必须更改。
7879

79-
Likewise, if we want to replace our underlying cache technology (Memcached) with another technology (Redis), we again will have to modify our repository. Our repository should not have so much knowledge regarding who is providing them data or how they are providing it.
80+
同样,如果我们要用另一种缓存技术( Redis )替换我们的底层缓存技术( Memcached ),我们再次必须修改我们的 Repository 。我们的 Repository 类不应该知道这么多关于谁提供了数据,或是如何提供等细节。
8081

81-
**Instead of this approach, we can improve our code by depending on a simple, vendor agnostic interface:**
82+
**比起上面的做法,我们可以使用一个简单,和扩展包无关的接口来改进代码:**
8283

8384
<?php
8485

@@ -89,12 +90,12 @@ Likewise, if we want to replace our underlying cache technology (Memcached) with
8990
class Repository
9091
{
9192
/**
92-
* The cache instance.
93+
* 缓存实例。
9394
*/
9495
protected $cache;
9596

9697
/**
97-
* Create a new repository instance.
98+
* 创建一个仓库实例。
9899
*
99100
* @param Cache $cache
100101
* @return void
@@ -105,23 +106,24 @@ Likewise, if we want to replace our underlying cache technology (Memcached) with
105106
}
106107
}
107108

108-
Now the code is not coupled to any specific vendor, or even Laravel. Since the contracts package contains no implementation and no dependencies, you may easily write an alternative implementation of any given contract, allowing you to replace your cache implementation without modifying any of your cache consuming code.
109+
现在代码没有与任何软件包耦合,甚至 Laravel 。由于 contracts 不包含实现和依赖,因此您可以轻松地编写任何 contracts 实现,从而替换缓存实现,而无需修改任何缓存代码。
109110

110111
<a name="simplicity"></a>
111-
### Simplicity
112+
### 简单性
113+
114+
当所有的 Laravel 服务都使用简洁的接口定义,就能够很容易决定一个服务需要提供的功能。 **可以将 contracts 视为说明框架特色的简洁文档。**
112115

113-
When all of Laravel's services are neatly defined within simple interfaces, it is very easy to determine the functionality offered by a given service. **The contracts serve as succinct documentation to the framework's features.**
116+
除此之外,当依赖的接口足够简洁时,代码的可读性和可维护性大大提高。比起搜索一个大型复杂的类里有哪些可用的方法,你有一个简单,干净的接口可以参考。
114117

115-
In addition, when you depend on simple interfaces, your code is easier to understand and maintain. Rather than tracking down which methods are available to you within a large, complicated class, you can refer to a simple, clean interface.
116118

117119
<a name="how-to-use-contracts"></a>
118-
## How To Use Contracts
120+
## 如何使用 Contracts
119121

120-
So, how do you get an implementation of a contract? It's actually quite simple.
122+
那么,如何获取一个 contract 实现呢?这其实很简单。
121123

122-
Many types of classes in Laravel are resolved through the [service container](/docs/{{version}}/container), including controllers, event listeners, middleware, queued jobs, and even route Closures. So, to get an implementation of a contract, you can just "type-hint" the interface in the constructor of the class being resolved.
124+
Laravel 中的许多类型的类都是通过 [服务容器](/docs/{{version}}/container) 解析,包括控制器,事件监听器,中间件,任务队列,甚至路由闭包。所以,要获得一个 contract 的实现,你只需要在正在解析的类的构造函数中的声明「类型约束」即可。
123125

124-
For example, take a look at this event listener:
126+
例如,看看这个事件监听器:
125127

126128
<?php
127129

@@ -134,12 +136,12 @@ For example, take a look at this event listener:
134136
class CacheOrderInformation
135137
{
136138
/**
137-
* The Redis database implementation.
139+
* Redis 数据库实现。
138140
*/
139141
protected $redis;
140142

141143
/**
142-
* Create a new event handler instance.
144+
* 创建事件处理器实例。
143145
*
144146
* @param Database $redis
145147
* @return void
@@ -150,7 +152,7 @@ For example, take a look at this event listener:
150152
}
151153

152154
/**
153-
* Handle the event.
155+
* 处理事件。
154156
*
155157
* @param OrderWasPlaced $event
156158
* @return void
@@ -161,12 +163,13 @@ For example, take a look at this event listener:
161163
}
162164
}
163165

164-
When the event listener is resolved, the service container will read the type-hints on the constructor of the class, and inject the appropriate value. To learn more about registering things in the service container, check out [its documentation](/docs/{{version}}/container).
166+
当事件监听器被解析时,服务容器将读取类的构造函数上的类型提示,并注入对应的类实例。 想了解如何注册绑定到容器,请查看[服务容器](/docs/{{version}}/container)
167+
165168

166169
<a name="contract-reference"></a>
167-
## Contract Reference
170+
## Contract 参考
168171

169-
This table provides a quick reference to all of the Laravel contracts and their equivalent facades:
172+
下面的表格提供了 Laravel contracts 及其对应的 facades 的参考:
170173

171174
Contract | References Facade
172175
------------- | -------------
@@ -203,3 +206,8 @@ Contract | References Facade
203206
[Illuminate\Contracts\Validation\Validator](https://github.com/illuminate/contracts/blob/{{version}}/Validation/Validator.php) | &nbsp;
204207
[Illuminate\Contracts\View\Factory](https://github.com/illuminate/contracts/blob/{{version}}/View/Factory.php) | View::make()
205208
[Illuminate\Contracts\View\View](https://github.com/illuminate/contracts/blob/{{version}}/View/View.php) | &nbsp;
209+
210+
## 译者署名
211+
| 用户名 | 头像 | 职能 | 签名 |
212+
|---|---|---|---|
213+
| [@e421083458](https://github.com/e421083458) | <img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/10802_1486368142.jpeg?imageView2/1/w/100/h/100"> | 翻译 | Github求star,[@e421083458](https://github.com/e421083458/) at Github |

0 commit comments

Comments
 (0)