GithubHelp home page GithubHelp logo

gimme's Introduction

gimme Build Status

Inject your services with magic.

A super-simple library to auto-magically inject named services from a $services* argument. Services come from service providers, that can be introduced to Gimme through a simple stack layout.

* Or not, have a look at Gimme\Resolver::setArgumentName

<?php

use Gimme\Resolver;
use Pimple;
use Foo;

// Create a service provider, for this example, using Pimple:
$pimple = new Pimple;
$pimple['myService'] = function() {
    return new Foo;
};

// Introduce the provider to Gimme, wrapping it in a closure
// that knows how to talk to Pimple:
$resolver = new Resolver;
$resolver->pushProvider(
    function($serviceName) use($pimple) { return $pimple[$serviceName]; };
);

// Use it. Tell Gimme that this callable (which can be a closure, a function
// or a method) wants to use a service called 'myService', which one of the
// registered service providers will be able to fetch for you.
$resolver->call(function($services = array('myService')) {
    var_dump($services->myService); #=> instance of Foo
});

Features:

  • Light-weight, tested code-base.

  • Hands-off service injection through the $services argument

  • Stupid-simple to implement support for any type of service provider

  • Your callable/method's arguments are preserved, so it's a great fit with those modern space-age frameworks

    // warning: example may not make much sense
    $app->get('/user/{id}', $resolver->bind(function($id, $services = array('user') {
        return $services->user->get($id);
    }));
  • Service providers may be a simple closure:

    $resolver->pushProvider(function($serviceName) {
        if($serviceName == 'bananas') {
            return 'Here, have some bananas!';
        }
    });
  • Support for callable-binding (bind your callable to the method injector, call it whenever).

    $bound = $resolver->bind(function($services = array('someService')) {
        return $services->someService->doThings();
    });
    
    // Give the bound method to your killer router of sorts:
    $app->get('/foo', $bound);
  • Support for service-name aliases:

    $resolver->alias('bananaService', 'turnipService');
    $resolver->call(function($services = array('bananaService')) {
        print "I love {$services->bananaService->name()}!"; #=> I love turnip!
    });

Should I use this?

Dunno. It was fun to work on, but I'm not going to be the one to tell you to use it. The code is solid, if there's a point in using it for your needs, it's a point for you to make.

Install it:

Use Composer, add the following to your composer.json:

{
    "require": {
        "filp/gimme": "dev-master"
    }
}

And then:

$ composer install

gimme's People

Contributors

filp avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.