codeinc/services-manager

此包已被废弃,不再维护。未建议替代包。

使用PHP 7编写的服务管理器

2.0.0-beta.1 2018-06-06 11:27 UTC

This package is not auto-updated.

Last update: 2022-02-01 13:12:37 UTC


README

该库旨在用作服务管理器。它使用PHP 7.1编写。

使用方法

<?php
use CodeInc\ServicesManager\ServicesManager;
use CodeInc\ServicesManager\ServiceInterface;

// a first service
class MyFirstService implements ServiceInterface 
{
	public function hello(string $name):void
	{
		echo sprintf("Hello %s!", $name);
	}
}

// a second service using the first service
class MySecondService implements ServiceInterface
 {
	/** @var MyFirstService */
	private $myFirstClass;
	
	public function __construct(MyFirstService $myFirstClass) 
	{
		$this->myFirstClass = $myFirstClass;
	}
	
	public function helloWorld():void
	{
		$this->myFirstClass->hello("World");
	}
}

// calling the second service, the service manager is going to first instantiated MyFirstService
// then instantiate MySecondService with MyFirstService as a parameter.
$serviceManager = new ServicesManager();
$mySecondService = $serviceManager->getService(MySecondService::class);
$mySecondService->helloWorld();

// you also can add external objects to makes them available to the servides,
// for instance a PSR-7 ServerRequest object or Doctrine's EntityManager.
$serviceManager->addService($entityManager);

// the service manager will pass the instance of the service manager added
// using addService()
class MyThirdService {
    public function __construct(EntityManager $entityManager) { }
}

安装

此库可通过Packagist获得,并可以使用Composer进行安装

composer require codeinc/services-manager

许可证

该库在MIT许可证下发布(见LICENSE文件)。