Lumberjack
WebsiteRareloopTimber DocumentationTwig Documentation
v3
v3
  • Documentation
  • Getting Started
    • Installation
    • Quick Start
    • Configuration
  • The Basics
    • Lifecycle
    • Post Types
    • WordPress Controllers
    • Routing
    • HTTP Responses
  • Container
    • Using the Container
    • Service Providers
    • Facades
  • Misc
    • Changelog
    • Contributing
    • Notable Mentions
    • Code of Conduct
    • View on GitHub
    • Submit an issue
Powered by GitBook
On this page
  • Introduction
  • Accessing the container
  • Setting entries in the container
  • Retrieving entries from the container
  • Use the container to create an object
  • Set concrete implementations for interfaces
  • Dependency Injection
  1. Container

Using the Container

PreviousHTTP ResponsesNextService Providers

Last updated 2 years ago

Introduction

Lumberjack features a PSR11 compatible container, powered by the popular open source . If this is a new term for you checkout this and don't worry, you don't have to make use of it if you don't want to.

Todo: flesh this out more and add more examples

Accessing the container

In the default Lumberjack functions.php you'll find the following code:

$app = new Application(__DIR__);

This creates the Application and the $app variable becomes your reference to the container.

Setting entries in the container

To add something to the container you simply call bind(), e.g.:

$app->bind('foo', 'bar');

Retrieving entries from the container

To retrieve an entry from the container you can use get(), e.g.:

$foo = $app->get('foo');

Use the container to create an object

You can use the container to create an object from any class that your application can autoload using make(), e.g.:

$comment = $app->make('\MyNamespace\Comment');

When creating an object using make, all the dependencies required by it's __construct() function will be automatically resolved from the container using type hinting. If your object requires additional parameters that can not resolved by type hinting you can pass them as a second param, e.g.:

namespace MyNamespace;

class Comment
{
  public function __construct(ClassInContainer $resolvable, $param1, $param2) {}
}

...

$comment = $app->make('\MyNamespace\Comment', [
  'param1' => 123,
  'param2' => 'abc',
]);

Set concrete implementations for interfaces

You can also tell the container what concrete class to use when resolving a certain type hinted interface. This allows you to write your app code against contracts and then use the container to switch in the correct implementation at runtime.

namespace App;

interface PaymentGateway {}

class StripePaymentGateway implements PaymentGateway{}

// In your service provider
$app->bind('\App\PaymentGateway', '\App\StripePaymentGateway');

// In your application
$gateway = $app->make('\App\PaymentGateway');
// $gateway is instance of '\App\StripePaymentGateway'

Dependency Injection

TODO: If you would like to know more about this, please submit an issue

PHPDI
great intro