在开发一个大型php项目时,我遇到了一个棘手的问题:如何有效地管理服务依赖和生命周期。随着项目的扩展,服务之间的依赖关系变得越来越复杂,手动管理这些依赖不仅耗时,而且容易出错。我尝试了几种方法,但都没有找到一个既灵活又高效的解决方案。最终,我通过使用ghostwriter/container库成功解决了这个问题。
使用Composer安装ghostwriter/container非常简单:
composer require ghostwriter/container
这个库提供了一个可扩展的依赖注入服务容器,支持自动化的对象组合、拦截和生命周期管理。它通过一系列强大的功能帮助你简化服务管理:
简单使用
你可以轻松地在容器中注册服务:
final readonly class Service
{
public function __construct(
private Dependency $dependency
) {}
public function dependency(): Dependency
{
return $this->dependency;
}
}
$container = Container::getInstance();
$service = $container->get(Service::class);
assert($service instanceof Service); // true
assert($service->dependency() instanceof Dependency); // true使用属性注解
你还可以使用属性注解来注册服务,这使得代码更加清晰和易于维护:
立即学习“PHP免费学习笔记(深入)”;
#[Inject]
use Ghostwriter\Container\Attribute\Inject;
final readonly class Service
{
public function __invoke(
#[Inject(Dependency::class)]
DependencyInterface $dependency
): Dependency
{
return $this->dependency;
}
}#[Factory]
use Ghostwriter\Container\Attribute\Factory;
#[Factory(ServiceFactory::class)]
final readonly class Service
{
public function __construct(
private Dependency $dependency
) {}
public function dependency(): Dependency
{
return $this->dependency;
}
}#[Extension]
use Ghostwriter\Container\Attribute\Extension;
#[Extension(ServiceExtension::class)]
final readonly class Service
{
public function __construct(
private Dependency $dependency
) {}
public function dependency(): Dependency
{
return $this->dependency;
}
}#[Provider]
use Ghostwriter\Container\Attribute\Provider;
#[Provider(ServiceProvider::class)]
final readonly class Service
{
public function __construct(
private DependencyInterface $dependency
) {}
public function dependency(): DependencyInterface
{
return $this->dependency;
}
}服务提供者
通过服务提供者,你可以更灵活地管理服务:
interface TaskInterface {}
final readonly class Task implements TaskInterface {}
final class Tasks
{
private array $tasks = [];
public function addTask(TaskInterface $task)
{
$this->tasks[] = $task;
}
}
final readonly class TasksServiceProvider implements ServiceProviderInterface
{
public function __invoke(ContainerInterface $container)
{
$container->alias(Task::class, TaskInterface::class);
// "set" the service instance
$container->set(FirstTask::class, new FirstTask(), [Task::class]);
// "define" the service builder
$container->define(Tasks::class, static function (Container $container) {
/** @var Tasks $tasks */
$tasks = $container->build(Tasks::class);
foreach ($container->tagged(Task::class) as $service) {
$tasks->addTask($service);
}
return $tasks;
}, [Tasks::class, 'tasks']);
}
}
$container->provide(TasksServiceProvider::class);
$service = $container->get(TaskInterface::class);
assert($service instanceof Task); // true上下文绑定
上下文绑定允许你根据不同的上下文注入不同的依赖:
interface ClientInterface {}
final readonly class RestClient implements ClientInterface {}
final readonly class GraphQLClient implements ClientInterface {}
final readonly class GitHub
{
public function __construct(
private ClientInterface $client
) {
}
public function getClient(): ClientInterface
{
return $this->client;
}
}
// When GitHub::class asks for ClientInterface::class, it would receive an instance of GraphQLClient::class.
$container->bind(GitHub::class, ClientInterface::class, GraphQLClient::class);
// When any other service asks for ClientInterface::class, it would receive an instance of RestClient::class.
$container->alias(ClientInterface::class, RestClient::class);服务扩展
你可以使用服务扩展来增强现有服务的功能:
/** * @implements ExtensionInterface*/ final readonly class GitHubExtension implements ExtensionInterface { /** * @param GitHubClient $service * @return GitHubClient */ public function __invoke(ContainerInterface $container, object $service): object { $service->setEnterpriseUrl( $container->get(GitHubClient::GITHUB_HOST) ); return $service; } } $container->alias(GitHubClientInterface::class, GitHubClient::class); $container->extend(GitHubClientInterface::class, $container->get(GitHubExtension::class));
服务工厂
服务工厂允许你动态创建服务实例:
final readonly class Dependency {}
final readonly class Service
{
public function __construct(
private Dependency $dependency
){}
public function dependency(): Dependency
{
return $this->dependency;
}
}
final readonly class ServiceFactory {
public function __invoke(Container $container): Service
{
return new Service($container->get(Dependency::class));
}
}
$container = Container::getInstance();
$container->factory(Service::class, ServiceFactory::class);
$service = $container->get(Service::class);
assert($service instanceof Service); // true
assert($service->dependency() instanceof Dependency); // true使用ghostwriter/container库,我能够更加灵活和高效地管理服务依赖和生命周期。它不仅简化了代码,还提高了项目的可维护性和可扩展性。如果你在处理PHP项目中的依赖注入和服务管理问题,强烈推荐你尝试这个库。











