A dependency-injection container agnostic service provider
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
tests
.gitattributes
.gitignore
.travis.yml
LICENSE
Makefile
README.md
composer.json
helpers.php
phpunit.xml

README.md

Service

Release Build Status Code Quality Code Coverage Packagist

ICanBoogie/Service provides means to reference, resolve, and invoke services using your favorite dependency injection container, in the most transparent way possible.

Please, consider the following example:

<?php

use ICanBoogie\Service\ServiceProvider;
use function ICanBoogie\Service\ref;

ServiceProvider::define(function ($id) {

	if ($id === 'hello')
	{
		return function ($name = "world") {
			return "Hello $name!";
		};
	}

	throw new \LogicException("Unknown service: $id");
});

# getting a service through the provider
$service = ServiceProvider::provide('hello');
echo $service("Madonna");
// Hello Madonna!

# using a reference
$reference = ref('hello');
echo $reference;
// hello
echo $reference("Madonna");
// Hello Madonna! 

References are callables

Service references created with ref are especially useful when you need to provide a callable but you don't want that callable to be instantiated right away:

<?php

use function ICanBoogie\Service\ref;

class Compute
{
	public function __construct(callable $computer)
	{
		//
	}

	//
}

$compute = new Compute(ref('expansive_instance'));

References can be exported

ServiceReference instances can safely be exported with var_export():

<?php

use ICanBoogie\Service\ServiceReference;

$id = 'my_service';
$reference = new ServiceReference($id);
$dump = var_export($reference, true);

$r = eval("return $dump;");

echo get_class($r);   // ICanBoogie\Service\ServiceReference 
echo (string) $r;     // my_service

Requirements

The package requires PHP 5.6 or later.

Installation

The recommended way to install this package is through Composer:

$ composer require ICanBoogie/Service

Cloning the repository

The package is available on GitHub, its repository can be cloned with the following command line:

$ git clone https://github.com/ICanBoogie/Service.git

Documentation

The package is documented as part of the [ICanBoogie][] framework documentation. You can generate the documentation for the package and its dependencies with the make doc command. The documentation is generated in the build/docs directory. ApiGen is required. The directory can later be cleaned with the make clean command.

Testing

The test suite is ran with the make test command. PHPUnit and Composer need to be globally available to run the suite. The command installs dependencies as required. The make test-coverage command runs test suite and also creates an HTML coverage report in build/coverage. The directory can later be cleaned with the make clean command.

The package is continuously tested by Travis CI.

Build Status Code Coverage

License

ICanBoogie/Service is licensed under the New BSD License - See the LICENSE file for details.