Search by

webdevcave / yadic

carloscarucce

Yet another dependency injection container for PHP

Package info

github.com/WebdevCave/yadic-php

pkg:composer/webdevcave/yadic

Statistics

Installs: 39

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v2.0 2026-11-16 14:20 UTC

This package is auto-updated.

Last update: 2026-11-16 14:23:15 UTC


README

StyleCi Latest Stable Version Latest Unstable Version Total Downloads License codecov

This is a simple to use, yet powerful service container that provides a seamless way to automate dependency injection featuring auto-wiring and object hydration.

Requires PHP 8.3 or newer.

composer require webdevcave/yadic

Alternatively, you can clone the repository or download the source files directly and include them in your project.

Usage

Autowiring

<?php

require_once __DIR__.'/vendor/autoload.php';

use Webdevcave\Yadic\Annotations\Provides;
use Webdevcave\Yadic\Annotations\Singleton;
use Webdevcave\Yadic\ServiceContainer;

interface StorageInterface
{
    public function store(mixed $data): bool;
}

#[Provides('storage')]
#[Provides(StorageInterface::class)]
#[Singleton]
class Storage implements StorageInterface
{
    public function store(mixed $data): bool
    {
        //store data...

        return true;
    }
}

class MyController
{
    public function __construct(
        private StorageInterface $storage
    ) {
    }

    public function save(): bool
    {
        return $this->storage->store('my data...');
    }
}

$container = new ServiceContainer();

//No need to do this in a real world application:
$container->addAlias(StorageInterface::class, Storage::class);

//Use this instead:
//$container->loadDefinitionsFromDirectory($directory, $namespace); //Loads annotations from classes declared in a PSR4 directory
//var_dump($container->get('storage')->store($data));

var_dump($container->get(MyController::class)->save()); //bool(true)

Invoking a method ft. autowiring

$arguments = ['nonInjectableArgument' => 'value']; //optional
$container->invoke([$instance, 'methodName'], $arguments);

Hydration

//Class declarations:

use Webdevcave\Yadic\Annotations\ArrayOf;

class Candidate
{
    public function __construct(
        public ?string $name = null,
        public ?int $age = null,
        #[ArrayOf(Skill::class)]
        public array $skills = []
    ) {
    }
}

class Skill
{
    public function __construct(
        public string $title,
    ) {
    }
}

// Hydration example 1:
$data = [
    'name'   => 'John Doe',
    'age'    => 25,
    'skills' => [
        ['title' => 'PHP'],
        ['title' => 'Java'],
        ['title' => 'Rust'],
        ['title' => 'React'],
    ],
];
$instance = $container->hydrate(Candidate::class, $data);

//Results output
/*
print_r($instance);
This test printed output: 
Candidate Object
(
    [name] => John Doe
    [age] => 25
    [skills] => Array
        (
            [0] => Skill Object
                (
                    [title] => PHP
                )

            [1] => Skill Object
                (
                    [title] => Java
                )

            [2] => Skill Object
                (
                    [title] => Rust
                )

            [3] => Skill Object
                (
                    [title] => React
                )

        )

)
 */

// Hydration example 2:
$data = [
    [
        'name' => 'Foo',
        //...
    ],
    [
        'name' => 'Bar',
        //...
    ]
];
$instances = $container->hydrate(Candidate::class, $data);
//Results output
/*
print_r($instances);
This test printed output: 
Array
(
    [0] => Candidate Object
        (
            [name] => Foo
            //...
        )

    [1] => Candidate Object
        (
            [name] => Bar
            //...
        )
)
 */

Union types and enums

Constructor parameters declared with union types (including DNF types) are supported:

  • null is kept when the parameter is nullable;
  • objects that are already instances are passed through untouched;
  • scalars are kept as is, except for backed enums, which are built with from()/tryFrom();
  • arrays are hydrated into the class whose constructor best matches the given keys. If that fails, the next candidate is tried. Plain lists, or arrays no class can be built from, are kept as is when the parameter also accepts array.
class Profile
{
    public function __construct(
        public Address|GeoPoint|string|null $location = null,
        public Status|null $status = null, // backed enum
    ) {
    }
}

$profile = $container->hydrate(Profile::class, [
    'location' => ['lat' => -23.5, 'lng' => -46.6], // GeoPoint
    'status'   => 'active',                         // Status::Active
]);

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or a pull request on GitHub.

License

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