divsmith/airlock 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

divsmith/airlock

Composer 安装命令:

composer require divsmith/airlock

包简介

IoC for non interface conforming dependencies.

README 文档

README

Airlock is a simple abstract adapter class to provide decoupling to dependencies that don't specify their own interfaces.

Installation

Install Airlock by adding the following to your composer.json file

{
    "require": {
        "divsmith/airlock": "1.*"
    }
}

Usage

  1. Create an interface specifying the methods of the concrete dependency you are going to depend on.

    interface ExampleInterface {
        public function method1($args);
        public function method2($args);
        etc...
    }
  2. Create an adapter class that extends Airlock and implements the interface.

    class ExampleInterfaceAdapter extends \Airlock\Airlock implements ExampleInterface {
    
        public function method1($args)
        {
            return $this->delegate(__FUNCTION__, func_get_args());
        }
    
        public function method2($args)
        {
            return $this->delegate(__FUNCTION__, func_get_args());
        }
    
    }

    Each method needs to contain

    return $this->delegate(__FUNCTION__, func_get_args());

    or it will not behave as expected.

  3. Inject the concrete dependency via the adapter constructor. This can either be done by using the predefined Airlock constructor

    $adapter = new ExampleInterfaceAdapter(new ConcreteDependency());

    or typehinted and injected via an IoC container

    class ExampleInterfaceAdapter extends\Airlock\Airlock implements ExampleInterface {
    
        public function __construct(\Namespace\ConcreteDependency $dependency)
        {
            $this->locker = $dependency;
        }
    
        ...
    }
  4. Enjoy! You can now typehint the interface and inject the adapter instead of the concrete dependency (assuming appropriate IoC bindings). Use the adapter exactly as you would the concrete dependency and sleep better at night knowing that your code is decoupled from it.

Other Usage

You can use Airlock with empty interfaces that simply provide latches for your IoC container to resolve. Just inject your concrete dependency into your adapter and let Airlock do the rest. Not as architecturally sound as a well defined interface, but it gets you the benefits of not being tied to the concrete dependency while not requiring you to define each method in the adapter.

interface ExampleInterface {};

class ExampleAdapter extends \Airlock\Airlock implements ExampleInterface{
    
    public function __construct(\Namespace\ConcreteDependency $dependency)
    {
        $this->locker = $dependency;
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-07-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固