Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
geekish committed May 8, 2017
1 parent 32dc702 commit b1dac33
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions README.md
Expand Up @@ -11,19 +11,61 @@ Bringing [mindplay/unbox][link-unbox] into [Slim Framework][link-slim].

## Install

Via Composer
Via [Composer][link-composer]:

``` bash
$ composer require geekish/slimbox
composer require geekish/slimbox
```

## Usage

This package contains an extended (final) Container and ContainerFactory from [Unbox][link-unbox]. Usage is almost exactly the same as using Unbox directly, except the extended Container _partially_ implements ArrayAccess. What this means is that you may use array notation to _access_ services in the Container; however, due to the fact that Unbox uses a factory class to create the container, you cannot use array notation to set/configure services on the Container.
### Service Provider

Also provided is `DefaultServicesProvider`, registering all the same services required by Slim. All services are registered under their class name, with aliases by interface and Slim-specific short name (e.g. "router", "foundHandler"). The default services provider is _not_ automatically registered for you, so you need to do this yourself.
The most important class in this package is `Geekish\Slimbox\DefaultServicesProvider`.
It ensures the same services [required by Slim](https://www.slimframework.com/docs/concepts/di.html#required-services) are available through Unbox.
Besides the change in container, it differs from `Slim\DefaultServicesProvider` by registering services under their FQCN first, then registers aliases by their interfaces, and finally the short aliases used by slim (e.g. "router", "foundHandler"). Registering services by their class name enables Unbox to automatically inject them as dependencies as needed by other classes.

The service provider accepts an array of settings that Slim uses to configure various services (see: [Slim Default Settings](//www.slimframework.com/docs/objects/application.html#slim-default-settings)). These are injected into a provided Settings class, so you may type hint against it in your classes for injection.
The service provider is _not_ automatically registered for you, so you need to do this yourself:

```php
use Geekish\Slimbox\DefaultServicesProvider;
use mindplay\unbox\ContainerFactory;

$factory = new ContainerFactory;
$factory->add(new DefaultServicesProvider);

$container = $factory->createContainer();
```

### Settings

Also included is `Geekish\Slimbox\Settings`, which extends from `Slim\Collection`. This class replaces the simple array that Slim registers under "settings" (see: [Slim Default Settings][link-slim-default-settings]).

Settings may be provided via the constructor of the `DefaultServicesProvider`:

```php
$factory->add(new DefaultServicesProvider([
"outputBuffering" => "prepend",
]));
```

Or via the `configure()` method on `ContainerFactory`:

```php
$factory->configure(
Settings::class,
function (Settings $settings) {
$settings['displayErrorDetails'] = true;
return $settings;
}
);
```

### Container and Container Factory (Optional)

This package contains an extended (final) Container and ContainerFactory from [Unbox][link-unbox]. Usage is almost exactly the same as using Unbox directly, except the extended Container _partially_ implements [ArrayAccess](http://php.net/manual/en/class.arrayaccess.php). This allows you to use the Container like an array to _access_ services; however, due to the fact that Unbox uses a factory class to create the container, you cannot use array notation to set/configure services on the Container.

To use the extended ContainerFactory:

``` php
use Geekish\Slimbox\ContainerFactory;
Expand All @@ -36,15 +78,9 @@ $factory->add(new DefaultServicesProvider([
]));

$container = $factory->createContainer();

$app = new App($container);

// register your routes on App

$app->run();
```

Usage of the packaged `Container` and `ContainerFactory` is entirely optional; they are included purely for convenience and consistency with Slim's packaged `Container`. Simply swap out `Geekish\Slimbox\ContainerFactory` in the snippet above for `mindplay\unbox\ContainerFactory`.
Usage of the packaged `Container` and `ContainerFactory` is entirely optional; they are included purely for convenience and consistency with Slim's packaged `Container`. Simply replace `Geekish\Slimbox\ContainerFactory` in the snippet above with `mindplay\unbox\ContainerFactory`.

## Change log

Expand Down Expand Up @@ -81,6 +117,9 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[ico-downloads]: https://img.shields.io/packagist/dt/geekish/slimbox.svg?style=flat-square

[link-slim]: //github.com/slimphp/Slim
[link-slim-required-services]: //www.slimframework.com/docs/concepts/di.html#required-services
[link-slim-default-settings]: //www.slimframework.com/docs/objects/application.html#slim-default-settings
[link-composer]: //getcomposer.org
[link-unbox]: //github.com/mindplay-dk/unbox
[link-packagist]: //packagist.org/packages/geekish/slimbox
[link-travis]: //travis-ci.org/geekish/slimbox
Expand Down

0 comments on commit b1dac33

Please sign in to comment.