欢迎体验 QueryPHP, composer 一键安装,主框架 5 万行单元测试覆盖了 73%的代码, 2 年开发即将问世

2018-09-04 21:37:23 +08:00
 doyouhaobaby

2016 年 10 月 QueryPHP 开始基于以前的一个过气老 php.5.0-5.4 版本框架进行重构,经过两年的开发项目终于接近尾声。随着单元测试逐步逼近 100%,第一个 alpha 版本正在整理发布。

根据以前的计划,我希望做一个这样的框架,首先它是一个传统的 PHP 框架,无须依赖任何扩展,很容易安装。

首先它应该是团队友好

为每一个 PHP 脚本开启强类型,提高代码质量,严禁意味着后期更少的错误

<?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2018 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Kernel;

use Leevel\Http\IRequest;
use Leevel\Http\IResponse;

/**
 * 内核执行接口.
 *
 * @author Xiangmin Liu <635750556@qq.com>
 *
 * @since 2016.11.18
 *
 * @version 1.0
 */
interface IKernel
{
    /**
     * 响应 HTTP 请求
     *
     * @param \Leevel\Http\IRequest $request
     *
     * @return \Leevel\Http\IResponse
     */
    public function handle(IRequest $request): IResponse;

    /**
     * 执行结束
     *
     * @param \Leevel\Http\IRequest  $request
     * @param \Leevel\Http\IResponse $response
     */
    public function terminate(IRequest $request, IResponse $response): void;

    /**
     * 返回项目.
     *
     * @return \Leevel\Kernel\IProject
     */
    public function getProject(): IProject;
}

单元测试高覆盖

我发现国内很多 PHP 框架竟然是 0 单元测试,随着我自己开始编写越来越多的单元测试,发现了单元测试不仅仅是发现 bug,更是可以帮助你设计框架接口,站在使用者的立场来优化框架组件。而且对于后期将会生成良性循环,更少 bug 减少反反复复地陷入无法自拔,代码质量无法保证,所以 QueryPHP 第一个版本基本维度 100% 覆盖,后续加强。

<?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2018 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tests\Cache;

use Leevel\Cache\File;
use Tests\TestCase;

/**
 * file test.
 *
 * @author Xiangmin Liu <635750556@qq.com>
 *
 * @since 2018.06.05
 *
 * @version 1.0
 */
class FileTest extends TestCase
{
}

良好的体验

QueryPHP 以 Laravel 和 Symfony 为参考对象,主打 ioc + ddd 方式来构建良好用户体验的框架。orm 在做小规模重构,相关设计在试错中。

<?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2018 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tests\Database\Ddd\Entity;

use Leevel\Database\Ddd\Entity;

/**
 * TestConstructWhiteEntity.
 *
 * @author Xiangmin Liu <635750556@qq.com>
 *
 * @since 2018.06.30
 *
 * @version 1.0
 */
class TestConstructWhiteEntity extends Entity
{
    const TABLE = 'test';

    /**
     * 存在复合主键.
     *
     * @var array
     */
    const PRIMARY_KEY = [
        'id',
    ];

    const AUTO_INCREMENT = 'id';

    const STRUCT = [
        'id' => [
            'name'            => 'id', // database
            'type'            => 'int', // database
            'length'          => 11, // database
            'primary_key'     => true, // database
            'auto_increment'  => true, // database
            'default'         => null, // database
            'construct_white' => true,
        ],
        'name' => [
            'name'           => 'name',
            'type'           => 'varchar',
            'length'         => 45,
            'primary_key'    => false,
            'auto_increment' => false,
            'default'        => null,
        ],
    ];
    protected $id;

    protected $name;
}

组件化

每一个组件即是一个 composer, 遵循高内聚低耦合。

https://github.com/queryyetsimple

可选扩展框架实现高性能 Leevel

我们访问一个类, composer 根据 psr4 规则去搜索到我们文件而载入,如下的脚本会被载入。

https://github.com/hunzhiwange/framework/blob/master/src/Queryyetsimple/Di/Container.php

use Leevel\Di\Container

如果我们存在一个扩展就提供了这样一个类并随着 PHP 常驻,是不是性能不错,实际上是可以,QueryPHP 选择了 zephir 来实现。

https://github.com/hunzhiwange/leevel/blob/master/leevel/di/container.zep

实际上会被编译成 c

https://github.com/hunzhiwange/leevel/blob/master/ext/leevel/di/container.zep.c

这样子,不需要修改代码直接提升性能。

extension = leevel.so

opcache 开启后还有性能优化吗?

Yes, QueryPHP 的 PHP 版本和扩展版本功能一致,根据有对比性,核心组件走扩展来提升性能。 你可以亲自去编译一下。

https://github.com/hunzhiwange/leevel

如何保证 PHP 版本和扩展版本在对外 API 的一致性

高单元测试覆盖率,公用一份单元测试,我们通过 subtree 将单元测试抽离到一个 git 仓库来共享单元测试。

git subtree add --prefix=tests git@github.com:queryyetsimple/tests.git master
https://github.com/hunzhiwange/leevel/blob/master/tests/Di/ContainerTest.php
https://github.com/hunzhiwange/framework/blob/master/tests/Di/ContainerTest.php

超高性能支持 swoole

swoole 的问世对于 PHP 后端来说是一种福音,看到 swoole 4.1 开始支持原始 pdo,redis 协程,支持 swoole 势在必行。已经做了一些基础工作,对于第一个版本我们要完善单元测试,所以下一个版本以及未来数年主要支持 swoole.我们不想在传统的框架与 TP 竞争,不会疯狂扩展功能。稳定与性能是第一个要素,QueryPHP 志在高性能。未来主要围绕着微服务,与微服务基础设施进行整合的相关研究。

体验吧。求 star

访问

https://github.com/hunzhiwange/queryphp

composer create-project hunzhiwange/queryphp myapp dev-master --repository=https://packagist.org/
php leevel server <Visite http://127.0.0.1:9527/>
Api Test http://127.0.0.1:9527/api/test
php leevel link:public http://127.0.0.1:9527/public/css/page.css
php leevel link:storage http://127.0.0.1:9527/storage/logo.png
php leevel link:apis http://127.0.0.1:9527/apis/

优化性能

php leevel production
开启 opcache

更高性能,编译安装配套可选扩展

https://github.com/hunzhiwange/leevel

git clone git@github.com:hunzhiwange/leevel.git
cd ext

$/path/to/phpize
$./configure --with-php-config=/path/to/php-config
$make && make install

extension = leevel.so

来体验吧,如果你觉得有意思,给我一个 star,关注 QueryPHP.

4071 次点击
所在节点    PHP
36 条回复
mingyun
2018-09-04 23:17:00 +08:00
应该说明 require PHP7.1.3+
ps:有点像 laravel
zn
2018-09-04 23:20:22 +08:00
名字不太好,换个名字。
jisibencom
2018-09-04 23:49:26 +08:00
还以为是采集器。。。
ericgui
2018-09-05 00:03:27 +08:00
@zn
@jisibencom

既然是 0 依赖,可以考虑 soloPHP
sagaxu
2018-09-05 00:11:44 +08:00
看上去不错,不过小众框架的使用成本太高了
wwwxxxfr
2018-09-05 00:17:25 +08:00
@zn 支持,感觉名字不太好

也是用 zephir 写的啊,感觉挺好
yangqi
2018-09-05 00:29:07 +08:00
优点是什么?无需依赖任何扩展

"require": {
"php": "^7.1.3",
"ext-mbstring": "*",
"ext-openssl": "*",
"symfony/console": "~4.0",
"symfony/var-dumper": "~4.0",
"symfony/process": "~4.0",
"symfony/finder": "~4.0",
"clio/clio": "@stable",
"robmorgan/phinx": "^0.9.2",
"vlucas/phpdotenv": "~2.2",
"nesbot/carbon": "~1.20",
"league/flysystem": "^1.0",
"monolog/monolog": "^1.23",
"swiftmailer/swiftmailer": "6.0.2",
"nunomaduro/collision": "~2.0",
"twig/twig": "~2.0",
"zircote/swagger-php": "2.0.13",
"gettext/gettext": "^4.6.0",
"fzaninotto/faker": "^1.6"
}
yangqi
2018-09-05 00:29:55 +08:00
另外,加密算法居然用的 discuz 的,我是穿越了么?

/**
* 来自 Discuz 经典 PHP 加密算法.
*
* @param string $strings
* @param bool $decode
* @param string $key
* @param int $expiry
*
* @return string
*/
protected function authcode(string $strings, bool $decode = true, string $key = '', int $expiry = 0)
{
doyouhaobaby
2018-09-05 00:33:02 +08:00
@yangqi 不依赖 ext
fleam
2018-09-05 00:40:53 +08:00
学不动了,自己写了个反向生成,定义好请求规则自动生成库表字段和基本接口代码,前端请求完毕,后端数据库和接口也好了,就图个快……
zhaolion
2018-09-05 00:45:06 +08:00
其实框架 UT 覆盖率只是一个靠谱的标志之一,重点在于好用和社区,加油吧,骚年
doyouhaobaby
2018-09-05 00:52:01 +08:00
@zhaolion 说的有道理,还未出生没社区,一点点坚持
@fleam 有点厉害
yangqi
2018-09-05 00:55:34 +08:00
@doyouhaobaby

"ext-mbstring": "*",
"ext-openssl": "*",
tomfs
2018-09-05 01:09:54 +08:00
支持,造轮子不易。
agdhole
2018-09-05 07:51:16 +08:00
框架还是看社区活跃,才出的新框架还是观望观望
dajj
2018-09-05 08:23:47 +08:00
请问优势是什么,相比市面上的框架。 老实说,实在厌倦了 PHP 框架, 各种造轮子, 好多功能大同小异,偏偏那小异,让人不断在用法上折腾,精疲力尽。
azoon
2018-09-05 08:28:54 +08:00
一眼看名字以为是 phpQuery
doyouhaobaby
2018-09-05 08:47:05 +08:00
@dajj 是这样的,首先用法上要遵循 psr,api 与 laravel 和 symfony 等贴近,http 组件基于 symfony 二次开发,主要是为了编译成 zephir 扩展,例外用 composer 里面精致轮子,避免重复造这些非核心组件。第二个就是完整地对所有核心采用 zephir 制作成 c 扩展来提升性能。最后也是以后的重点是对 swoole 的支持,未来主要在这一块进行扩展。php7(2|3)+redis+框架扩展化常驻+swoole 业务常驻+微服务基础设施整合(比如统一配置中心,服务注册于发现,与 grpc 和 thrift,以及日志系统接入),最后两个做了一点点,是未来重点关注对象
doyouhaobaby
2018-09-05 08:53:03 +08:00
@yangqi 这个 php 现在内置,编译带上,非第三方扩展,mbstring 系列可以避免以前 iconv 一些兼容判断,openssl 这个加解密必需的。
lepig
2018-09-05 09:08:43 +08:00
支持一波 php 的轮子 确实已经很多了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/486163

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX