定制 bayfrontmedia/container 二次开发

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

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

bayfrontmedia/container

Composer 安装命令:

composer require bayfrontmedia/container

包简介

An easy to use PSR-11 compatible dependency injection container.

README 文档

README

An easy to use PSR-11 compatible dependency injection container.

License

This project is open source and available under the MIT License.

Author

Bayfront Media

Requirements

  • PHP ^8.0 (Tested up to 8.4)

Installation

composer require bayfrontmedia/container

Usage

Start using the container

use Bayfront\Container;

$container = new Container();

Public methods

set

Description:

Set an entry into the container.

Anonymous functions (closures) are called on the first get().

Parameters:

  • $id (string)
  • $value (mixed)
  • $overwrite = false (bool): If false, a ContainerException is thrown if an entry with the same ID already exists. Otherwise, it is overwritten.

Returns:

  • (void)

Throws:

  • Bayfront\Container\ContainerException

Example:

Any type of value can be set in the container.

Services with dependencies can be set using an anonymous function (a \Closure) which returns an instance of the class.

The first time a service is requested from the container using get(), the anonymous function is called and the result is saved. On subsequent calls, the same ID always returns the same result.

The anonymous function always calls the container instance as the first argument. This allows you to reference other items from the container, if needed. If you do not need access to the container, the parameter may be omitted from the function signature.

// Set a service with no dependencies

$container->set('Fully\Namespaced\ClassName', function () {
    return new ClassName();
});

// Set a service with dependencies

$container->set('Fully\Namespaced\ClassName', function (ContainerInterface $container) {
    $dependency = $container->get('Fully\Namespaced\Dependency');
    return new ClassName($dependency);
});

// Any type of value can be set, then used as a parameter

$container->set('classname_config', [
    // Config array
]);

$container->set('Fully\Namespaced\ClassName', function (ContainerInterface $container) {
    $config = $container->get('classname_config');
    return new ClassName($config);
});

// Preexisting class instances can be set without using an anonymous function

$class = new ClassName();
$container->set('ClassName', $class);

getEntries

Description:

Returns an array of all ID's existing in the container.

Parameters:

  • None.

Returns:

  • (array)

Example:

$entries = $container->getEntries();

get

Description:

Get an entry from the container by its ID or alias.

Parameters:

  • $id (string)

Returns:

  • (mixed)

Throws:

  • Bayfront\Container\NotFoundException

Example:

$service = $container->get('Fully\Namespaced\ClassName');

make

Description:

Makes and returns a new class instance, automatically injecting dependencies which exist in the container.

Parameters:

  • $class (string)
  • $params = [] (array): Additional parameters to pass to the class constructor.

Returns:

  • (mixed)

Throws:

  • Bayfront\Container\ContainerException
  • Bayfront\Container\NotFoundException

Example:

class ClassName {

    protected $service;
    protected $config;
    
    public function __construct(AnotherService $service, array $config)
    {
        $this->service = $service;
        $this->config = $config;
    }

}

$instance = $container->make('Fully\Namespaced\ClassName', [
    'config' => []
]);

has

Description:

Does entry or alias exist in the container? (ie: Can an entry be resolved using get() with this ID?)

Parameters:

  • $id (string): ID or alias

Returns:

  • (bool)

Example:

if ($container->has('Fully\Namespaced\ClassName')) {
    // Do something
}

remove

Description:

Remove entry from container, if existing.

Parameters:

  • $id (string)

Returns:

  • (void)

Example:

$container->remove('Fully\Namespaced\ClassName');

setAlias

Description:

Set an alias for a given ID.

Parameters:

  • $alias (string)
  • $id (string)
  • $overwrite = false (bool): If false, a ContainerException is thrown if an alias with the same name already exists. Otherwise, it will be overwritten.

Returns:

  • (void)

Throws:

  • Bayfront\Container\ContainerException

Example:

$container->setAlias('alias', 'Fully\Namespaced\ClassName');

One benefit of aliases is that they allow you to retrieve entries from the container in a concise, easy to remember manner. In addition, aliases allow you to bind an interface to an implementation.

For example:

$container->setAlias('Fully\Namespaced\Implementation', 'Fully\Namespaced\Interface');

Now, whenever a class requires an implementation of Fully\Namespaced\Interface, an instance of Fully\Namespaced\Implementation will be returned, if existing in the container.

getAliases

Description:

Returns an array of all existing aliases.

Parameters:

  • None.

Returns:

  • (array)

Example:

$aliases = $container->getAliases();

hasAlias

Description:

Does alias exist?

Parameters:

  • $alias (string)

Returns:

  • (bool)

Example:

if ($container->hasAlias('alias')) {
    // Do something
}

removeAlias

Description:

Remove alias.

Parameters:

  • $alias (string)

Returns:

  • (void)

Example:

$container->removeAlias('alias');

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固