PHP code example of migears / wiring

1. Go to this page and download the library: Download migears/wiring library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

migears / wiring example snippets




use Psr\Container\ContainerInterface;

return [
    // factories may ignore the container …
    'app.name' => static fn (): string => 'demo',

    // … or reach sibling entries through it
    PDO::class => static fn (): PDO => new PDO('sqlite:' . __DIR__ . '/../var/app.db'),
    LoggerInterface::class => static fn (): LoggerInterface => new NullLogger(),
    UserDao::class => static fn (ContainerInterface $c): UserDao
        => new UserDao($c->get(PDO::class), $c->get(LoggerInterface::class)),
];

use MiGears\Wiring\Wiring;

Wiring::load(__DIR__ . '/config/wiring-list.php', $container);

use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RuntimeException;

final class Container implements ContainerInterface
{
    /** @var array<string, callable> */ private array $factories = [];
    /** @var array<string, mixed> */    private array $instances = [];

    public function set(string $id, callable $factory): static
    {
        $this->factories[$id] = $factory;
        unset($this->instances[$id]);
        return $this;
    }

    public function has(string $id): bool
    {
        return isset($this->factories[$id]) || array_key_exists($id, $this->instances);
    }

    public function get(string $id): mixed
    {
        if (array_key_exists($id, $this->instances)) {
            return $this->instances[$id];
        }
        if (!isset($this->factories[$id])) {
            throw new class extends RuntimeException implements NotFoundExceptionInterface {};
        }
        return $this->instances[$id] = ($this->factories[$id])();
    }
}

public static function load(string $file, ContainerInterface $container): void



use Psr\Container\ContainerInterface;

return [
    // 工厂可以忽略容器 …
    'app.name' => static fn (): string => 'demo',

    // … 也可以通过它取到兄弟条目
    PDO::class => static fn (): PDO => new PDO('sqlite:' . __DIR__ . '/../var/app.db'),
    LoggerInterface::class => static fn (): LoggerInterface => new NullLogger(),
    UserDao::class => static fn (ContainerInterface $c): UserDao
        => new UserDao($c->get(PDO::class), $c->get(LoggerInterface::class)),
];

use MiGears\Wiring\Wiring;

Wiring::load(__DIR__ . '/config/wiring-list.php', $container);

use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RuntimeException;

final class Container implements ContainerInterface
{
    /** @var array<string, callable> */ private array $factories = [];
    /** @var array<string, mixed> */    private array $instances = [];

    public function set(string $id, callable $factory): static
    {
        $this->factories[$id] = $factory;
        unset($this->instances[$id]);
        return $this;
    }

    public function has(string $id): bool
    {
        return isset($this->factories[$id]) || array_key_exists($id, $this->instances);
    }

    public function get(string $id): mixed
    {
        if (array_key_exists($id, $this->instances)) {
            return $this->instances[$id];
        }
        if (!isset($this->factories[$id])) {
            throw new class extends RuntimeException implements NotFoundExceptionInterface {};
        }
        return $this->instances[$id] = ($this->factories[$id])();
    }
}

public static function load(string $file, ContainerInterface $container): void