flexsounds/slim-container-builder

A light weight container builder from a configuration file

dev-master 2017-03-07 19:28 UTC

This package is auto-updated.

Last update: 2024-12-10 06:42:41 UTC


README

Build Status Packagist license Coverage Status

Slim-Container-Builder

A tool to build your Slim Framework 3 container. Instead of having to define your container in php, you just define it from a configuration file.

This will not replace the pimple container, but will build one for you.

Installation

User composer to install

composer require flexsounds/slim-container-builder

Install symfony/yaml to use yaml files!

Default usage

This will create a slim framework container with your services defined in a configuration file.

$containerBuilder = new \Flexsounds\Slim\ContainerBuilder\ContainerBuilder();
$containerBuilder->setLoader($loader = new \Flexsounds\Slim\ContainerBuilder\Loader\FileLoader('./config'));
$loader->addFile("config.yml");
$app = new Slim\App($containerBuilder->getContainer());

// define your routes here.

$app->run();

Configuration file support

We use hassankhan/config to load the configuration, which has support for YML, XML, JSON and some more.

You can import other configuration files from a configuration file, independent of what file format.

# config.yml
imports:
  - { Resource: otherconfig.xml }

All examples are created in yaml notation!

Creating your services

The service configuration has almost the same notation as Symfony does. Which means you could copy your service configuration you built to a Symfony application (keep in mind, symfony has many more features than Slim Container Builder)

All your services must be defined under the services key and are shared throughout your entire application. The default services like settings, request, response, router etc. from Slim Framework are also available as service

Basic example

Creating a service without any arguments is very simple.

services:
  my.custom.service:
    class: 'App\MyBasicService'

In your code you can use

$app->get('/', function($request, $response){
    $myCustomService = $this->get('my.custom.service');
    // $myCustomService is now an instance of 'App\MyBasicService'
});

Non-Shared services

All services are shared by default. Which means each time you retrieve the service, you get the same instance. When you want a new instance each time you call the service, use the shared key

services:
    my.nonshared.service:
        class: 'App\MyNonSharedService'
        shared: false

Dependency Injection

Some of your services might need some dependency injection, this could be done with the arguments key. You can inject anyting you want, even other services.

services:
  mailer:
    class: 'Location\To\Mailer'


  newslettermanager:
    class: 'App\Newsletter\NewsletterManager'
    arguments:
      - '@mailer'
namespace App\Newsletter;

use Location\To\Mailer;

class NewsletterManager {

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }
}

Contributing

Read the CONTRIBUTING for details