定制 charcoal/factory 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

charcoal/factory

Composer 安装命令:

composer require charcoal/factory

包简介

Charcoal object creation (Factory, AbstractFactory, Builder, Class Resolver)

关键字:

README 文档

README

The Factory package provides abstract object factories to create objects.

Installation

composer require charcoal/factory

Usage

Factories can resolve a type to a FQN and create instance of this class with an optional given set of arguments, while ensuring a default base class.

Factory options should be set directly from the constructor:

$factory = new Charcoal\Factory\GenericFactory([
    // Ensure the created object is a Charcoal Model
    'base_class' => '\Charcoal\Model\ModelInterface',

    // An associative array of class map (aliases)
    'map' => [
        'foo' => '\My\Foo',
        'bar' => '\My\Bar',
    ],

    // Constructor arguments
    'arguments' => [
        $dep1,
        $dep2,
    ],

    // Object callback
    'callback' => function ($obj) {
        $obj->do('foo');
    },
]);

// Create a "\My\Custom\Baz" object with the given arguments + callbck
$factory->create('\My\Custom\Baz');

// Create a "\My\Foo" object (using the map of aliases)
$factory->create('foo');

// Create a "\My\Custom\FooBar" object with the default resolver
$factory->create('my/custom/foo-bar');

Constructor options (class dependencies) are:

Name Type Default Description
base_class string '' Optional. A base class (or interface) to ensure a type of object.
default_class string '' Optional. A default class, as fallback when the requested object is not resolvable.
arguments array [] Optional. Constructor arguments that will be passed along to created instances.
callback callable null Optional. A callback function that will be called upon object creation.
resolver callable null[1] Optional. A class resolver. If none is provided, a default will be used.
resolver_options array null Optional. Resolver options (prefix, suffix, capitals and replacements). This is ignored / unused if resolver is provided.

Notes:

  • [1] If no resolver is provided, a default \Charcoal\Factory\GenericResolver will be used.

Class resolver

The type (class identifier) sent to the create() method can be parsed / resolved with a custom Callable resolver.

If no resolver is passed to the constructor, a default \Charcoal\Factory\GenericResolver is used. This default resolver transforms, for example, my/custom/foo-bar into \My\Custom\FooBar.

Class map and aliases

Class aliases can be added by setting them in the Factory constructor:

$factory = new GenericFactory([
    'map' => [
        'foo' => '\My\Foo',
        'bar' => '\My\Bar',
    ],
]);

// Create a `\My\Foo` instance
$obj = $factory->create('foo');

Ensuring a type of object

Ensuring a type of object can be done by setting the base_class property.

The recommended way of setting the base class is by setting it in the constructor:

$factory = new GenericFactory([
    'base_class' => '\My\Foo\BaseClassInterface',
]);

👉 Note that Interfaces can also be used as a factory's base class.

Setting a default type of object

It is possible to set a default type of object (default class) by setting the default_class property.

The recommended way of setting the default class is by setting it in the constructor:

$factory = new GenericFactory([
    'default_class' => '\My\Foo\DefaultClassInterface',
]);

⚠️ Setting a default class name changes the standard Factory behavior. When an invalid class name is used, instead of throwing an Exception, an object of the default class type will always be returned.

Constructor arguments

It is possible to set "automatic" constructor arguments that will be passed to every created object.

The recommended way of setting constructor arguments is by passing an array of arguments to the constructor:

$factory = new GenericFactory([
    'arguments' => [
        [
            'logger' => $container['logger'],
        ],
        $secondArgument,
    ],
]);

Executing an object callback

It is possible to execute an object callback upon object instanciation. A callback is any Callable that takes the newly created object by reference as its function parameter.

// $obj is the newly created object
function callback($obj): void;

The recommended way of adding an object callback is by passing a Callable to the constructor:

$factory = new GenericFactory([
    'arguments' => [[
        'logger' => $container['logger']
    ]],
    'callback' => function ($obj) {
        $obj->foo('bar');
        $obj->logger->debug('Objet instanciated from factory.');
    }
]);

Resources

charcoal/factory 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-08