bingo-soft / util
PHP 工具类
1.8.3
2024-04-02 16:49 UTC
Requires
- php: ^8.0
- symfony/process: ^7.0
Requires (Dev)
README
util
PHP 工具类
安装
使用 Composer 安装库
composer require bingo-soft/util
代理类
/* Original class */ interface BusinessServiceInterface { } class BusinessServiceImpl { public function doSomething(int $id, string $name) { return "$id - $name"; } } /* Handler */ use Util\Proxy\MethodHandlerInterface; class DoSomethingMethodHandler implements MethodHandlerInterface { public function invoke($proxy, \ReflectionMethod $thisMethod, \ReflectionMethod $proceed, array $args) { return 'prepend - ' . $proceed->invoke($proxy, ...$args); } } /* Custom proxy factory*/ use Util\Proxy\{ MethodHandlerInterface, ProxyFactory }; class MyProxyFactory { public static function createProxy(string $type, MethodHandlerInterface $method, array $args = []) { $enhancer = new ProxyFactory(); $enhancer->setSuperclass($type); $enhancer->setInterfaces([ BusinessServiceInterface::class ]); return $enhancer->create($args); } } /* Creation and test of proxy class*/ $method = new DoSomethingMethodHandler(); $proxy = MyProxyFactory::createProxy(BusinessServiceImpl::class, $method); $proxy->setHandler($method); echo $proxy->doSomething(1, "curitis"); // will print "prepend - 1 - curitis"
增强反射
/* Set nested property */ use Tests\Domain\Misc\RichType; use Util\Reflection\SystemMetaObject; $rich = new RichType(); $meta = SystemMetaObject::forObject($rich); $meta->setValue("richType.richField", "foo"); echo $meta->getValue("richType.richField"); // new RichType( richType => new RichType ( richField => "foo" )) /* Create meta object from array */ $map = []; $metaMap = SystemMetaObject::forObject($map); $metaMap->setValue("id", "100"); $metaMap->setValue("name.first", "Clinton"); print_r($map); // [ "id" => 100, "name" => [ "first" => "Clinton" ]]
运行测试
./vendor/bin/phpunit ./tests