承接 nimbly/carton 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

nimbly/carton

Composer 安装命令:

composer require nimbly/carton

包简介

A simple PSR-11 container implementation offering reflection based autowiring.

README 文档

README

Latest Stable Version GitHub Workflow Status Codecov branch License

A simple PSR-11 container implementation.

Requirements

  • PHP 8.2+

Features

  • PSR-11 compliant
  • Singleton and factory builders
  • Service providers
  • Nested container support
  • Reflection based autowiring
  • Aliasing

Install

composer require nimbly/carton

Getting the container

Instantiate container

You can create a new instance of a Container.

$container = new Container;

Singleton instance

Or use the container singleton getInstance method.

$container = Container::getInstance();

Basic usage

Set an instance

The most basic usage is to just assign an instance to the container itself.

$container->set(SomeInterface::class, new SomeClass);

Of course, you don't need to assign objects - it can be anything you like.

$container->set("timezone", "UTC");
$container->set("foo_fh", \fopen("/tmp/foo", "r"));

Retrieving a value

Grab a value from the container by its key.

$someClass = $container->get(SomeClass::class);

NOTE: Retrieving a value that does not exist will throw a Nimbly\Carton\NotFoundException.

Checking for instance

You can check for the existance of items registered in the container.

if( $container->has(SomeClass::class) ){
	echo "Container has SomeClass::class.";
}

Advanced usage

Singleton builder

The singleton builder will ensure only a single instance is ever returned when it is retrieved from the container. The singleton builder requires a callable that will be invoked when it needs to build your dependency and will pass along the Container instance as a single parameter.

It also has the added benefit over the set method by lazily calling your callback. I.e. it will only be created when it is actually needed.

$container->singleton(
	SomeClass::class,
	function(Container $container): void {
		return new SomeClass(
			$container->get("SomeDependency")
		);
	}
);

Factory builder

The factory builder will create new instances each time it is retrieved from the container. The factory builder requires a callable that will be invoked when it needs to build your dependency and will pass along the Container instance as a single parameter.

Just like the singleton builder, it has the added benefit over the set method by lazily calling your callback. I.e. it will only be created when it is actually needed.

$container->factory(
	SomeClass::class,
	function(Container $container): SomeClass {
		return new SomeClass(
			$container->get("SomeDependency")
		);
	}
);

Aliases

You can create aliases of your container items. These aliases simply point to an existing container item and fetch that item for you.

$container->set(Bar::class, new Bar);
$container->alias(Foo:class, Bar::class);
$instance = $container->get(Foo::class); // Returns Bar::class instance.

Alternatively, you can provide an alias or an array of aliases when calling set, singleton, or factory.

$container->singleton(
	Bar:class,
	function(Container $container): Bar {
		return new Bar;
	},
	[Foo::class, Baz::class]
)

$instance = $container->get(Bar::class); // Returns Bar::class instance.
$instance = $container->get(Foo::class); // Returns Bar::class instance.
$instance = $container->get(Baz::class); // Returns Bar::class instance.

Autowiring

You can have instances made for you automatically using the make method - which will attempt to pull dependencies in from the container itself or recursively attempt to make them if not found.

class Foo
{
	public function __construct(
        protected DateTime $date)
	{
	}
}

class Bar
{
	public function __construct(
        protected Foo $foo)
	{
	}
}

$bar = $container->make(Bar::class);

Dependecy injection on instance methods

Calling an instance method couldn't be easier - Carton will attempt to autoresolve dependencies (autowire) for you when making a call to an instance method.

class BooksController
{
	public function get(ServerRequestInterface $request, string $isbn): Response
	{
		return new Response(
			Books::find($isbn)
		);
	}
}

$container->set(ServerRequestInterface::class, $serverRequest);
$response = $container->call([BooksController::class, "get"], ["isbn" => "123123"]);

Adding additional containers

You can extend Carton with additional PSR-11 compliant container instances by calling the addContainer method. When Carton attempts to resolve an item, it will always attempt to resolve locally first, and if not found, will loop through any additional containers you have provided.

For example, if you had a configuration manager that implemented ContainerInterface (PSR-11), you could add it to Carton.

$container->addContainer(
	new Config([
		new FileLoader(__DIR__ . "/config")
	])
);

$container->get("database.connections");

Now you can retrieve your configuration data through the container instance.

Service providers

Service providers allow you to organize your application dependencies in a set of classes.

Create service classes that implement ServiceProviderInterface.

class MyServiceProvider implements ServiceProviderInterface
{
	public function register(Container $container): void
	{
		$container->singleton(
			MyService::class,
			function(Container $container): void {
				return new MyService(
					$container->get(SomeDependency::class)
				);
			}
		);
	}
}

Then register your service providers with the container.

$container->register(new MyServiceProvider);

You can also register multiple services at once and register services by their class name.

// Register group of services at once.
$container->register([
	new MyServiceProvider,
	new MyOtherServiceProvider
]);

// Register services by class name.
$container->register(MyServiceProvider::class);

// Register group of services by class name.
$container->register([
	MyServiceProvider::class,
	MyOtherServiceProvider::class
]);

nimbly/carton 适用场景与选型建议

nimbly/carton 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.81k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 nimbly/carton 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nimbly/carton 我们能提供哪些服务?
定制开发 / 二次开发

基于 nimbly/carton 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-04