1. Go to this page and download the library: Download elie29/zend-phpdi-config 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/ */
elie29 / zend-phpdi-config example snippets
use Elie\PHPDI\Config\Config;
use Elie\PHPDI\Config\ContainerFactory;
$factory = new ContainerFactory();
$container = $factory(
new Config([
'dependencies' => [
'services' => [],
'invokables' => [],
'autowires' => [], // A new key added to support PHP-DI autowire technique
'factories' => [],
'aliases' => [],
'delegators' => [],
],
// ... other configuration
// Enable compilation
Config::DI_CACHE_PATH => __DIR__, // Folder path
// Write proxies to file : cf. https://php-di.org/doc/lazy-injection.html
Config::DI_PROXY_PATH => __DIR__, // Folder path
// Disable autowire (enabled by default)
Config::USE_AUTOWIRE => false
// Enable cache
Config::ENABLE_CACHE_DEFINITION => false, // boolean, true if APCu is activated
])
);
> $config = $container->get('config');
>
declare(strict_types = 1);
use Elie\PHPDI\Config\Config;
use Elie\PHPDI\Config\ContainerFactory;
// Protect variables from global scope
return call_user_func(function () {
$config =
class ConfigProvider
{
/**
* Returns the configuration array
*/
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies()
];
}
/**
* Returns the container dependencies
*/
public function getDependencies(): array
{
return [
'autowires' => [
UserManager::class
]
];
}
}
class UserManager
{
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function register($email, $password)
{
$this->mailer->mail($email, 'Hello and welcome!');
}
}
class Mailer
{
public function mail($recipient, $content)
{
}
}
console
> ./vendor/bin/add-autowires-entry config.php "Laminas\\Stdlib\\ConsoleHelper"
> [DONE] Changes written to config.php
>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.