A Faster Lightweight Full-Stack PHP Framework :rocket:
A Faster Lightweight Full-Stack PHP Framework :rocket:
A Faster Lightweight Full-Stack PHP Framework
中文版
Just one command to build all env for the easy-php
Why do we need to build a PHP framework by ourself? Maybe the most of people will say "There have so many PHP frameworks be provided, but we still made a wheel?". My point is "Made a wheel is not our purpose, we will get a few of knowledge when making a wheel which is our really purpose".
Then, how to build a PHP framework by ourself? General process as follows:
Entry file ----> Register autoload function
----> Register error(and exception) function
----> Load config file
----> Request
----> Router
----> (Controller Model)
----> Response
----> Json
----> View
In addition, unit test, nosql support, api documents and some auxiliary scripts, e.g. Finnally, My framework directory as follows:
…
Defined a entrance file that provide a uniform file for user visit, which hide the complex logic like the enterprise service bus.
// require the application run file
require('../framework/run.php');
Register a autoload function in the __autoload queue by used spl_autoload_register, after that, we can use a class by namespace and keyword 'use'.
Register a function by used set_error_handler to handle error, but it can't handle the following error, E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING and the E_STRICT produced by the file which called set_error_handler function. So, we need use register_shutdown_function and error_get_last to handle this finally error which set_error_handler can't handle. When the framework running, we can handle the error by ourself, such as, give a friendly error messge for client.
[file: framework/hanles/ErrorHandle.php]
Register a function by used set_exception_handler to handle the exception which is not be catched, which can give a friendly error messge for client.
[file: framework/hanles/ExceptionHandle.php]
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]
All output is json in the framework, neithor framework's core error or business logic's output, beacuse I think is friendly.
$request = App::$container->get('request');
$request->check('username', 'require');
$request->check('password', 'length', 12);
$request->check('code', 'number');
[file: framework/Response.php]
├── router [datebase object relation map class directory]
├── RouterInterface.php [router strategy interface]
├── General.php [general strategy class]
├── Pathinfo.php [pathinfo strategy class]
├── Userdefined.php [userdefined strategy class]
├── Micromonomer.php [micromonomer strategy class]
├── Job.php [job strategy class]
└── EasyRouter.php [router strategy entrance class]
Execute the target controller's function by the router parse the url information.Is composed of four types of:
tradition router
domain/index.php?module=Demo&contoller=Index&action=test&username=test
pathinfo router
domain/demo/index/modelExample
user-defined router
// config/moduleName/route.php, this 'this' point to RouterHandle instance
$this->get('v1/user/info', function (Framework\App $app) {
return 'Hello Get Router';
});
micro monolith router
What's the micro monolith router? There are a lot of teams are moving in the SOA service structure or micro service structure, I think it is difficult for a small team. So the micro monolith was born, what's this? In my opinion, this is a SOA process for a monolith application.For example:
app
├── UserService [user service module]
├── ContentService [content service module]
├── OrderService [order service module]
├── CartService [cart service module]
├── PayService [pay service module]
├── GoodsService [goods service module]
└── CustomService [custom service module]
As above, we implemented a easy micro monolith structure.But how these module to communicate with each other? As follows:
App::$app->get('demo/index/hello', [
'user' => 'TIGERB'
]);
So we can resolve this problem loose coupling. In the meantime, we can exchange our application to the SOA structure easily, beacuse we only need to change the method get implementing way in the App class, the way contain RPC, REST. etc.
[file: framework/hanles/RouterHandle.php]
The tradition MVC pattern includes the model,view,controller layer. In general, you always write the business logic in the controller or model layer. But you will feel the code is difficult to read, maintain, expand after a long time. So I add a logic layer in the framework forcefully where you can implement the business logic by yourself. You can not only implement a tool class but also implement your business logic in a new subfolder, what's more, you can implement a gateway based on the pattern of responsibility (I provided a example).
In the end, the structure as follows:
Logics layer
A gateway example:
I built a gateway in the logics folder, structure as follows:
gateway [gateway directory in logics]
├── Check.php [interface]
├── CheckAppkey.php [check app key]
├── CheckArguments.php [check require arguments]
├── CheckAuthority.php [check auth]
├── CheckFrequent.php [check call frequent]
├── CheckRouter.php [router]
├── CheckSign.php [check sign]
└── Entrance.php [entrance file]
The gateway entrance class code as follows:
…
After the gateway be implemented, how to use this in the framework?I provide a user-defined's class, we just register this in the UserDefinedCase class. for example:
/**
* register user-defined behavior
*
* @var array
*/
private $map = [
// for example, loading user-defined gateway
'App\Demo\Logics\Gateway\Entrance'
];
So, the gateway is running.But what's the UserDefinedCase that can be loading before RouterHandle.
Where is the view layer?I abandon it, beacuse I chose the SPA for frontend, detail as follows.
source code folder
The separate-frontend-and-backend and two-way data binding, modular is so popular.In the meantime, I moved the project easy-vue that built by myself to the framework as the view layer. The frontend source code folder as follows:
frontend [application frontend source code directory]
├── src [source folder]
│ ├── components [vue components]
│ ├── views [vue views]
│ ├── images [images folder]
│ ├── ...
├── app.js [vue root js]
├── app.vue [vue root component]
├── index.template.html [frontend entrance template file]
├── store.js [vuex store file]
Build Step
yarn install
DOMAIN=http://yourdomain npm run dev
After build
After built success, there made dist folder and index.html in the public. This file will be ignore when this branch is not the release branch.
public [this is a resource directory to expose service resource]
├── dist [frontend source file after build]
│ └── ...
├── index.html [entrance html file]
What's the ORM(Object Relation Map)? In my opinion, ORM is a thought that build a relationship of object and the abstract things.The model is the database's table and the model's instance is a operation for the table."Why do you do that, use the sql directly is not good?", my answer:you can do what you like to do, everything is flexable, but it's not be suggested from a perspective of a framework's reusable, maintainable and extensible.
On the market for the implemention of the ORM, such as: Active Record in thinkphp and yii, Eloquent in laravel, then we call the ORM here is "ORM" simply. The "ORM" structure in the framework as follows:
├── orm
│ ├── Interpreter.php [sql Interpreter]
│ ├── DB.php [database operate class]
│ ├── Model.php [base model class]
│ └── db
│ └── Mysql.php [mysql class]
DB example
/**
* DB operation example
*
* findAll
*
* @return void
*/
public function dbFindAllDemo()
{
$where = [
'id' => ['>=', 2],
];
$instance = DB::table('user');
$res = $instance->where($where)
->orderBy('id asc')
->limit(5)
->findAll(['id','create_at']);
$sql = $instance->sql;
return $res;
}
Model example
…
What's the service container?
Service container is difficultly 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.
The meaning of the service container?
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:
class Demo
{
public function __construct ()
{
// the demo directly dependent on RelyClassName
$instance = new RelyClassName ();
}
}
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 contain
No open issues yet, or sync has not completed.