Skip to content

Commit 7b9ad69

Browse files
committed
docs(README): add nosql and service container desc 📝
1 parent 6693d0c commit 7b9ad69

20 files changed

+172
-4
lines changed

README-CN.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ framework [Easy PHP核心框架目录]
5050
│ ├── ErrorHandle.php [错误处理机制类]
5151
│ ├── ExceptionHandle.php [未捕获异常处理机制类]
5252
│ ├── ConfigHandle.php [配置文件处理机制类]
53+
│ ├── NosqlHandle.php [nosql处理机制类]
5354
│ └── RouterHandle.php [路由处理机制类]
5455
├── orm [对象关系模型]
5556
│ ├── Interpreter.php [sql解析器]
@@ -163,20 +164,114 @@ README.md [readme文件]
163164

164165
### 服务容器
165166

167+
什么是服务容器?
168+
169+
服务容器听起来很浮,按我的理解简单来说就是提供一个第三方的实体,我们把业务逻辑需要使用的类或实例注入到这个第三方实体类中,当需要获取类的实例时我们直接通过这个第三方实体类获取。
170+
171+
服务容器的意义?
172+
173+
用设计模式来讲:其实不管设计模式还是实际编程的经验中,我们都是强调“高内聚,松耦合”,我们做到高内聚的结果就是每个实体的作用都是极度专一,所以就产生了各个作用不同的实体类。在组织一个逻辑功能时,这些细化的实体之间就会不同程度的产生依赖关系,对于这些依赖我们通常的做法如下:
174+
175+
```
176+
class Demo
177+
{
178+
public function __construct()
179+
{
180+
// 类demo直接依赖RelyClassName
181+
$instance = new RelyClassName();
182+
}
183+
}
184+
```
185+
186+
这样的写法没有什么逻辑上的问题,但是不符合设计模式的“最少知道原则”,因为之间产生了直接依赖,整个代码结构不够灵活是紧耦合的。所以我们就提供了一个第三方的实体,把直接依赖转变为依赖于第三方,我们获取依赖的实例直接通过第三方去完成以达到松耦合的目的,这里这个第三方充当的角色就类似系统架构中的“中间件”,都是协调依赖关系和去耦合的角色。最后,这里的第三方就是所谓的服务容器。
187+
188+
在实现了一个服务容器之后,我把Rquest,Config等实例都以单例的方式注入到了服务容器中,当我们需要使用的时候从容器中获取即可,十分方便。使用如下:
189+
190+
```
191+
// 注入单例
192+
App::$container->setSingle('别名,方便获取', '对象/闭包/类名');
193+
194+
// 例,注入Request实例
195+
App::$container->setSingle('request', $request);
196+
// 获取Request对象
197+
App::$container->getSingle('request');
198+
```
199+
166200
### Nosql的支持
167201

202+
提供对nosql的支持,提供全局单例对象,借助我们的服务容器我们在框架启动的时候,通过配置文件的配置把需要的nosql实例注入到服务容器中。目前我们支持redis/memcahed/mongodb。
203+
204+
如何使用?如下,
205+
206+
```
207+
// 获取redis对象
208+
App::$container->getSingle('redis');
209+
// 获取memcahed对象
210+
App::$container->getSingle('memcahed');
211+
// 获取mongodb对象
212+
App::$container->getSingle('mongodb');
213+
```
214+
168215
[file: framework/nosql/*]
169216

170217
### 接口文档生成和接口模拟
171218

172219
通常我们写完一个接口后,接口文档是一个问题,我们这里使用Api Blueprint协议完成对接口文档的书写和mock,同时我们配合使用Swagger通过接口文档实现对接口的实时访问。
173220

174-
[file: doc/*]
221+
Api Blueprint接口描述协议选取的工具是snowboard,具体使用说明如下:
222+
223+
**接口文档生成说明**
224+
225+
```
226+
cd docs/apib
227+
228+
./snowboard html -i demo.apib -o demo.html -s
229+
230+
open the website, http://localhost:8088/
231+
```
232+
233+
**接口mock使用说明**
234+
235+
```
236+
cd docs/apib
237+
238+
./snowboard mock -i demo.apib
239+
240+
open the website, http://localhost:8087/demo/index/hello
241+
```
242+
243+
[file: docs/*]
175244

176245
### 单元测试
177246

178247
基于phpunit的单元测试,写单元测试是个好的习惯。
179248

249+
如何使用?
250+
251+
tests目录下编写测试文件,具体参考tests/demo目录下的DemoTest文件,然后运行:
252+
253+
```
254+
vendor/bin/phpunit
255+
```
256+
257+
测试断言示例:
258+
259+
```
260+
/**
261+
* 演示测试
262+
*/
263+
public function testDemo()
264+
{
265+
$this->assertEquals(
266+
'Hello Easy PHP',
267+
// 执行demo模块index控制器hello操作,断言结果是不是等于Hello Easy PHP 
268+
App::$app->get('demo/index/hello')
269+
);
270+
}
271+
```
272+
273+
[phpunit断言文档语法参考](https://phpunit.de/manual/current/zh_cn/appendixes.assertions.html)
274+
180275
[file: tests/*]
181276

182277
### Git钩子配置

README.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ framework [easy-php framework directory]
5151
│ ├── ErrorHandle.php [error handle class]
5252
│ ├── ExceptionHandle.php [exception handle class]
5353
│ ├── ConfigHandle.php [config handle class]
54+
│ ├── NosqlHandle.php [nosql handle class]
5455
│ └── RouterHandle.php [router handle class]
5556
├── orm [datebase object relation map class directory]
5657
│ ├── Interpreter.php [sql Interpreter class]
@@ -143,16 +144,88 @@ Where is the view layer?I abandon it, beacuse I chose the SPA for frontend, deta
143144

144145
### Service Container
145146

147+
What's the service container?
148+
149+
Service container is default to understand, I think it just a third party class, which can inject the class and instance. we can get the instance in the container very simple.
150+
151+
The meaning of the service container?
152+
153+
According to the design patterns: we need make our code "highly cohesive, loosely coupled". As the result of "highly cohesive" is "single principle", As the result of "single principle" is the class rely on each other. General way that handle the dependency as follows:
154+
155+
```
156+
class Demo
157+
{
158+
public function __construct ()
159+
{
160+
// the demo directly dependent on RelyClassName
161+
$instance = new RelyClassName ();
162+
}
163+
}
164+
```
165+
166+
The above code is no problem, but is not conform to the design pattern of "The least kown principle", beacuse it has a direct dependence. We bring a third class in the framework, which can new a class or get a instance. So, the third party class is the service container, which like the role of 'middleware' in the architecture of the system.
167+
168+
After implements a service container, I put the Rquest, Config and other instances are injected into service in the singleton container, when we need to use can be obtained from the container, is very convenient.Use the following:
169+
170+
```
171+
// Inject the single instance
172+
App::$container->setSingle('alias', 'object/closure/class name');
173+
174+
// Such as,Inject Request instance
175+
App::$container->setSingle('request', $request);
176+
// get Request instance
177+
App::$container->getSingle('request');
178+
```
179+
146180
### Nosql Support
147181

148-
### Api Doc
182+
Inject the nosql's single instance in service container when the framework loading, you can decide what nosql you need use whit the configuration. At present we support redis/memcahed/mongodb.
183+
184+
Some example:
185+
186+
```
187+
// get redis instance
188+
App::$container->getSingle('redis');
189+
// get memcahed instance
190+
App::$container->getSingle('memcahed');
191+
// get mongodb instance
192+
App::$container->getSingle('mongodb');
193+
```
194+
195+
### Api Docs
149196

150197
Usually after we write an api, the api documentation is a problem, we use the Api Blueprint protocol to write the api document and mock. At the same time, we can request the api real-timely by used Swagger.
151198

152199
### PHPunit
153200

154201
Based on the phpunit, I think write unit tests is a good habit.
155202

203+
How to make a test?
204+
205+
Write test file in the folder tests by referenced the file DemoTest.php, then run:
206+
207+
```
208+
vendor/bin/phpunit
209+
```
210+
211+
The assertion example:
212+
213+
```
214+
/**
215+
* test assertion example
216+
*/
217+
public function testDemo()
218+
{
219+
$this->assertEquals(
220+
'Hello Easy PHP',
221+
// assert the result by run hello function in demo/Index controller
222+
App::$app->get('demo/index/hello')
223+
);
224+
}
225+
```
226+
227+
[phpunit assertions manual](https://phpunit.de/manual/current/en/appendixes.assertions.html)
228+
156229
### Git Hooks
157230

158231
- The standard of coding: verify the coding forcefully before commit by used php_codesniffer

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "TIGERB/easy-php",
33
"description": "A lightweight PHP framework for studying",
4-
"version":"0.1.0",
4+
"version":"0.3.6",
55
"type": "framework",
66
"authors": [
77
{
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)