Tiny dependency management library for PHP

v1.0.1 2016-06-21 05:21 UTC

This package is auto-updated.

Last update: 2024-10-29 04:18:01 UTC


README

Zit

Build Status Total Downloads Latest Stable Version

Description

Zit is a tiny dependency injection library inspired by Pimple and written by Selvin Ortiz

Requirements

Install

composer require selvinortiz/zit

Test

sh spec.sh

Usage

Dependency management is a difficult topic to explain but essentially, Zit lets you stuff things into it that you can pop out later throughout your application.

// Make an instance
$app = Zit::make();

// Stash a config object
$app->stash('config', new Config(['usr' => 'root', 'pwd' => 'secret']));

// Bind a session generator
$app->bind('session', function() {
    return new Session();
});

// Bind a database generator
$app->bind('db', function($app) {
    return new Db($app->config->usr, $app->config->pwd);
});

// Extend your $app with new functionality
$app->extend('end', function($app) {
    $app->db->close();
    $app->session->destroy();
});

// Finish
$app->end();

You can also use Zit without making a new instance, the static way if you will.

// Stash a config object
Zit::stash('config', new Config(['usr' => 'root', 'pwd' => 'secret']));

// Bind a session generator
Zit::bind('session', function() {
    return new Session();
});

// Bind a database generator
Zit::bind('db', function($zit) {
    return new Db($zit->config->usr, $zit->config->pwd);
});

// Extend Zit with new functionality
Zit::extend('end', function($zit) {
    $zit->db->close();
    $zit->session->destroy();
});

// Finish
Zit::end();

Which approach should you use? Whatever you prefer. If you aren't sure, you can use the static way unless you need to create your own instance that extends Zit

Note

Whether you bind(), stash(), or extend() it. You can pop it out using:

Zit::pop('db');  // Formal
Zit::db();       // Via __callStatic()
Zit::db          // Via __callStatic() property sniffing

// If you had done $app = Zit::make()
$app->pop('db'); // Formal
$app->db();      // Via __call()
$app->db;        // Via __get()

API

pop($id, $args = array())

pop() lets you pop a dependency out of the container

Zit::pop('db');
// See the note above on popping stuff out

bind($id, callable $serviceGenerator)

bind() lets you register a service generator. Useful when you need to instantiate a service that depends on a config object for example. Services get generated only once on first call.

Zit::make()->bind('db', function($zit) {
   return new Db($zit->config->usr, $zit->config->pwd); 
});

stash($id, $service)

stash() lets you stash anything inside the container. You get out what you put in, which is useful if you need to stash a bunch of stuff that needs to be accessible throughout your whole application.

Zit::make()->stash('session', new Session())

extend($id, $callback)

extend() gives you the ability to add new functionality to the container

Zit::make()->extend('logout', function($zit) {
    $zit->session->logout();
});

Contribute

Zit wants to be friendly to first time contributors. Just follow the steps below and if you have questions along the way, please reach out.

  1. Fork it!
  2. Create your bugfix or feature branch
  3. Commit and push your changes
  4. Submit a pull request

License

Zit is open source software licensed under the MIT License