定制 selvinortiz/zit 二次开发

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

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

selvinortiz/zit

Composer 安装命令:

composer require selvinortiz/zit

包简介

Tiny dependency management library for PHP

README 文档

README

Zit

Build Status Total Downloads Latest Stable Version

Description

Zit is a tiny dependency injection library inspired by Pimple and written by Selvin Ortiz

Requirements

Install

composer require selvinortiz/zit

Test

sh spec.sh

Usage

Dependency management is a difficult topic to explain but essentially, Zit lets you stuff things into it that you can pop out later throughout your application.

// Make an instance
$app = Zit::make();

// Stash a config object
$app->stash('config', new Config(['usr' => 'root', 'pwd' => 'secret']));

// Bind a session generator
$app->bind('session', function() {
    return new Session();
});

// Bind a database generator
$app->bind('db', function($app) {
    return new Db($app->config->usr, $app->config->pwd);
});

// Extend your $app with new functionality
$app->extend('end', function($app) {
    $app->db->close();
    $app->session->destroy();
});

// Finish
$app->end();

You can also use Zit without making a new instance, the static way if you will.

// Stash a config object
Zit::stash('config', new Config(['usr' => 'root', 'pwd' => 'secret']));

// Bind a session generator
Zit::bind('session', function() {
    return new Session();
});

// Bind a database generator
Zit::bind('db', function($zit) {
    return new Db($zit->config->usr, $zit->config->pwd);
});

// Extend Zit with new functionality
Zit::extend('end', function($zit) {
    $zit->db->close();
    $zit->session->destroy();
});

// Finish
Zit::end();

Which approach should you use? Whatever you prefer. If you aren't sure, you can use the static way unless you need to create your own instance that extends Zit

Note

Whether you bind(), stash(), or extend() it. You can pop it out using:

Zit::pop('db');  // Formal
Zit::db();       // Via __callStatic()
Zit::db          // Via __callStatic() property sniffing

// If you had done $app = Zit::make()
$app->pop('db'); // Formal
$app->db();      // Via __call()
$app->db;        // Via __get()

API

pop($id, $args = array())

pop() lets you pop a dependency out of the container

Zit::pop('db');
// See the note above on popping stuff out

bind($id, callable $serviceGenerator)

bind() lets you register a service generator. Useful when you need to instantiate a service that depends on a config object for example. Services get generated only once on first call.

Zit::make()->bind('db', function($zit) {
   return new Db($zit->config->usr, $zit->config->pwd); 
});

stash($id, $service)

stash() lets you stash anything inside the container. You get out what you put in, which is useful if you need to stash a bunch of stuff that needs to be accessible throughout your whole application.

Zit::make()->stash('session', new Session())

extend($id, $callback)

extend() gives you the ability to add new functionality to the container

Zit::make()->extend('logout', function($zit) {
    $zit->session->logout();
});

Contribute

Zit wants to be friendly to first time contributors. Just follow the steps below and if you have questions along the way, please reach out.

  1. Fork it!
  2. Create your bugfix or feature branch
  3. Commit and push your changes
  4. Submit a pull request

License

Zit is open source software licensed under the MIT License

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-08-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固