pdsinterop / solid-crud
适用于处理资源 CRUD 的 Solid HTTPS REST API 规范的兼容实现
Requires
- php: ^8.0
- ext-mbstring: *
- laminas/laminas-diactoros: ^2.14
- league/flysystem: ^1.0
- mjrider/flysystem-factory: ^0.7
- pdsinterop/flysystem-rdf: ^0.5
- pietercolpaert/hardf: ^0.3
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- textalk/websocket: ^1.5
Requires (Dev)
- phpunit/phpunit: ^9
README
适用于处理资源 CRUD 的 Solid HTTPS REST API 规范的兼容实现
Solid 规范扩展了Linked Data Platform 规范,用于在 Solid 服务器(或“Solid Pod”)中读取和写入资源。
该项目提供了一个单类 API,用于对资源和容器执行 CRUD 操作,该 API 遵守Solid HTTPS REST API 规范。
所使用的请求和响应对象符合PHP 标准推荐 HTTP 消息接口(PSR-7),以便更容易地将现有项目和框架集成。
目录
背景
该项目是 PDS Interop 的 PHP 项目堆栈的一部分。它被 Solid-Nextcloud 应用程序和独立的 PHP Solid 服务器所使用。
由于该功能对其他项目似乎很有用,因此将其作为独立包实现。
安装
建议通过 composer 安装
composer require pdsinterop/solid-crud
支持 PHP 版本 7.3 及以上。为了使此包正常工作,需要启用 mbstring
扩展。
使用
此包提供了一个 Pdsinterop\Solid\Resources\Server
类,当提供符合 PSR-7 的请求对象时,将返回符合 PSR-7 的响应对象。
要正常运行,服务器需要一个实现 League\Flysystem\FilesystemInterface
的对象和一个实现 Psr\Http\Message\ResponseInterface
的对象
具体的文件系统对象 必须 提供由 pdsinterop/flysystem-rdf
提供的 Rdf 适配器和 AsMime 插件。根据您的具体用例,也可以提供由同一包提供的 ReadRdf 插件。
创建文件系统
以下是如何使用 local 适配器 创建文件系统对象的示例
$formats = new \Pdsinterop\Rdf\Formats(); $rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf( new \League\Flysystem\Adapter\Local('/path/to/data'), new \EasyRdf_Graph(), $formats, 'https://example.com/' ); $filesystem = new \League\Flysystem\Filesystem($rdfAdapter); $filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats));
有关使用文件系统的更多详细信息,请参阅 pdsinterop/flysystem-rdf
的文档。
创建服务器
使用文件系统,可以创建服务器以及一个响应对象。在此示例中,我们使用的是 Laminas Diactoros 的 Response
对象,但任何符合 PSR-7 的响应对象都可以使用
$server = new \Pdsinterop\Solid\Resources\Server($filesystem, new \Laminas\Diactoros\Response());
处理请求
一旦创建了服务器,就可以提供请求来处理。在此示例中,请求对象是由符合 PSR-17 的 ServerRequest Factory(也由 Laminas Diactoros 包提供)创建的,但任何符合 PSR-7 的请求对象都可以使用
$request = \Laminas\Diactoros\ServerRequestFactory::fromGlobals( $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES ); $response = $server->respondToRequest($request);
这将填充并返回由服务器提供的响应对象,以便由您的应用程序或框架进一步处理。
更改请求
如果请求与服务器对象预期处理的请求之间存在差异,则应在将其传递给服务器之前更改请求对象。
例如,要更改请求的路径
$request = $request->withUri($request->getUri()->withPath($changedPath));
或请求方法
$request = $request->withMethod('PUT');
完整示例
将所有这些放在一起,我们将得到类似这样的结果
<?php /*/ Create the filesystem /*/ $formats = new \Pdsinterop\Rdf\Formats(); $rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf( new \League\Flysystem\Adapter\Local('/path/to/data'), new \EasyRdf_Graph(), $formats, 'https://example.com/' ); $filesystem = new \League\Flysystem\Filesystem($rdfAdapter); $filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats)); /*/ Create the server /*/ $server = new \Pdsinterop\Solid\Resources\Server($filesystem, new \Laminas\Diactoros\Response()); /*/ Create a PSR-7 Request object /*/ $request = \Laminas\Diactoros\ServerRequestFactory::fromGlobals( $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES ); /*/ Remove the `/data` prefix from the path /*/ $changedPath = substr($request->getUri()->getPath(), 5); $request = $request->withUri($request->getUri()->withPath($changedPath)); /*/ Handle the request /*/ $response = $server->respondToRequest($request);
一个完整的工作示例已在以下位置提供:src/example.php
。
要尝试这个示例服务器,请运行
php -S localhost:${PORT:-8080} -t ./src/ ./src/example.php
或通过调用:composer run dev:example
让 composer 为您运行
服务器预计将在 HTTPS 上运行,但它可以被强制接受
贡献
可以通过在 GitHub 上创建一个问题来提出问题或反馈。
所有 PDS Interop 项目都是开源的,并且对社区友好。任何贡献都受欢迎!有关更多详细信息,请阅读贡献指南。
所有 PDS Interop 项目都遵守代码宣言,作为其行为准则。贡献者应遵守其条款。
GitHub 上列出了所有贡献者。
要查看更改列表,请参阅变更日志或GitHub 发布页面。
许可证
PDS Interop 创建的所有代码都根据MIT 许可证授权。