helsingborg-stad/wpservice
Composer 安装命令:
composer require helsingborg-stad/wpservice
包简介
Simplifies WordPress integration by providing a centralized WpService that exposes global WordPress functions in a streamlined manner. Simplify your development workflow and enhance WordPress integration with ease.
README 文档
README
WpService
Simplifies WordPress integration by providing a centralized WpService that exposes global WordPress functions in a streamlined manner. Simplify your development workflow and enhance WordPress integration with ease.
About WpService
Enable the use of global WordPress functions in plugins and themes where applying Interface Segregation. Different flavors of the WordPress Service can be utilized by applying available decorators.
Getting Started
Installation
- Install the package via composer:
composer require helsingborg-stad/wpservice
- Use the WpService in your plugin or theme:
use WpService\Implementations\NativeWpService; $wpService = new NativeWpService(); $wpService->getTheId();
Decorators
Text Domain Decorator
Applies default text domain to translation functions.
use WpService\Implementations\WpServiceWithTextDomain; use WpService\Implementations\NativeWpService; $service = new NativeWpService(); $service = new WpServiceWithTextDomain($service, 'my-text-domain'); $service->__('Hello World'); // Will automatically use 'my-text-domain' as the text domain.
Using WpService in tests
To make it easier when testing code that depends on the WpService or parts of it, a FakeWpService implementation is available. This implementation is useful when you want to test your code without having to rely on the WordPress functions.
Example
Consider that you have the following class that utilizes a part of the WpService:
use WpService\Contracts\GetTheId; class MyService { public function __construct(private GetTheId $wpService) { } public function getCurrentPostId(): int { return $this->wpService->getTheId(); } }
You can then use FakeWpService in your tests to fake the results of the WpService as well as veifyin the calls made to the WpService functions:
use WpService\Implementations\FakeWpService; use PHPUnit\Framework\TestCase; class MyServiceTest extends TestCase { public function testGetCurrentPostId() { // Given $fakeWpService = new FakeWpService(['getTheId' => 123]); $myService = new MyService($fakeWpService); // When $postId = $myService->getCurrentPostId(); // Then $this->assertEquals([123], $wpService->methodCalls['isSingle'][0]); $this->assertEquals(123, $postId); } }
Passing return values to the FakeWpService
The FakeWpService constructor accepts an array of key-value pairs where the key is the name of the method and the value is the return value of the method.
# Using a generic return value for all calls to the method. $fakeWpService = new FakeWpService(['getTheTitle' => 'Test Title']); $fakeWpService->getTheTitle(); // Returns 'Test Title' $fakeWpService->getTheTitle(321); // Returns 'Test Title' $fakeWpService->getTheTitle(123); // Returns 'Test Title' # Using a callback to determine the return value based on the arguments passed to the method. $return = fn($postId) => $postId === 123 ? 'Test Title' : ''; $fakeWpService = new FakeWpService(['getTheId' => $return]); $fakeWpService->getTheTitle(); // Returns '' $fakeWpService->getTheTitle(321); // Returns '' $fakeWpService->getTheTitle(123); // Returns 'Test Title'
Built With
- PHP
Tests
Run tests
Run composer test in the terminal.
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
Distributed under the MIT License.
helsingborg-stad/wpservice 适用场景与选型建议
helsingborg-stad/wpservice 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 54.54k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 helsingborg-stad/wpservice 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 helsingborg-stad/wpservice 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 54.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 45
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-24