Skip to content

This package provides a factory interfaces for a domain driven design approach

License

Notifications You must be signed in to change notification settings

jardisPort/factory

Repository files navigation

JardisPort Factory

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

This package provides factory interfaces for a domain-driven design (DDD) approach.

Installation

Install the package via Composer:

composer require jardisport/factory

Requirements

  • PHP >= 8.2

Usage

The package provides a FactoryInterface that defines a standard method for creating objects with support for versioning, dynamic parameters, and shared (singleton) instances.

Creating Objects

use JardisPort\Factory\FactoryInterface;

// Get a new instance
$service = $factory->get(MyService::class);

// With a specific version
$service = $factory->get(MyService::class, 'v2');

// With constructor parameters
$service = $factory->get(MyService::class, null, $param1, $param2);

Parameters

  • $className: The fully qualified class name to instantiate
  • $classVersion: Optional version string for versioned class creation
  • ...$parameters: Variadic parameters passed to the class constructor

Shared Instances

Register classes as shared to reuse the same instance per (className, version) combination:

// Register a single class
$factory->registerShared(MyService::class);

// Register multiple classes
$factory->registerShared([MyService::class, AnotherService::class]);

Shared instances are instantiated once and reused on subsequent get() calls. Only use for stateless services — stateful objects (entities, DTOs, handlers with mutable state) must not be registered.

Development

Code Quality

The project uses PHPStan for static analysis and PHP_CodeSniffer for code style checks:

# Run PHPStan
vendor/bin/phpstan analyse

# Run PHP_CodeSniffer
vendor/bin/phpcs

Pre-commit Hook

A pre-commit hook is automatically installed via Composer's post-install script to ensure code quality before commits.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Authors

Keywords

  • factory
  • interfaces
  • JardisPort
  • Headgent
  • DDD (Domain-Driven Design)

About

This package provides a factory interfaces for a domain driven design approach

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors