承接 eniams/spy 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eniams/spy

Composer 安装命令:

composer require eniams/spy

包简介

Handler to know if an object was modified

README 文档

README

Build Status

Spy helps you to know if an object was modified and allow you to fire/listen an event when the given object is modified.

⚠️ This project is a work in progress. ⚠️

Installation

$ composer require eniams/spy

How it works ?

Spy Workflow

SpyBase Workflow

Behind the scene:

The initial object will be copied with a specific cloner then value of the initial (copied) object and the manipulated (current) object will be compared on demand to check if there is some modifications.

Suppose you want to spy a foo object to know if it was modified :

  1. Tag the class to spy with an interface that will correspond to the chosen cloner, there is 2 built-in cloners :
  • Eniams\Spy\Cloner\DeepCopyClonerInterface that use the famous library DeepCopy

or

  • Eniams\Spy\Cloner\SpyClonerLoadPropertyObjectInterface or Eniams\Spy\Cloner\SpyClonerInterface that allows you to clone more deeper the object/array stored in properties
<?php
namespace App\Entity\Foo;

class Foo implements \Eniams\Spy\Cloner\DeepCopyClonerInterface
// or SpyClonerLoadPropertyObjectInterface 
// or SpyClonerInterface
{}

You can create a custom Cloner to copy your object :

  • Create the Cloner that should implements Eniams\Spy\Cloner\ClonerInterface.
  • Create an interface related to the created Cloner that should implements Eniams\Spy\SpyInterface.
<?php
namespace App\Service;
// Create the Cloner
class UserLandCloner implements \Eniams\Spy\Cloner\ClonerInterface
{
    public function doClone($object)
    {
        // Stuff to clone the given $object.
    }
    
    public function supports($object): bool
    {
        return $object instanceof UserLandClonerInterface;
    }   
}

// Create the Interface
interface UserLandClonerInterface extends \Eniams\Spy\SpyInterface

// Use your Cloner (Implement the created interface in the class) 
class Foo implements \Eniams\Spy\Cloner\UserLandClonerInterface

If you're using Symfony thanks to the autoconfigure tags you don't have to follow the next step, the created cloner will be registered to the ChainCloner that is responsible to clone the oject to spy. So you can go to step 3.

  1. For Vanilla PHP if you don't want to use the default cloners you can Register yours in the Eniams\Spy\ClonerChainCloner
<?php
 $chainCloner = new \Eniams\Spy\Cloner\ChainCloner([new UserLandCloner()]);
  1. Time to spy your object :shipit:
<?php
// $chainCloner is optional and need to be use only if you want to use a custom cloners,
// for Symfony remember that your custom cloner is already registered in the `ChainCloner $chainCloner` and it is a public service that can be retrieve from the container.
$spied = new \Eniams\Spy\Spy($foo, $chainCloner); 

$spied->isModified();
$spied->isNotModified();
Now, you want to want to know if a specific property was modified and get the initial and the current value.
<?php
$foo = (new \App\Entity\Foo())->setName('Smaone');

$spied = new \Eniams\Spy\Spy($foo, $chainCloner);

$foo->setName('Dude');

$spied->isPropertyModified('name'); // output true

$propertyState = $spied->getPropertyState('name');

$propertyState->getFqcn(); // App\Entity\Foo
$propertyState->getProperty(); // 'name'
$propertyState->getInitialValue(); // 'Smaone'
$propertyState->getCurrentValue(); // 'Dude'
Working with Services container you can store an object in the SpyBase to retrieve it later in your application
Symfony
<?php
class FooController extends AbstractController
{
    /**
     * @Route("/foo", name="foo")
     */
    public function index(SpyBase $spyBase)
    {
        $user = (new \Foo\Entity\User())->setName('Smaone');
        $spyBase = (new \Eniams\Spy\SpyBase());
        $spyBase->add('your_key', $user);
        
Vanilla PHP
<?php
$user = (new \Foo\Entity\User())->setName('Smaone');
$spyBase = (new \Eniams\Spy\SpyBase());
$spyBase->add('your_key', $user); // behind the scene $object is converted to a \Eniams\Spy\Spy object and the cloner class will be resolve by the interface implemented by the $object.

$yourContainer
    ->register('spy_base_service', $spyBase);

$spyBase = $yourContainer->get('spy_base_service');
// fetch the object
$spyBase->get('your_key');

// remove
$spyBase->remove('your_key');
For simple use case that don't need to clone an object, you can also check the difference between 2 "same" classes.
<?php
$firstUser = (new App\Entity\User())->setName('Smaone');
$secondUser = (new App\Entity\User())->setName('Dude');

$propertyState = Eniams\Spy\Property\PropertyStateFactory::createPropertyState('name', $firstUser, $secondUser);

$propertyState->getFqcn(); // App\Entity\User
$propertyState->getProperty(); // 'name'
$propertyState->getInitialValue(); // 'Smaone'
$propertyState->getCurrentValue(); // 'Dude'
More advanced use case
You can define a context to check some properties.
<?php
class User implements SpyClonerInterface, PropertyCheckerContextInterface {

private $age;
private $adresse;
private $firstname;
 public static function propertiesInContext(): array
    {
        return [
            'context_check_firstname' => ['firstname', 'age'],
            'context_check_adresse' => ['adresse'],
        ];
    }
}

// index.php
$spied->isModifiedInContext(['context_check_firstname']); // true only if 'firstname', 'age' were modified
$spied->isModifiedInContext(['context_check_adresse']); // true only if 'adresse' is modified
$spied->getPropertiesModifiedInContext(['context_check_adresse']); // return modified properties for context context_check_adresse
You can define dynamically which properties to check
<?php
class User implements SpyClonerInterface{

private $age;
private $adresse;
private $firstname;
}

// index.php
$spied->isModifiedForProperties(['age']); // true only if age was modified
You can exclude some properties.
<?php
class User implements SpyClonerInterface, PropertyCheckerBlackListInterface {

private $age;
private $adresse;
private $firstname;

 public static function propertiesBlackList(): array
    {
        return ['age'];
    }
}
// index.php
$user->setAge(33);
$spied->isModified(); // return false because $age is blacklisted
$spied->getPropertiesModifiedWithoutBlackListContext(); // return age even it's blacklisted

eniams/spy 适用场景与选型建议

eniams/spy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 11, 最近一次更新时间为 2020 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「event」 「object」 「immutable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 eniams/spy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 eniams/spy 我们能提供哪些服务?
定制开发 / 二次开发

基于 eniams/spy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-29