All Projects → godruoyi → easy-container

godruoyi / easy-container

Licence: other
A small PHP dependency injection container from Laravel Container, support PHP 5.3

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to easy-container

fengming
No description or website provided.
Stars: ✭ 14 (-56.25%)
Mutual labels:  container
inspr
Inspr is an agnostic application mesh for simpler, faster, and securer development of distributed applications (dApps).
Stars: ✭ 49 (+53.13%)
Mutual labels:  container
docker-garby
Just another Docker maintenance script, managing garbage collection of Docker containers and images.
Stars: ✭ 36 (+12.5%)
Mutual labels:  container
docker
🐳 Official Docker image of the SinusBot for TeamSpeak 3 and Discord.
Stars: ✭ 50 (+56.25%)
Mutual labels:  container
EvDev
Full-Featured Dockerized Development Environment
Stars: ✭ 21 (-34.37%)
Mutual labels:  container
httptest
A simple concurrent HTTP testing tool
Stars: ✭ 42 (+31.25%)
Mutual labels:  container
edge-home-orchestration-go
Home Edge Project in LF Edge - Edge Orchestration for home edge devices to enabling smart home use cases.
Stars: ✭ 68 (+112.5%)
Mutual labels:  container
imagepullsecret-patcher
A simple Kubernetes client-go application that creates and patches imagePullSecrets to service accounts in all Kubernetes namespaces to allow cluster-wide authenticated access to private container registry.
Stars: ✭ 159 (+396.88%)
Mutual labels:  container
docker-rsyslog
Rsyslog server with multiple input listeners (UDP/TCP/RELP) and outputs (file/kafka/syslog).
Stars: ✭ 18 (-43.75%)
Mutual labels:  container
Huddy
Huddy = Hugo + Caddy docker container
Stars: ✭ 14 (-56.25%)
Mutual labels:  container
container-amiga-gcc
Containerfile for AmigaOS Cross-Compiler Toolchain
Stars: ✭ 51 (+59.38%)
Mutual labels:  container
HerokuContainer
Dockerized ASP.NET Core Web API app in Heroku
Stars: ✭ 26 (-18.75%)
Mutual labels:  container
singularityhub.github.io
Container tools for scientific computing! Docs at https://singularityhub.github.io/singularityhub-docs
Stars: ✭ 68 (+112.5%)
Mutual labels:  container
kube-notary
A Kubernetes watchdog for verifying image trust with Codenotary (www.codenotary.com)
Stars: ✭ 55 (+71.88%)
Mutual labels:  container
docker-postgres-windows
No description or website provided.
Stars: ✭ 19 (-40.62%)
Mutual labels:  container
elixir-ms
an elixir microservice base/skeleton 💀
Stars: ✭ 39 (+21.88%)
Mutual labels:  container
fastfreeze
Turn-key solution to checkpoint/restore applications running in Linux containers
Stars: ✭ 68 (+112.5%)
Mutual labels:  container
render
A simple web service that renders a Blender 3D scene with custom text.
Stars: ✭ 27 (-15.62%)
Mutual labels:  container
coreos-gpu-installer
Scripts to build and use a container to install GPU drivers on CoreOS Container Linux
Stars: ✭ 21 (-34.37%)
Mutual labels:  container
github-ci
An example GitHub Action (CI) to build a Singularity container
Stars: ✭ 46 (+43.75%)
Mutual labels:  container

A simple dependency injecting container from laravel.

styleci passed Latest Stable Version Total Downloads License

Why

中文文档

Currently more popular php container:

Pimple is a simple and excellent php 5.3 container, which is also the most used service container, and the installed capacity of packagist is also up to 1000W+ .But Pimple just a simple service container that does not support many features such as:

class Cache
{
    public function __construct(Config $config){}
}

class Config
{
}

// not support
$cache = $container->make('Cache');

Pimple Does not support the automatic injection of dependency parameters, when you need to rely on other objects object, you can only instantiate the required parameters.

Laravel Container is the most full-featured service container, including auto-injection, load-loading, alias, TAG, and so so. But the official does not recommend using the component in non-laravel project.

If you have noticed the composer.json file under that component,You will find that he depends on the illuminate/contracts component.(see also)

Based on this, easy-container was born, and the project code relied heavily on Laravel Container 😄 😄 . You can use it like a Laravel Container container.

Install

🐝 Now, we support most PHP versions, which you can view here.

composer require godruoyi/easy-container

Use

You can get more help with container usage at laravel.com.

Initialize the container.

$app = new Godruoyi\Container\Container;

The following documents support from laravel.com, reproduced please indicate the source.

Simple Bindings

We can register a binding using the bind method, passing the class or interface name that we wish to register along with a Closure that returns an instance of the class:

$app->bind('HelpSpot\API', function ($app) {
    return new HelpSpot\API($app->make('HttpClient'));
});

Note,All anonymous functions accept the service container instance as a parameter.

Binding A Singleton

The singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:

$app->singleton('HelpSpot\API', function ($app) {
    return new HelpSpot\API($app->make('HttpClient'));
});

Each time you call $app['HelpSpot\API'] will return the same object.

Binding A Singleton

The singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:

$api = new HelpSpot\API(new HttpClient);

$app->instance('HelpSpot\API', $api);

Binding Interfaces To Implementations

A very powerful feature of the service container is its ability to bind an interface to a given implementation. For example, let's assume we have an EventPusher interface and a RedisEventPusher implementation. Once we have coded our RedisEventPusher implementation of this interface, we can register it with the service container like so:

$app->bind(
    'App\Contracts\EventPusher',
    'App\Services\RedisEventPusher'
);

This statement tells the container that it should inject the RedisEventPusher when a class needs an implementation of EventPusher. Now we can type-hint the EventPusher interface in a constructor, or any other location where dependencies are injected by the service container:

use App\Contracts\EventPusher;

/**
 * Create a new instance of the class, which will be injected into the App\Services\RedisEventPusher instance.
 *
 * @param  EventPusher  $pusher
 * @return void
 */
public function __construct(EventPusher $pusher)
{
    $this->pusher = $pusher;
}

Resolving

The make Method

You may use the make method to resolve a class instance out of the container(regardless of what type of parameter the object needs). The make method accepts the name of the class or interface you wish to resolve:

$api = $app->make('HelpSpot\API');

The mark method is the most important method I think of,You can simply use the "type prompt" way to add dependencies,the container will automatically parse all the parameters you need.

// Automatically parses the dependencies required by the UserController constructor
$userController = $app->make(UserController::class);

class UserController
{
    public function __construct(UserRepository $users, HttpClient $client, $other = 'default')
    {
    }
}

PSR-11

Laravel's service container implements the PSR-11 interface. Therefore, you may type-hint the PSR-11 container interface to obtain an instance of the Laravel container:

use Psr\Container\ContainerInterface;

$service = $app->get('Service');

LISTEN

MIT

Thanks

laravel-china

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].