PSR笔记
PSR 是 PHP Standard Recommendations (PHP 推荐标准)的简写,由 PHP FIG 组织制定的 PHP 规范,是 PHP 开发的实践标准。
1. PSR-0(弃用)
2. PSR-1(编码风格)
- 使用PSR-4规范自动加载规范,命名空间
- 类名使用大坨峰
- 类中的常量使用全大写,下划线
- 方法名使用小驼峰
3. PSR-2(编码风格)
- 每个namespace和use块后空一行
- 花括号自成一行
- 控制结构的”{}” ,写在同一行
- true,false,null必须小写
- “()”前后不能有空格
- “}” 和 “(“ 中间有空格
4. PSR-3(日志规范)
- LoggerInterface 定义了8个等级的日志
- Psr\Log\LoggerInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| <?php
namespace Psr\Log;
interface LoggerInterface {
public function emergency($message, array $context = array());
public function alert($message, array $context = array());
public function critical($message, array $context = array());
public function error($message, array $context = array());
public function warning($message, array $context = array());
public function notice($message, array $context = array());
public function info($message, array $context = array());
public function debug($message, array $context = array());
public function log($level, $message, array $context = array()); }
|
5. PSR-4(自动加载)
- 必须拥有一个顶级命名空间,可以有几个子命名空间
- 必须有一个类名
- 区分大小写
6. PSR-5(未通过)
7.PSR-6(缓存接口规范)
- Psr\Cache\CacheItemInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| <?php
namespace Psr\Cache;
interface CacheItemInterface {
public function getKey();
public function get();
public function isHit();
public function set($value);
public function expiresAt($expiration);
public function expiresAfter($time);
}
|
- Psr\Cache\CacheItemPoolInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| <?php
namespace Psr\Cache;
interface CacheItemPoolInterface {
public function getItem($key);
public function getItems(array $keys = array());
public function hasItem($key);
public function clear();
public function deleteItem($key);
public function deleteItems(array $keys);
public function save(CacheItemInterface $item);
public function saveDeferred(CacheItemInterface $item);
public function commit(); }
|
8. PSR-7 (HTTP消息接口规范)
- RequestInterface 和ResponseInterface都继承于MessageInterface
- Psr\Http\Message\RequestInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| <?php namespace Psr\Http\Message;
interface RequestInterface extends MessageInterface {
public function getRequestTarget();
public function withRequestTarget($requestTarget);
public function getMethod();
public function withMethod($method);
public function getUri();
public function withUri(UriInterface $uri, $preserveHost = false); }
|
- Psr\Http\Message\ResponseInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?php namespace Psr\Http\Message;
interface ResponseInterface extends MessageInterface {
public function getStatusCode();
public function withStatus($code, $reasonPhrase = '');
public function getReasonPhrase(); }
|
9. PSR-8 到PSR-10 未通过
10. PSR-11 (容器接口)
设定 ContainerInterface 的目的是为了标准化框架或类库如何使用容器来获取对象和参数。
- Psr\Container\ContainerInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?php namespace Psr\Container;
interface ContainerInterface {
public function get($id);
public function has($id); }
|
11. PSR-15 (HTTP请求处理器)
主要用于描述抽象的请求处理器和中间件
- Psr\Http\Server\RequestHandlerInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| namespace Psr\Http\Server;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;
interface RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface; }
|
- Psr\Http\Server\MiddlewareInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| namespace Psr\Http\Server;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;
interface MiddlewareInterface {
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface; }
|
12. PSR-16(更简单的缓存接口)
- Psr\SimpleCache\CacheInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| <?php
namespace Psr\SimpleCache;
interface CacheInterface {
public function get($key, $default = null);
public function set($key, $value, $ttl = null);
public function delete($key);
public function clear();
public function getMultiple($keys, $default = null);
public function setMultiple($values, $ttl = null);
public function deleteMultiple($keys);
public function has($key); }
|
13. PSR-17(HTTP工厂)
HTTP 工厂是可以创建由 PSR-7 定义的 HTTP 对象的方法。HTTP 工厂 必须 实现包中提供的所有对象类型。
- Psr\Http\Message\RequestFactoryInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| namespace Psr\Http\Message;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface;
interface RequestFactoryInterface {
public function createRequest(string $method, $uri): RequestInterface; }
|
- Psr\Http\Message\ResponseFactoryInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| namespace Psr\Http\Message;
use Psr\Http\Message\ResponseInterface;
interface ResponseFactoryInterface {
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface; }
|
先写这么多。