agorlov / lipid
面向对象框架,用于Web应用
1.0.7
2023-02-10 13:03 UTC
Requires
- php: >= 8.0
- twig/twig: ^2.0
Requires (Dev)
- phpmd/phpmd: ^2.6
- phpstan/phpstan: ^0.11.8
- phpunit/phpunit: ^8
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-09-21 15:48:21 UTC
README
Lipid是一个为PHP Web应用设计的对象化微框架。
快速入门
创建应用目录
$ mkdir testapp $ composer require agorlov/lipid
Lipid已安装,从示例应用开始,运行
$ vendor/bin/lipidstrap
将创建3个文件
index.php
- 这是您的应用,它由每个页面或请求的动作对象组成;src/ActIndex.php
- 这是主页的示例动作;tpl/index.twig
- 这是示例索引页模板。
然后启动您的应用
$ php -S localhost:8000 index.php
最后在浏览器中打开: https://:8000
享受结果,并开始创建您的应用页面。
如何创建动作(页面或API响应)
ActIndex.php
<?php class ActIndex implements Action { public function handle(Response $resp): Response { return $resp->withBody( "Hello, World 2!<br>" . '<a href="/login">login</a>' ); } }
如果需要数据库或GET参数,将其放在构造函数中
在此示例中 $_GET['test'] -> 与 $this->GET 数组
<?php class ActIndex implements Action { private $rqGet; public function __construct(array $GET = null) { $this->rqGet = $GET ?? $_GET; } public function handle(Response $resp): Response { $test = $this->GET['test'] ?? 'nothing'; return $resp->withBody( "Hello, World 2!<br>" . '<a href="/login">login</a>' . '$_GET[test]=' . htmlentities($test) ); } }
GET请求、POST请求、数据库、配置、环境、会话
<?php public function __construct( array $GET = null, AppPDO $db = null, Config $config = null, Request $env = null, Session $sess = null, Tpl $tpl // or anything you need for your Action ) { $this->POST = $GET ?? $_POST; $this->GET = $GET ?? $_GET; $this->db = $db ?? new AppPDO; $this->config = $config ?? new CfgFile; $this->env = $env ?? new RqENV; $this->tpl = $tpl ?? new AppTpl }
设计原则
- 真正的OOP
- 每个对象都是领域术语的表示
- 无静态
- 小型对象
- 不使用继承
- 广泛使用装饰器
- 严格的CI/CD管道:单元测试、PSR2检查器
- 不可变
- 受@yegor256 优雅对象 启发
-
微格式,就像lipid一样。
-
从
Lipid框架
到 Lipid作为过程(包括设计原则、规则、CI/CD)
库开发周期
-
克隆仓库
-
$ composer install
-
$ composer dump-autoload
-
$ composer example
-
打开浏览器: https://:8000/
-
查看源代码:example目录。
-
进行一些更改,为问题创建分支
$ git checkout -b 'Issue123'
-
检查并修复PSR2
$ composer phpcs
和$composer phpcs-fix
-
通过单元测试进行检查
$ composer tests
- 提交、推送并创建PR。
git commit -m 'Thats was done closes #123' -a
git push --set-upstream origin Issue123
PHPMD
@todo #75 setup and add phpmd to merge checks
要禁用某些讨厌的规则,请添加注释
- 运行phpmd:
$ composer phpmd-xml
- 查看规则名称
- 将字符串
@SuppressWarnings("rule name")
添加到方法或类的phpdoc块中
/**
* Class Title
*
* ..
* @SuppressWarnings("ElseExpression")
* ..
*/