定制 irfantoor/container 二次开发

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

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

irfantoor/container

Composer 安装命令:

composer require irfantoor/container

包简介

Irfan's Container: An abstraction for many types of containers with a single API.

README 文档

README

Note: this Readme will be updated shortly, it reflects the functionality of v1.0.4

Container

Container implementing Psr\ContainerInterface, ArrayAccess, Countable and IteratorAggregate.

The identifiers can use dot notation to access an identifier down a hierarchical level, e.g. to access $container['environment']['headers']['host'] you can code in doted notation as: $config['environment.headers.host']

Initializing

You can initialize by passing an array of key value pairs while creating a new instance, it will use array in memory for this case.

<?php

use IrfanTOOR\Container;

$container = new IrfanTOOR\Container(
  # id => values
  'app' => [
    'name'    => 'My App'
    'version' => '1.1',
  ]
);

Setting

You can by set a service in the Container by using the method 'set':

use IrfanTOOR\Container;

$container = new IrfanTOOR\Container();

# setting hello => world
$container->set('hello', 'world');

# using an array notation
$container->set(['hello' => 'world']);

# defining multiple
$container->set([
  'hello'     => 'world',
  'box'       => 'empty',
  'something' => null,
  'array'     => [
    'action'       => 'go',
    'step'         => 1,
  ],
]);

# defining sub values
$container->set('app.name', 'Another App');
$container->set('debug.level', 2);

# defining a factory service
$container->factory('hash', function(){
    $salt = rand(0, 10000);
    return md5($salt . time());
});

using array access mechanism:

$container['hello'] = 'world';
$container['debug.log.file'] = '/my/debug/log/file.log';

Getting

You can get the stored value or the result from a service in the container by its identifier using the method 'get':

# returns a random hash
$hash = $container->get('hash');

you can also use the array access:

$hash = $container['hash'];

Checking if a value is present in the container

You can use the method 'has' to check if the container has an entry identified with the identifier id:

if ($container->has('hash')) {
  # this will be executed even if the given identifier has the value NULL, 0
  # or false
  echo 'random hash : ' . $container->get('hash');
}

using the array access the above code will become:

if (isset($container['hash']) {
  # this will be executed even if the given identifier has the value NULL, 0
  # or false
  echo 'random hash : ' . $container['hash'];
}

Removing an entry

You can use the method 'remove' or unset on the element:

# using method remove
$container->remove('hash');

# using unset
unset($container['hash']);

Container to Array

The method 'toArray' can be used to convert the container into an array:

$array = $container->toArray();

Array of service identifiers

The array of services identifiers can be retrieved by using the method 'keys':

$services = $container->keys();

Count

The number of items present in the container can be retrieved using the method 'count'. Note that it will return the count of the items at base level.

# will return 1 for the Collection defined in initialization section
$count = $container->count();

Iteration

The container can directly be used in a foreach loop or wherever an iterator is used. for example the code:

foreach($container->toArray() as $k => $v)
{
    $v->close();
}

can be simplified as:

foreach($container as $k => $v)
{
    $v->close();
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固