helsingborg-stad / wpservice
通过提供集中化的WpService,以简洁的方式暴露全局WordPress函数,简化WordPress集成。轻松简化您的开发工作流程,并轻松增强WordPress集成。
1.48.0
2024-09-23 12:28 UTC
Requires
- php: ^8.1
Requires (Dev)
- helsingborg-stad/phpcs: ^0.3.5
- php-stubs/wordpress-stubs: ^6.6
- phpunit/phpunit: ^9.6
- dev-main
- 1.48.0
- 1.47.0
- 1.46.0
- 1.45.0
- 1.44.4
- 1.44.2
- 1.44.1
- 1.43.0
- 1.42.0
- 1.41.0
- 1.40.6
- 1.40.5
- 1.40.2
- 1.39.0
- 1.38.0
- 1.37.1
- 1.37.0
- 1.36.0
- 1.35.0
- 1.34.1
- 1.33.2
- 1.33.0
- 1.32.2
- 1.32.1
- 1.32.0
- 1.31.2
- 1.31.0
- 1.30.0
- 1.29.0
- 1.28.0
- 1.27.0
- 1.26.0
- 1.25.2
- 1.25.1
- 1.25.0
- 1.24.0
- 1.23.0
- 1.22.2
- 1.22.1
- 1.22.0
- 1.21.0
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.1
- 1.16.0
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.3
- 1.11.2
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- dev-wp_safe_redirect
- dev-nonce
- dev-feat/attachments-and-files
- dev-feat/getTaxonomy
- dev-feat/updatePost
- dev-feat/getPostTypeObject
- dev-feat/getObjectTaxonomies
- dev-feat/more-functions
- dev-fix/renaming-wp
- dev-wp_remote_retrieve_body
- dev-wp_remote_get
- dev-update_option
- dev-get_post_types
This package is auto-updated.
Last update: 2024-09-23 12:28:48 UTC
README
WpService
通过提供集中化的WpService,以简洁的方式暴露全局WordPress函数,简化WordPress集成。轻松简化您的开发工作流程,并轻松增强WordPress集成。
关于WpService
通过应用接口分离,在插件和主题中启用全局WordPress函数的使用。可以通过应用可用的装饰器来使用WordPress服务的不同版本。
入门
安装
- 通过composer安装包
composer require helsingborg-stad/wpservice
- 在您的插件或主题中使用WpService
use WpService\Implementations\NativeWpService; $wpService = new NativeWpService(); $wpService->getTheId();
装饰器
文本域装饰器
应用于翻译函数的默认文本域。
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.
在测试中使用WpService
为了使测试依赖于WpService或其部分的代码更容易,提供了一个FakeWpService实现。当您想在不依赖WordPress函数的情况下测试代码时,此实现非常有用。
示例
假设您有一个以下类,它使用WpService的一部分
use WpService\Contracts\GetTheId; class MyService { public function __construct(private GetTheId $wpService) { } public function getCurrentPostId(): int { return $this->wpService->getTheId(); } }
然后您可以在测试中使用FakeWpService来模拟WpService的结果以及验证对WpService函数的调用
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); } }
传递返回值到FakeWpService
FakeWpService构造函数接受一个键值对数组,其中键是方法名称,值是方法的返回值。
# 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'
构建工具
- PHP
测试
运行测试
在终端中运行 composer test
。
贡献
贡献使得开源社区成为一个如此美妙的学习、灵感和创造的地方。您所做出的任何贡献都将被 高度赞赏。
- 分支项目
- 创建功能分支 (
git checkout -b feature/AmazingFeature
) - 提交更改 (
git commit -m 'Add some AmazingFeature'
) - 推送到分支 (
git push origin feature/AmazingFeature
) - 打开Pull Request
许可协议
在 MIT 许可协议 下分发。