lane4hub/factory

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

1.0.0 2025-01-31 17:16 UTC

This package is auto-updated.

Last update: 2025-06-29 02:09:48 UTC


README

Build Status

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.
  • Dependency Injection via a Psr\Container\ContainerInterface.
  • Dynamic construction of classes with parameter support.

Key Methods

  • get($className, $version = null, ...$parameters): Creates an instance of the specified class, optionally with versioning and parameters.
  • classVersion(): Returns the currently configured versioning instance.
  • setClassVersion($versionService): Sets a new versioning instance.
  • container(): Returns the current container.
  • setContainer($container): Sets the container.

Notes

  • If a container is provided, the instance is retrieved from the container if available.
  • If no container is available or the class is not registered in the container, the instance is dynamically created using reflection.
  • Setting a container or ClassVersion is only possible once.

Usage

$factory = new Factory($container, $classVersion);
$instance = $factory->get(MyClass::class, '1.0', ['param1', 'param2']);

The class provides a central point for instantiation and management of objects with optional versioning and dependency injection.

Example code without classVersioning and without DI container

use Jardis\Factory\Factory;

$factory = new Factory();
$myClassInstance = $factory->get(MyClass::class);
$myClassInstance = $factory->get(MyClassWithTwoParameters::class, null, $var1, $var2);

Example code with classVersioning and without DI container

use Jardis\Factory\Factory;
use Jardis\Version\config\ClassVersionConfig;

$classVersions = ['subDirectory' => ['version1', 'version2']];
$classVersionConfig = new ClassVersionConfig($classVersions);
$classVersion = new ClassVersion($classVersionConfig);

$factory = new Factory(null, $classVersion);
$myClassInstance = $factory->get(MyClass::class);
$myClassInstance = $factory->get(MyClassWithTwoParameters::class, null, $var1, $var2);

Example code with classVersioning and DI container

use Jardis\Factory\Factory;
use Jardis\Version\config\ClassVersionConfig;

$container = new Container();
$classVersions = ['subDirectory' => ['version1', 'version2']];
$classVersionConfig = new ClassVersionConfig($classVersions);
$classVersion = new ClassVersion($classVersionConfig);

$factory = new Factory($container, $classVersion);
$myClassInstance = $factory->get(MyClass::class);
$myClassInstance = $factory->get(MyClassWithTwoParameters::class, null, $var1, $var2);

Quickstart Composer

composer require lane4hub/factory
make install

Quickstart GitHub

git clone https://github.com/lane4hub/factory.git
cd factory

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

The DockerFile for creating the PHP image is built more extensively than necessary for this tool since the resulting PHP image is used in various Lane4 tools.

Docker Image Version

We also ensure that our images are as small as possible and leave no unnecessary files on your system during repeated image builds.

Jardis Framework

This tool is part of the development of our Domain-Driven Design framework Jardis (Just a reliable domain integration system).

Jardis consists of a collection of highly professional PHP software packages and concepts based on DDD, explicitly developed for the efficient and sustainable solution of complex business applications.

Our development is based on defined standards such as DDD and PSR, aiming to deliver the highest possible quality for functional and non-functional requirements.

For technical quality assurance, we use PhpStan Level 8, PhpCS, and generate full test coverage with PhpUnit.

Our software packages meet the following quality attributes:

  • Analyzability
  • Adaptability
  • Extensibility
  • Modularity
  • Maintainability
  • Testability
  • Scalability
  • High performance