decodelabs/veneer
Composer 安装命令:
composer require decodelabs/veneer
包简介
Automated static facades
README 文档
README
Create automated static frontages for your PHP objects.
Use Veneer to provide easy access to your most commonly used functionality without sacrificing testability.
Install
This package requires PHP 8.4 or higher.
Install via Composer:
composer require decodelabs/veneer
Usage
Say you have a common library class you use regularly:
namespace Some\Random\Library; // This is a library class you use regularly class MyThing { public function doAThing() { echo 'Done!'; } }
You can bind a static, automatically generated frontage by:
namespace App\Setup; // This is your environment setup code use DecodeLabs\Veneer; use Some\Random\Library\MyThing; use App\CoolThing; Veneer::register( MyThing::class, // active object class CoolThing::class // frontage class ); namespace Some\Other\Code; use App\CoolThing; // Your general userland code CoolThing::doAThing();
Plugins
Unfortunately PHP still doesn't have __getStatic() yet so we have to statically declare plugin names at binding time, but they're still useful for creating more expansive interfaces.
Define plugins as properties on your FacadeTarget with a Plugin attribute. By default, plugins require manual instantiation in the constructor, however you can flag it as auto to have it automatically built at bind time, or lazy if it doesn't need to be loaded straight away.
namespace My\Library { use DecodeLabs\Veneer\Plugin; class MyThing { #[Plugin] public MyPlugin $plugin; #[Plugin(auto: true)] public MyPlugin $autoPlugin; #[Plugin(lazy: true)] public MyPlugin $lazyPlugin; public function __construct() { $this->plugin = new MyPlugin(); } } class MyPlugin { public function doAThing(): string { return 'Hello from plugin'; } } } namespace Some\Other\Code { use My\Library\MyThing; MyThing::$plugin->doAThing(); // Hello from plugin MyThing::$autoPlugin->doAThing(); // Hello from plugin MyThing::$lazyPlugin->doAThing(); // Hello from plugin }
Note, if your target class has a constructor with required parameters, you will need to add decodelabs/slingshot to your project to allow Veneer to instantiate it.
Lazy instantiation uses the new ghost and proxy functionality in PHP8.4 and will only instantiate the plugin when it is first accessed. Due to the limitations of lazy objects in PHP, you cannot create a lazy proxy for internal classes so you may find that plugins are referenced with a transparent Plugin\Wrapper class which resolves to the actual plugin instance when accessed. This usually isn't an issue unless you try to pass a plugin instance to a function that expects a specific class type, directly from the proxy. In these cases you should return the plugin instance from a method on the target class.
Property Hooks
PHP 8.4 property hooks can be used in combination with Plugins, however be aware that they will conflict with auto and lazy instantiation. Hooks defined with the structure below will effectively act like a lazy loaded plugin, however with the additional benefits of being able to control how it is instantiated rather than relying on Slingshot.
namespace My\Library { use DecodeLabs\Veneer\Plugin; class MyThing { #[Plugin] protected(set) MyPlugin $plugin { get => $this->plugin ??= new MyPlugin(); } } }
Hooks can be virtual (ie, don't require a value backed property), however the proxy will reference the first instantation of a virtual hook and your plugin instances will likely go out of sync.
Note, the protected(set) visibility in the example; it is not a requirement, but it is recommended to prevent direct write access to the property. If you need to replace a plugin instance, you should do so via Veneer::replacePlugin($providerInstance, 'propertyName', $newPlugin). This allows Veneer to update the plugin in the static frontage proxy as well as the target instance.
Licensing
Veneer is licensed under the MIT License. See LICENSE for the full license text.
decodelabs/veneer 适用场景与选型建议
decodelabs/veneer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37.89k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 「tools」 「facade」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 decodelabs/veneer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 decodelabs/veneer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 decodelabs/veneer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
A Laravel package to retrieve data from Google Search Console
Inbox pattern process implementation for your Laravel Applications
GHT D-Tools Bundle
Allow KoolReport to use twig template engine to render view
Asynchronous MQTT client built on React
统计信息
- 总下载量: 37.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-11