定制 jardiscore/factory 二次开发

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

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

jardiscore/factory

最新稳定版本:1.0.0

Composer 安装命令:

composer require jardiscore/factory

包简介

The Factory class serves as a flexible instantiation and access factory for classes, supporting optional versioning and dependency injection (DI).

README 文档

README

Build Status License: MIT PHP Version PHPStan Level PSR-4 PSR-11 PSR-12 Coverage

Purpose

The Factory serves as a flexible instantiation and access factory for classes, supporting optional versioning and dependency injection (DI).

Description for Developers

The class provides methods to dynamically create instances of classes or retrieve them from a provided container. It supports:

  • Class versioning through a ClassVersionInterface (optional).
  • Dependency Injection via a Psr\Container\ContainerInterface (optional).
  • Dynamic construction of classes with parameter support using reflection.

Key Features

  • PSR-11 Container Integration: Automatically uses the container to resolve dependencies if available.
  • Optional Class Versioning: Supports versioned class resolution through ClassVersionInterface.
  • Flexible Parameter Passing: Supports both variadic and array-based parameter passing.
  • Fallback to Reflection: If no container is provided, or the class is not in the container, instances are created via reflection.

Notes

  • Both ContainerInterface and ClassVersionInterface are optional dependencies.
  • If a container is provided and has the requested class, it will be retrieved from the container (ignoring parameters).
  • If no container is available or the class is not registered, the instance is created dynamically using reflection with provided parameters.
  • The ClassVersionInterface can return a class name (string), an object instance, or null.

Basic Usage

use JardisCore\Factory\Factory;

// Simple usage without dependencies
$factory = new Factory();
$instance = $factory->get(MyClass::class);

// With parameters (variadic)
$instance = $factory->get(MyClass::class, null, $param1, $param2);

// With parameters (array)
$instance = $factory->get(MyClass::class, null, [$param1, $param2]);

Example code without ClassVersion and without DI container

use JardisCore\Factory\Factory;

$factory = new Factory();

// Simple instantiation
$myClassInstance = $factory->get(MyClass::class);

// With constructor parameters
$myClassInstance = $factory->get(MyClassWithTwoParameters::class, null, $var1, $var2);

Example code with ClassVersion and without DI container

use JardisCore\Factory\Factory;
use JardisCore\Contract\ClassVersion\ClassVersionInterface;

// Your ClassVersionInterface implementation
$classVersion = new YourClassVersionImplementation();

$factory = new Factory(null, $classVersion);

// ClassVersion may return a different class based on versioning logic
$myClassInstance = $factory->get(MyClass::class);

// With specific version parameter
$myClassInstance = $factory->get(MyClass::class, 'v2.0', $var1, $var2);

Example code with ClassVersion and DI container

use JardisCore\Factory\Factory;
use JardisCore\Contract\ClassVersion\ClassVersionInterface;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = new YourContainer();

/** @var ClassVersionInterface $classVersion */
$classVersion = new YourClassVersionImplementation();

$factory = new Factory($container, $classVersion);

// ClassVersion determines the actual class, container provides the instance
$myClassInstance = $factory->get(MyClass::class);

// With version parameter
$myClassInstance = $factory->get(MyClass::class, 'v2.0');

Installation

Composer

composer require jardiscore/factory

GitHub

git clone https://github.com/jardiscore/factory.git
cd factory
make install

Contents in the GitHub Repository

  • Source Files:
    • src
    • tests
  • Support:
    • Docker Compose
    • .env
    • pre-commit-hook.sh
    • Makefile Simply run make in the console
  • Documentation:
    • README.md

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-27