Skip to content

Commit

Permalink
feat(db): master and slave support ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed May 22, 2017
1 parent 6e9748f commit 4ed7253
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 79 deletions.
31 changes: 28 additions & 3 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<p align="center">
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/build-passing-brightgreen.svg" alt="Build Status"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/php-5.4%2B-blue.svg" alt="PHP Version"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/version-0.6.7-green.svg" alt="Version"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/framework-148KB-orange.svg" alt="Framework Size"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/version-0.6.9-green.svg" alt="Version"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/framework-152KB-orange.svg" alt="Framework Size"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/framework--phar-76KB-red.svg" alt="Framework Phar Size"></a>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/cocoapods/l/AFNetworking.svg" alt="License"></a>
</p>
Expand Down Expand Up @@ -171,6 +171,31 @@ require('../framework/run.php');

加载框架自定义和用户自定义的配置文件。

例如,数据库主从配置.env文件参数示例:

```
[database]
dbtype = mysqldb
dbprefix = easy
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
slave = 0,1
[database-slave-0]
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
[database-slave-1]
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
```

[[file: framework/hanles/ConfigHandle.php](https://github.com/TIGERB/easy-php/blob/master/framework/handles/ConfigHandle.php)]

## 输入和输出
Expand All @@ -180,7 +205,7 @@ require('../framework/run.php');

框架中所有的异常输出和控制器输出都是json格式,因为我认为在前后端完全分离的今天,这是很友善的,目前我们不需要再去考虑别的东西。

# 请求参数校验,目前提供必传,长度,数字类型校验,使用如下
##### 请求参数校验,目前提供必传,长度,数字类型校验,使用如下
```
$request = App::$container->getSingle('request');
$request->check('username', 'require');
Expand Down
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<p align="center">
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/build-passing-brightgreen.svg" alt="Build Status"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/php-5.4%2B-blue.svg" alt="PHP Version"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/version-0.6.7-green.svg" alt="Version"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/framework-148KB-orange.svg" alt="Framework Size"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/version-0.6.9-green.svg" alt="Version"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/framework-152KB-orange.svg" alt="Framework Size"></a>
<a href="https://github.com/TIGERB/easy-php/releases"><img src="https://img.shields.io/badge/framework--phar-76KB-red.svg" alt="Framework Phar Size"></a>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/cocoapods/l/AFNetworking.svg" alt="License"></a>
</p>

<p align="center"> A lightweight PHP framework for studying <p>
<p align="center"> A Faster Lightweight Full-Stack PHP Framework <p>

<p align="center"> <a href="./README-CN.md">中文版</a> <p>

Expand Down Expand Up @@ -168,6 +168,31 @@ Register a function by used set_exception_handler to handle the exception which

Loading framework-defined and user-defined config files.

For example,the master-salve database config:

```
[database]
dbtype = mysqldb
dbprefix = easy
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
slave = 0,1
[database-slave-0]
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
[database-slave-1]
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
```

[[file: framework/hanles/ConfigHandle.php](https://github.com/TIGERB/easy-php/blob/master/framework/handles/ConfigHandle.php)]

## Request&Response Module
Expand All @@ -177,7 +202,7 @@ Loading framework-defined and user-defined config files.

All output is json in the framework, neithor framework's core error or business logic's output, beacuse I think is friendly.

# Request param check, Support require/length/number check at present. Use as follows:
##### Request param check, Support require/length/number check at present. Use as follows:
```
$request = App::$container->getSingle('request');
$request->check('username', 'require');
Expand Down Expand Up @@ -671,3 +696,7 @@ project address: [https://github.com/TIGERB/easy-php](https://github.com/TIGERB/
- The performance test and optimize
- Use the lazy load thought to optimize the framework
- Change Helper's method to the framework's function
- v0.6.9(2017/05/22)
- more friendly for api develop process
+ request param check:require/length/number
- support master-salve config for db
15 changes: 11 additions & 4 deletions app/demo/controllers/DbOperationDemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public function dbFindDemo()
$sql = $instance->sql;
$database = $instance->masterSlave;

// return $sql;
return $res;
return [
'db' => $database,
'sql' => $sql,
'res' => $res
];
}

/**
Expand Down Expand Up @@ -106,8 +109,12 @@ public function dbSaveDemo()
}

return [
'user_id' => $userId,
'test_id' => $testId
'db' => $user->masterSlave,
'sql' => $user->sql,
'res' => [
'user_id' => $userId,
'test_id' => $testId
]
];
}

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tigerb/easy-php",
"description": "A lightweight PHP framework for studying",
"version": "0.6.8",
"description": "A Faster Lightweight Full-Stack PHP Framework",
"version": "0.6.9",
"type": "framework, easy-php, php framework",
"homepage": "http://php.tiegrb.cn/",
"license": "MIT",
Expand All @@ -24,7 +24,8 @@
"composer dump-autoload --optimize"
],
"post-root-project-cmd": [
"composer install"
"composer install",
"cp ./.git-hooks/* ./git/hooks"
],
"pre-status-cmd": [
"cp .env.example .env",
Expand Down
19 changes: 14 additions & 5 deletions framework/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public function setSingle($alias = '', $object = '')
if (array_key_exists($alias, $this->instanceMap)) {
return $this->instanceMap[$alias];
}
$this->instanceMap[$alias] = $object();
return $this->instanceMap[$alias];
$this->instanceMap[$alias] = $object;
}
if (is_object($alias)) {
$className = get_class($alias);
Expand Down Expand Up @@ -144,14 +143,24 @@ public function setSingle($alias = '', $object = '')
*
* get a sington instance
*
* @param string $alias 类名或别名
* @param string $alias 类名或别名
* @param Closure $closure 闭包
* @return object
*/
public function getSingle($alias = '')
public function getSingle($alias = '', $closure = '')
{
if (array_key_exists($alias, $this->instanceMap)) {
return $this->instanceMap[$alias];
$instance = $this->instanceMap[$alias];
if (is_callable($instance)) {
return $instance();
}
return $instance;
}

if (is_callable($closure)) {
return $this->instanceMap[$alias] = $closure();
}

throw new CoreHttpException(
404,
'Class:' . $alias
Expand Down
Loading

0 comments on commit 4ed7253

Please sign in to comment.