theodorejb/phaster
Composer 安装命令:
composer require theodorejb/phaster
包简介
A PSR-7 compatible library for making CRUD API endpoints
README 文档
README
Phaster is a PSR-7 compatible library for easily making CRUD API endpoints. It enables mapping API fields to columns in the database or from a select query, and implementing custom validation or row modification logic. Built on top of PeachySQL.
Installation
composer require devtheorem/phaster
Usage
Create a class extending Entities, and implement the methods to map properties to columns and
define the base SQL query (see example below).
Pass the callable returned by the route handling functions to your Slim or other PSR-7 compatible framework.
<?php use DevTheorem\Phaster\{Entities, Prop, QueryOptions}; class Users extends Entities { // Return the table to insert/update/delete/select from. // If not implemented, the class name will be used as the table name. protected function getTableName(): string { return 'users'; } // Return an array which maps properties to writable column names. Returns an // empty array by default, so no properties will be writable if not implemented. protected function getMap(): array { return [ 'username' => 'uname', 'firstName' => 'fname', 'lastName' => 'lname', 'isDisabled' => 'disabled', 'role' => [ 'id' => 'role_id', ], ]; } // Return a list of `Prop` objects, which map properties to readable columns and/or values, // and enable setting a type to cast the column value to, or defining a virtual property // which depends on other columns. See the Prop constructor for the full list of parameters // that can be set. Returns an empty array by default. protected function getSelectProps(): array { return [ new Prop('id', col: 'u.user_id'), new Prop('username', col: 'u.uname'), new Prop('firstName', col: 'u.fname'), new Prop('lastName', col: 'u.lname'), new Prop('isDisabled', col: 'u.disabled', type: 'bool'), new Prop('role.id', col: 'u.role_id'), new Prop('role.name', col: 'r.role_name'), ]; } // Return a SELECT SQL query (without a WHERE clause) in order to join other tables when // selecting data. If not implemented, mapped columns will be selected from the table // returned by getTableName(). protected function getBaseQuery(QueryOptions $options): string { return "SELECT {$options->getColumns()} FROM users u INNER JOIN roles r ON r.role_id = u.role_id"; } // Set the default column/direction for sorting selected results. If not implemented, // results will be sorted by the id property in ascending order by default. protected function getDefaultSort(): array { return ['username' => 'asc']; } }
If it is necessary to bind parameters in the base query, use getBaseSelect() instead:
use DevTheorem\PeachySQL\QueryBuilder\SqlParams; // ... protected function getBaseSelect(QueryOptions $options): SqlParams { $sql = "WITH num_orders AS ( SELECT user_id, COUNT(*) as orders FROM Orders WHERE category_id = ? GROUP BY user_id ) SELECT {$options->getColumns()} FROM Users u INNER JOIN num_orders n ON n.user_id = u.user_id"; return new SqlParams($sql, [321]); } // ...
<?php use My\DatabaseFactory; use DevTheorem\Phaster\{Entities, EntitiesFactory}; class MyEntitiesFactory implements EntitiesFactory { public function createEntities($class): Entities { return new $class(DatabaseFactory::getDb()); } }
<?php use DevTheorem\Phaster\RouteHandler; $phaster = new RouteHandler(new MyEntitiesFactory()); $app->get('/users', $phaster->search(Users::class)); $app->get('/users/{id}', $phaster->getById(Users::class)); $app->post('/users', $phaster->insert(Users::class)); $app->put('/users/{id}', $phaster->update(Users::class)); $app->patch('/users/{id}', $phaster->patch(Users::class)); $app->delete('/users/{id}', $phaster->delete(Users::class));
Example API request/response
GET https://example.com/api/users?q[firstName]=Ted&q[isDisabled]=0&fields=id,username,role
{
"offset": 0,
"limit": 25,
"lastPage": true,
"data": [
{
"id": 5,
"username": "Teddy01",
"role": {
"id": 1,
"name": "Admin"
}
},
{
"id": 38,
"username": "only_tj",
"role": {
"id": 2,
"name": "Standard"
}
}
]
}
Author
Theodore Brown
https://theodorejb.me
License
MIT
theodorejb/phaster 适用场景与选型建议
theodorejb/phaster 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.52k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2017 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「rest」 「api」 「crud」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 theodorejb/phaster 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 theodorejb/phaster 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 theodorejb/phaster 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Api bundle
PHP module for MySql database
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
统计信息
- 总下载量: 3.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-12