jtreminio/zimple 问题修复 & 功能扩展

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

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

jtreminio/zimple

Composer 安装命令:

composer require --dev jtreminio/zimple

包简介

A wrapper for Pimple container that adds several useful features to make it easier to manage your code!

README 文档

README

This is a wrapper around the simple Pimple container. It provides a few features that makes handling objects and writing tests much easier.

If you have a codebase that is untestable and you are unable to offer deep refactoring to make it untestable, having a container such as this one is a good step toward gaining control over your codebase.

Installation

Add it to your composer.json file:

"jtreminio/zimple": "1.0.*@dev"

Then run ./composer.phar update. This will also install the Pimple container

Now just put it in your code:

Zimple::setPimple(new Pimple);

Usage

Zimple accepts a fully qualified name:

$date = Zimple::get('\DateTime');

You can also pass in an optional array of parameters for the object constructor:

$date = Zimple::get('\DateTime', array('now', Zimple::get('\DateTimeZone')));

By default Zimple returns a new instance of an object everytime you call Zimple::get(). You can override this behavior by calling Zimple::set() and setting the third parameter to true:

$today = Zimple::get('\DateTime');
$tomorrow = Zimple::get('\DateTime', array('tomorrow'));

// $today !== $tomorrow

Zimple::set('\DateTime', $tomorrow, true);

$tomorrowDup = Zimple::get('\DateTime');

// $tomorrowDup == $tomorrow

$twoDaysAgo = Zimple::get('\DateTime', array('2 days ago'));

// $twoDaysAgo == $tomorrow

You can define objects as you normally would for Pimple before or after passing to Zimple:

// From Pimple's homepage
$pimple = new Pimple;

// define some objects
$pimple['session_storage'] = function ($c) {
    return new $c['session_storage_class']($c['cookie_name']);
};

$pimple['session'] = function ($c) {
    return new Session($c['session_storage']);
};

Zimple::setPimple($pimple);

Then access them as normal: $session = Zimple::get('session_storage');

Zimple is a Weird Name

Zimple wraps around Pimple, which is a gross name. SandyZoop recommended I name it Cyst or Zit, which is equally gross. The goal of this container is to make dependency management simple and easy to use, and make previously untestable code testable.

Pimple + Simple = Zimple.

You can always alias it with

use jtreminio\Zimple\Zimple as Container;
Container::setPimple(new Pimple);
$date = Container::get('\DateTime');

Testing

Zimple makes the process of inserting mocks from your tests extremely easy. It makes use of ::set() to prevent the code from overwriting the PHPUnit mock. Check out the tests for several easy examples.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-04-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固