gyselroth / micro
Micro PHP 库
Requires
- php: >=7.1
- psr/container: *
- psr/log: 1.*
Requires (Dev)
- phpunit/phpunit: 5.7.*
This package is not auto-updated.
Last update: 2018-02-01 12:04:01 UTC
README
使用 micro 组件替代
- micro-auth
- micro-log
- micro-config
- micro-container
- micro-http
Micro (又一 PHP 库)
...但不是废话
描述
Micro 提供了最基本的核心功能来编写新的应用程序。它不提供一个功能丰富的库,而只提供几个命名空间。它包含一个日志记录器(和多个适配器)、配置解析器、HTTP 路由/响应、认证(和多个适配器)以及一些数据库和 ldap 的包装器。
- \Micro\Auth
- \Micro\Config
- \Micro\Container
- \Micro\Http
- \Micro\Log
要求
该库仅与 >= PHP7.1 兼容。
下载
该包可在 Packagist 上找到:https://packagist.org.cn/packages/gyselroth/micro
要使用 composer 安装此包,请执行
composer require gyselroth/micro
配置 (\Micro\Config)
读取
简单读取 xml 配置并初始化您的配置
$config = new \Micro\Config(new \Micro\Config\Xml($path)); var_dump($config->myconfigentry); string(1) "1"
您的实际 XML 配置将如下所示
<config version="1.0"> <production> <myconfigentry>1</myconfigentry> </production> </config>
每个配置都有配置环境,如果您只有一个,只需将其作为 . 中的第一个节点即可。有关更多信息,请参阅环境。
合并
合并多个配置文件
$config1 = new \Micro\Config\Xml($path); $config2 = new \Micro\Config\Xml($path); $config1->merge($config2); $config = new \Micro\Config($config1);
环境
您可以请求自定义配置环境
$config = new \Micro\Config(new \Micro\Config\Xml($path, 'development')); var_dump($config->myconfigentry); string(1) "2"
而您的 XML 配置将如下所示
<config version="1.0"> <production> <myconfigentry>1</myconfigentry> </production> <development> <myconfigentry>2</myconfigentry> </development> </config>
继承
配置解析器支持继承。一个简单的例子就是从一个环境继承另一个环境
<config version="1.0"> <production> <a>a</a> </production> <development inherits="production"> </development> </config>
$config = new \Micro\Config(new \Micro\Config\Xml($path, 'development')); var_dump($config->a); string(1) "a"
您还可以在一个环境中继承单个元素(递归),并覆盖最初继承的元素
<config version="1.0"> <production> <a>a</a> <b inherits="a"/> <c> <child>c</child> </c> <d inherits="c"> <child2>d</child2> </d> </development> </config>
$config = new \Micro\Config(new \Micro\Config\Xml($path)); var_dump($config->a); string(1) "a" var_dump($config->b); string(1) "a" var_dump($config->c->child); string(1) "c" var_dump($config->d->child); var_dump($config->d->child2); string(1) "c" string(1) "d"
您也可以访问任何元素(节点路径由点号分隔)
<config version="1.0"> <production> <a> <child> <subchild>a</subchild> </child> </a> <b inherits="c"> <child inherits="a.child"> <child2 inherits="a.child.subchild"> </b> </development> </config>
$config = new \Micro\Config(new \Micro\Config\Xml($path)); var_dump($config->a->child->subchild); string(1) "a" var_dump($config->b->child->subchild); var_dump($config->b->child2); string(1) "a" string(1) "a"
XML 属性
XML 属性和 XML 节点之间不再有区别。 \Micro\Config\Xml 会以相同的方式解析两者。因此,您可以选择或随时切换一个名称/值是否应该是一个属性或一个节点。
<config version="1.0"> <production> <a enabled="1"/> </development> </config>
这意味着上述配置将被解析为与以下相同的 Config 对象
<config version="1.0"> <production> <a> <enabled>1</enabled> </a> </development> </config>
日志记录器 (\Micro\Log)
描述
\Micro\Log 是一个 PSR-3 兼容的日志记录器,具有多个日志适配器。
初始化
$logger = new Logger(Iterable $options); $logger->info(string $message, array $context);
配置
$logger = new Logger([ 'adapter_name' => [ 'class' => '\Micro\Log\Adapter\File', 'config' => [ 'file' => '/path/to/file', 'date_format' => 'Y-d-m H:i:s', //https://php.ac.cn/manual/en/function.date.php 'format' => '{date} {level} {message} {context.category}', 'level' => 7 //PSR-3 log levels 1-7 ], ], 'adapter2_name' => [] ]);
当然,您也可以使用配置对象(以及其他任何可迭代的对象)来初始化记录器。
<log> <adapter_name enabled="1" class="\Micro\Log\Adapter\File"> <config> <file>/path/to/file</file> <date_format>Y-d-m H:i:s</date_format> <format>{date} {level} {message} {context.category}</format> <level>7</level> </config </adapter_name> </log>
$config = new \Micro\Config(new \Micro\Config\Xml($path)); $logger = new Logger($config);
格式化
消息格式是在每个适配器中单独配置的。可用的变量有
- {message} - 消息本身
- {date} - 当前时间戳,格式化为配置的 date_format 选项
- {level} - 日志级别,配置的数字将被替换为字符串,例如 7 => debug
- {context.} - 您可以访问每个上下文选项并将它们包含在消息格式中。例如,如果您有一个上下文 ['category' => 'router'],那么您可以将 {context.category} 配置为在消息中包含此上下文值。
日志适配器
- \Micro\Log\Adapter\File
- \Micro\Log\Adapter\Blackhole
- \Micro\Log\Adapter\Stdout
- \Micro\Log\Adapter\Syslog
您可以使用 \Micro\Log\Adapter\AdapterInterface 创建自己的日志适配器。
HTTP (\Micro\Http)
初始化路由器
HTTP 路由器需要一个包含 HTTP 头部的数组,通常这是 $_SERVER,以及一个 PSR-3 兼容的记录器。
$router = new \Micro\Http\Router(array $server, \Psr\Log\LoggerInterface $logger)
添加路由
$router = (new \Micro\Http\Router($_SERVER, $logger)) ->clearRoutingTable() ->addRoute(new \Micro\Http\Router\Route('/api/v1/user', 'MyApp\Rest\v1\User')) ->addRoute(new \Micro\Http\Router\Route('/api/v1/user/{uid:#([0-9a-z]{24})#}', 'MyApp\Rest\v1\User')) ->addRoute(new \Micro\Http\Router\Route('/api/v1$', 'MyApp\Rest\v1\Rest')) ->addRoute(new \Micro\Http\Router\Route('/api/v1', 'MyApp\Rest\v1\Rest')) ->addRoute(new \Micro\Http\Router\Route('/api$', 'MyApp\Rest\v1\Rest')); ->run(array $controller_params);
路由器尝试将请求映射到其路由表中的第一个匹配路由。请求将被映射到一个类和方法的组合。可选参数/查询字符串将自动提交到最终的控制器类。
给定上述路由表和以下最终控制器类
namespace MyApp\Rest\v1; class User { /** * GET https:///api/v1/user/540f1fc9a641e6eb708b4618/attributes * GET https:///api/v1/user/attributes?uid=540f1fc9a641e6eb708b4618 */ public function getAttributes(string $uid=null): \Micro\Http\Response { } /** * GET https:///api/v1/user/540f1fc9a641e6eb708b4618 * GET https:///api/v1/user?uid=540f1fc9a641e6eb708b4618 */ public function get(string $uid=null): \Micro\Http\Response { } /** * POST https:///api/v1/user/540f1fc9a641e6eb708b4618/password / POST body password=1234 * POST https:///api/v1/user/password?uid=540f1fc9a641e6eb708b4618 / POST body password=1234 * POST https:///api/v1/user/password / POST body password=1234, uid=540f1fc9a641e6eb708b4618 */ public function postPassword(string $uid, string $password): \Micro\Http\Response { } /** * DELETE https:///api/v1/user/540f1fc9a641e6eb708b4618/mail * DELETE https:///api/v1/user/mail?uid=540f1fc9a641e6eb708b4618 */ public function deleteMail(string $uid=null): \Micro\Http\Response { } /** * DELETE https:///api/v1/540f1fc9a641e6eb708b4618/mail * DELETE https:///api/v1/user?uid=540f1fc9a641e6eb708b4618 */ public function delete(string $uid=null): \Micro\Http\Response { } /** * HEAD https:///api/v1/user/540f1fc9a641e6eb708b4618 * HEAD https:///api/v1/user?uid=540f1fc9a641e6eb708b4618 */ public function headExists(string $uid=null): \Micro\Http\Response { } }
响应
每个端点都需要向路由器返回一个 Response 对象。
/** * HEAD https:///api/v1/user/540f1fc9a641e6eb708b4618 * HEAD https:///api/v1/user?uid=540f1fc9a641e6eb708b4618 */ public function headExists(string $uid=null): \Micro\Http\Response { if(true) { return (new \Micro\Http\Response())->setCode(200)->setBody('user does exists'); } else { return (new \Micro\Http\Response())->setCode(404); } }