1. Go to this page and download the library: Download kaspi/di-container 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/ */
kaspi / di-container example snippets
// src/Services/Envelope.php
namespace App\Services;
// Класс для создания сообщения
class Envelope {
public function subject(string $subject): static {
// ...
return $this;
}
public function message(string $message): static {
// ...
return $this;
}
}
// src/Services/Mail.php
namespace App\Services;
// Сервис отправки почты
class Mail {
public function __construct(private Envelope $envelope) {}
public function envelop(): Envelope {
return $this->envelope;
}
public function send(): bool {
// отправка сообщения
}
}
// src/Models/Post.php
namespace App\Models;
// Модель данных — пост в блоге.
class Post {
public string $title;
// ...
}
// src/Controllers/PostController.php
namespace App\Controllers;
use App\Services\Mail;
use App\Models\Post;
// Контроллер для обработки действия.
class PostController {
public function __construct(private Mail $mail) {}
public function send(Post $post): bool {
$this->mail->envelop()
->subject('Publication success')
->message('Post <'.$post->title.'> was published.');
return $this->mail->send();
}
}
use App\Controllers\PostController;
use App\Models\Post;
use Kaspi\DiContainer\DiContainerBuilder;
// Создать контейнер.
$container = (new DiContainerBuilder())
->build();
// more code...
// получить класс PostController с внедренным сервисом Mail.
$postController = $container->get(PostController::class);
//Заполняем модель данными.
$post = new Post();
$post->title = 'Publication about DiContainer';
// Выполняем метод `PostController::post()`.
$postController->send($post);
$post = new App\Controllers\PostController(
new App\Services\Mail(
new App\Services\Envelope()
)
);
use App\Controllers\PostController;
use App\Models\Post;
$post = new Post();
$post->title = 'Publication about DiContainer';
// ...
// получить класс PostController с внедренным сервисом Mail и выполнить метод "send"
// с передачей именованного аргумента
$container->call(
definition: [PostController::class, 'send'],
post: $post
);
use Kaspi\DiContainer\{DiContainerConfig, DiContainerBuilder};
$diConfig = new DiContainerConfig(
useZeroConfigurationDefinition: false,
useAttribute: false,
isSingletonServiceDefault: true,
);
// передать настройки в построитель контейнера
$container = (new DiContainerBuilder(containerConfig: $diConfig))
->build();
use Kaspi\DiContainer\DiContainerBuilder;
use Psr\Container\ContainerInterface;
function testFunc(ContainerInterface $c) {
return $c;
}
$container = (new DiContainerBuilder())->build();
var_dump($container->call('testFunc') instanceof DiContainer); // true
var_dump($container->call('testFunc') instanceof ContainerInterface); // true
use Kaspi\DiContainer\DiContainerBuilder;
use Psr\Container\ContainerInterface;
class TestClass {
public function __construct(
public ContainerInterface $container
) {}
}
$container = (new DiContainerBuilder())->build();
var_dump($container->get(TestClass::class)->container instanceof ContainerInterface); // true
shell
docker-compose run --rm php composer install
shell
docker-compose run --rm php vendor/bin/phpunit --no-coverage
shell
docker-compose run --rm php vendor/bin/phpunit
shell
docker-compose run --rm php vendor/bin/phpstan
shell
make test-supports-php
shell
docker-compose run --rm php sh
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.