codeinc/services-manager

This package is abandoned and no longer maintained. No replacement package was suggested.

Services manager written in 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

The library is intended to be used as a services manager. It is written in PHP 7.1.

Usage

<?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) { }
}

Installation

This library is available through Packagist and can be installed using Composer:

composer require codeinc/services-manager

License

This library is published under the MIT license (see the LICENSE file).