2017 © Pedro Peláez
 

library zit

Tiny dependency management library for PHP

image

selvinortiz/zit

Tiny dependency management library for PHP

  • Tuesday, June 21, 2016
  • by selvinortiz
  • Repository
  • 1 Watchers
  • 4 Stars
  • 233 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 14 % Grown

The README.md

Zit, (*1)

Build Status Total Downloads Latest Stable Version, (*2)

Description

Zit is a tiny dependency injection library inspired by Pimple and written by Selvin Ortiz, (*3)

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., (*4)

// 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., (*5)

// 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, (*6)

Note

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

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, (*8)

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., (*9)

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., (*10)

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

extend($id, $callback)

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

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., (*12)

  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, (*13)

The Versions

21/06 2016

dev-master

9999999-dev https://github.com/selvinortiz/zit

Tiny dependency management library for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dependency injection zit

21/06 2016

dev-dev

dev-dev https://github.com/selvinortiz/zit

Tiny dependency management library for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dependency injection zit

21/06 2016

v1.0.1

1.0.1.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dependency injection zit

16/06 2016

v1.0.0

1.0.0.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dependency injection zit

21/02 2015

v0.5.2

0.5.2.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

dependency injection zit

26/03 2014

v0.5.1

0.5.1.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

dependency injection zit

19/03 2014

v0.5.0

0.5.0.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

dependency injection zit

14/09 2013

v0.4.1

0.4.1.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

dependency injection zit

05/09 2013

v0.4.0

0.4.0.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

dependency injection zit

22/08 2013

v0.2.0

0.2.0.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

dependency injection zit

21/08 2013

v0.1.0

0.1.0.0 https://github.com/selvinortiz/zit

Tiny dependency management library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

dependency injection zit