hjerichen/class-instantiator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

hjerichen/class-instantiator

Composer 安装命令:

composer require hjerichen/class-instantiator

包简介

A factory for objects. Instantiates any class.

README 文档

README

Continuous Integration Coverage Status

Class Instantiator

The class instantiator tries to create an object of the passed class recursively.

<?php

use HJerichen\ClassInstantiator\ClassInstantiator;

class ClassA {

}

class ClassB {
    public function __construct(ClassA $instanceA) {}
}

$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);

Some arguments for a constructor cannot be created automatically. For example primitive types like integer or interfaces.
Or you want to inject a specific object, that should not be created automatically, like a PDO object.
For those scenarios, there are multiple solutions.

Pass predefined arguments:

<?php

use HJerichen\ClassInstantiator\ClassInstantiator;

class ClassA {
    public function __construct(PDO $database, int $id) {}
}

class ClassB {
    public function __construct(ClassA $instanceA) {}
}

$predefinedArguments = [
    'database' => new PDO('dsn'), 
    'id' => 5
];

$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class, $predefinedArguments);

Extend the class instantiator:

<?php

use HJerichen\ClassInstantiator\ClassInstantiator;

class ClassA {
    public function __construct(PDO $database, int $id) {}
}

class MyInstantiator extends ClassInstantiator {
    # The class instantiator will match the property name to the constructor parameter name
    private $id = 5;
    
    # The class instantiator will match the return type to the constructor parameter type
    public function getDatabase(): PDO
    {
        return new PDO('dsn');
    }
}

$instantiator = new MyInstantiator();
$object = $instantiator->instantiateClass(ClassA::class);

Use an attribute to define a class instantiator extension:

<?php

use HJerichen\ClassInstantiator\Attribute\Instantiator;
use HJerichen\ClassInstantiator\ClassInstantiator;

#[Instantiator(class: MyInstantiator::class)]
class ClassA {
    public function __construct(PDO $database, int $id) {}
}

class ClassB {
    public function __construct(ClassA $a) {}
}

class MyInstantiator extends ClassInstantiator {
    # The class instantiator will match the property name to the constructor parameter name
    private $id = 5;
    
    # The class instantiator will match the return type to the constructor parameter type 
    public function getDatabase(): PDO
    {
        return new PDO('dsn');
    }
}

$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);

Instantiate classes with an extension has a higher priority then using an attribute.
This allows overwriting the attribute and instantiate classes depending on your current needs.

You can also use the instantiator attribute to let the class build from a method in a specific instantiator:

<?php

use HJerichen\ClassInstantiator\Attribute\Instantiator;
use HJerichen\ClassInstantiator\ClassInstantiator;

#[Instantiator(class: MyInstantiator::class)]
class ClassA {
    public function __construct(PDO $database, int $id) {}
}

class ClassB {
    public function __construct(ClassA $a) {}
}

class MyInstantiator extends ClassInstantiator {    
    # The class instantiator will match the return type to the class type 
    public function buildClassA(): ClassA
    {
        $database = new PDO('dsn');
        return new ClassA($database, 55);
    }
}

$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);

The attribute can also be used on a constructor parameter:

<?php

use HJerichen\ClassInstantiator\Attribute\Instantiator;
use HJerichen\ClassInstantiator\ClassInstantiator;

class ClassA {
    public function __construct(PDO $database, int $id) {}
}

class ClassB {
    public function __construct(
        #[Instantiator(class: MyInstantiator::class)] ClassA $a
    ) {
    }
}

class MyInstantiator extends ClassInstantiator {    
    # The class instantiator will match the return type to the class type 
    public function buildClassA(): ClassA
    {
        $database = new PDO('dsn');
        return new ClassA($database, 55);
    }
}

$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);

The attribute on the parameter has a higher priority as the one on the class itself.

It is also possible to specify a method of the instantiator in the attribute.

<?php

use HJerichen\ClassInstantiator\Attribute\Instantiator;
use HJerichen\ClassInstantiator\ClassInstantiator;

class ClassA {
    public function __construct(PDO $database, int $id) {}
}

class ClassB {
    public function __construct(
        #[Instantiator(class: MyInstantiator::class, method: 'someMethod')] ClassA $a
    ) {
    }
}

class MyInstantiator extends ClassInstantiator {    
    # The class instantiator will not use this method to build a ClassA insance. 
    public function buildClassA(): ClassA
    {
        $database = new PDO('dsn');
        return new ClassA($database, 55);
    }
    # Is uses this method, because is is specified in the attribute.
    public function someMethod(): ClassA
    {
        $database = new PDO('dsn');
        return new ClassA($database, 60);
    }
}

$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);
Storing Objects

It is possible to sore objects permanently with an attribute.
Those objects will then be returned when a matching class is requested instead of creating a new one every time.

<?php

use HJerichen\ClassInstantiator\Attribute\Instantiator;

 #[Instantiator(class: MyInstantiator::class, store: true)]
class ClassA {
    public function __construct(PDO $database, int $id) {}
}

Storing objects with attribute does not work when the instantiation is done in the extension.
This is because of the higher priority for extensions.
But you can store objects as well when using the method "injectObject" inside the creation method of the extension.

Inject Objects

It is possible to inject objects into the class instantiator.
Those objects will be returned when a matching class is requested.

<?php

use HJerichen\ClassInstantiator\ClassInstantiator;

interface InterfaceA {}
class ClassA implements InterfaceA {}

$instantiator = new ClassInstantiator();

# only using the object resolves to this object when exactly this class is requested
$instantiator->injectObject(new ClassA());
$object = $instantiator->instantiateClass(ClassA::class);

# using the object and an interface as second parameter
# resolves to this object when exactly this interface is requested
$instantiator->injectObject(new ClassA(), InterfaceA::class);
$object = $instantiator->instantiateClass(InterfaceA::class);
PSR-11 Container

There is also a PSR-11 Container Implementation. You can use this container in frameworks and libraries who support those.
Example for GraphQLite:

<?php

use HJerichen\ClassInstantiator\ClassInstantiator;
use HJerichen\ClassInstantiator\ClassInstantiatorContainer;
use TheCodingMachine\GraphQLite\SchemaFactory;

# you can also inject an extension.
$container = new ClassInstantiatorContainer(new ClassInstantiator()); 
$factory = new SchemaFactory($cache, $container);
License and authors

This project is free and under the MIT Licence. Responsible for this project is Heiko Jerichen (heiko@jerichen.de).

hjerichen/class-instantiator 适用场景与选型建议

hjerichen/class-instantiator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.43k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 hjerichen/class-instantiator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-07