定制 mtymek/blast-reflection-factory 二次开发

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

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

mtymek/blast-reflection-factory

Composer 安装命令:

composer require mtymek/blast-reflection-factory

包简介

Universal auto-wiring factory for Laminas ServiceManager.

README 文档

README

Build Status

Universal auto-wiring factory for Laminas ServiceManager.

Introduction

Writing factories for Laminas ServiceManager can be boring, repeatable task. Typical service will consume one or more dependencies using constructor injection:

class Mailer
{
    public function __construct(MailTransportInterface $transport, MailRenderer $renderer)
    {
      // ...
    }
}

This is how factory is going to look like:

class MailerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        return new Mailer(
            $container->get(MailTransportInterface::class),
            $container->get(MailRenderer::class)
        );
    }
}

In typical application, you will end up with multiple factories that simply pull some services and create new object. ReflectionFactory can take care of this use case for you - it uses Reflection to scan parameter types in constructor and instantiates new object based on this information.

Installation

Install this package using Composer:

$ composer require mtymek/blast-reflection-factory

Usage

After installing this package, all you have to do is to tell ServiceManager to use ReflectionFactory to create your services.

For Laminas Mezzio application, configuration can look like this:

use Blast\ReflectionFactory\ReflectionFactory;

return [
    'dependencies' => [
        'factories' => [
            // use normal factory for classes that require complex instantiation 
            SmtpMailTransport::class => SmtpMailTransportFactory::class,
             
            // use ReflectionFactory for auto-wiring
            MailRenderer::class => ReflectionFactory::class,
            Mailer::class => ReflectionFactory::class,
        ],
        'aliases' => [
            MailTransportInterface::class => SmtpMailTransport::class,
        ],
    ]
];

Caching

Auto-wiring is expensive operation, so ReflectionFactory allows to store the result on disk to be reused later:

\Blast\ReflectionFactory\ReflectionFactory::enableCache('data/cache/reflection-factory.cache.php');

If you are using Zend Expressive Skeleton Application, then config/container.php would be a good place to enable this cache.

Warming-up cache

Cache file is automatically updated when a service is pulled from the container for the first time. This can lead to race conditions when your application is under heavy load. In order to avoid it, cache should be warmed up during deployment phase. The easiest way to do it is to go through all configured factories, pulling every service from the container.

Example script for applications based on Mezzio Skeleton:

<?php
// warmup-reflection-factory-cache.php

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

/** @var \Interop\Container\ContainerInterface $container */
$container = require 'config/container.php';

$config = require 'config/config.php';
foreach ($config['dependencies']['factories'] as $type => $factory) {
    $container->get($type);
}

Limitations

ReflectionFactory is only meant to be used in typical scenario, when all dependencies are injected using constructor. All of them must be type-hinted - otherwise ReflectionFactory won't be able to resolve them. Despite this limitation, this library should still let you reduce number of factories you have to write.

What is not supported (and won't be):

  • scalar value injection
  • setter injection

mtymek/blast-reflection-factory 适用场景与选型建议

mtymek/blast-reflection-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67.42k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2016 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 67.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2016-06-26