定制 luna-fw/djinn 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

luna-fw/djinn

Composer 安装命令:

composer require luna-fw/djinn

包简介

README 文档

README

Build Status Latest Stable Version Total Downloads License

Djinn is a full featured DI container for PHP. We call it Djinn, because a DI container is pretty much as a Djinn: It grants wishes (dependencies) to his master (the caller).

Install

The package must be installed using composer:

composer require luna-fw/djinn

Or include luna-fw/djinn on your composer.json dependencies.

Usage

This package implements the PSR-11 interfaces and Exceptions:

  • Psr\Container\ContainerInterface
    • has(key) - Return an instance based on a string key
    • get(key) - Return true if there is an entry for the given instance
  • Psr\Container\ContainerExceptionInterface
  • Psr\Container\NotFoundExceptionInterface

For further details, please refer to: https://www.php-fig.org/psr/psr-11/

We consider the PSR-11 specification too much simple, so we added some additional interfaces to the implementation (those extended implementations don't prevent someone to use the container as a PSR-11 compatible container).

Here are the extended interfaces:

  • ContainerConfigContract - Holds the methods related with the container config.
    • bind - Configures the resolution of a given key
    • singleton - Configures the resolution of a given key, always returning and immutable instance (singleton).
    • contextual - Same as bind, but the resolution depends on "who" is asking for the instance.
    • contextualSingleton - Same as contextual, but always return an immutable instance (singleton).
  • ExtendedContainerContract - Holds the methods related with the DI injection/resolution.
    • getFor - Returns an instance of the required class or interface, resolving all the dependencies, based on the defined configs. Allow contextual binding.
    • run - Executes a method, resolving all it's dependencies, based on the defined config.

To use it, you need to have the vendor/autoload.php file included. Then all you have to do is to instantiate the Luna\Djinn class and star using it.

<?php
require 'vendor/autoload.php';

// keep the container instance available globally
$djinn = new \Luna\Djinn();

// configure the bindings
$djinn->bind(FooContract::class, function() {
    return new FooClass('paramether'); 
});

// every time you need an instance that implements FooContract, you call the get method
$foo = $djinn->get(FooContract::class);

echo get_class($foo); // prints FooClass

This is the simplest use of the DI container. In a practical environment, you'll want to split all the config for the container on a specific file (like dependencies.php, or something like that), and you'll try to push all the calls to the container methods to the "borders" of the system (probably to the router), since PSR-11 recommends to don't pass the container object inside other objects.

Pay attention that we always have two steps on the resolution:

  1. Configure the container, letting him know how it should resolve your dependencies.
  2. Use the container

Some important considerations:

  • If your wish is a valid class, that don't take any parameters on it's constructor, it's instantiated automatically.
  • If your wish is a valid class, and all it's constructor parameters are resolvable by the container, they'll be resolved recursively.
  • A common use, is to use an interface as the wish, and a class name as the granted, at this case, the granted class will be resolved and granted.
  • It's possible to have multiple binding to the same wish (i.e multiple implementations for the same interface ), granting based on who is wishing. Ie. if you call the same interface from different places, you can get different results based on the context.
  • When automatically resolving parameters for a constructor or method, we always try to resolve using the variable name first, then by the type hint.
  • We also allow to run a function/method through Djinn, that will try to resolve all the dependencies. This is specially useful when building a router, then you don't have to manually inject everything through the constructor.
  • We also have the singleton binding option, that always return the same instance for the same wish.

Configuration/Binding Examples

Simple binding:

<?php
$djinn->bind(FooClass::class, function() {
    return new FooClass('paramether'); 
});

$djinn->bind('barservice', function() {
    return new BarClass($djinn->get(FooClass::class)); 
});

Binding a singleton (the same, but Djinn will always return the same object):

<?php
$djinn->singleton(FooClass::class, function() {
    return new FooClass('paramether'); 
});

Binding interfaces to implementations:

<?php
$djinn->bind(FileUploaderInterface::class, YoutubeUploader::class);

Contextual binding (lets say that you want to upload photos to google photos and videos to youtube):

<?php
$djinn->contextual(PhotoContoller::class, FileUploaderInterface::class, GooglePhotosUploader::class);
$djinn->contextual(VideoController::class, FileUploaderInterface::class, YoutubeUploader::class);

Binding a primitive (and running a method):

<?php
$djinn->contextual(FooClass::class.':sum', '$value1', 3);
$djinn->contextual(FooClass::class.':sum', '$value2', 7);

$djinn->run('sum', $djinn->get(FooClass::class)); // returns 10 

To get more examples on this container usage, please refer to the tests under tests folder.

Testing

To run the tests on the package, first run composer install, and then go to the root folder, and run one of the commands below:

On Linux/Unix:

./vendor/bin/phpunit

On Windows:

.\vendor\bin\phpunit.bat

Or even (any platform):

php ./vendor/phpunit/phpunit/phpunit

You can use all the phpunit options.

Contribute

Luna framework is still a very premature project. I just started, and I'm alone so far.

If you want to contribute, contact me at jeferson@webage.solutions, and we can discuss how you can help.

Right now, we don't have any process defined for pull requests, so you can help me to define the process and be part of it.

luna-fw/djinn 适用场景与选型建议

luna-fw/djinn 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 02 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 luna-fw/djinn 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 luna-fw/djinn 我们能提供哪些服务?
定制开发 / 二次开发

基于 luna-fw/djinn 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-10