wpsyntex / polylang-di
Polylang 的依赖注入容器。
dev-main
2022-04-11 09:48 UTC
Requires
- php: >=5.6
Requires (Dev)
- wpsyntex/polylang-cs: dev-main
- wpsyntex/polylang-phpstan: dev-master
- yoast/phpunit-polyfills: ^1.0.3
This package is auto-updated.
Last update: 2024-09-11 15:16:35 UTC
README
WPSyntex (Polylang) 的依赖注入容器。
此包符合 PSR-4 和 PSR-11。
然而,未实现 PSR-11 的接口,以避免与其他可能使用不同版本的插件和主题冲突。
该项目基本上是对 league/container 旧版本的分支,经过一些修改以保留我们项目中需要的特性。
安装
通过 Composer
composer require wpsyntex/polylang-di
要求
此包支持 php 5.6+。
文档
创建和检索共享和不共享的实例
<?php namespace Foobar; class Foo {} $container = new \WP_Syntex\Polylang_DI\Container(); // Add to the container. $container->add( 'foo', 'Foobar\Foo' ); // Instanciate and retrieve. $foo1 = $container->get( 'foo' ); $foo2 = $container->get( 'foo' ); // We created 2 different instances. var_dump( $foo1 === $foo2 ); // False. // Add to the container as a shared item. $container->addShared( 'foo', 'Foobar\Foo' ); // Instanciate and retrieve. $foo1 = $container->get( 'foo' ); $foo2 = $container->get( 'foo' ); // We created only one instance. var_dump( $foo1 === $foo2 ); // True.
添加参数
<?php namespace Foobar; class Foo { public $int; public $string; public function __construct( $int, $string = null ) { $this->int = $int; $this->string = $string; } } $container = new \WP_Syntex\Polylang_DI\Container(); // Add to the container. $container->add( 'foo', Foo::class ) ->withArgument( 46 ); // Instanciate and retrieve. $foo1 = $container->get( 'foo' ); $foo2 = $container->get( 'foo' ); // We created 2 different instances with the same arguments. var_dump( $foo1 === $foo2 ); // False. var_dump( $foo1->int ); // 46. var_dump( $foo1->string ); // Null. var_dump( $foo1->int === $foo2->int ); // True. var_dump( $foo1->string === $foo2->string ); // True. // Retrieve the definition and add an argument. $container->extend( 'foo' ) ->withArgument( 'a string' ); // Not not shared items only. // Instanciate and retrieve. $foo3 = $container->get( 'foo' ); var_dump( $foo3->int ); // 46. var_dump( $foo3->string ); // 'a string'. // Retrieve the definition and replace previous arguments. $container->extend( 'foo' ) ->withNewArguments( [ 22, 'some string', ] ); $foo4 = $container->get( 'foo' ); var_dump( $foo4->int ); // 22. var_dump( $foo4->string ); // 'some string'. // With an anonymous function. $container->add( 'prefix', 'String prefix' ); $container->add( 'foo', function ( $container, $int, $string ) { $prefix = $container->get( 'prefix' ); return new Foo( $int, $prefix . ' - ' . $string ); } ) ->withArguments( [ 32, 'another string', ] ); $foo5 = $container->get( 'foo' ); var_dump( $foo5->int ); // 32. var_dump( $foo5->string ); // 'String prefix - another string'.
引用其他项目(或否则)
<?php namespace Foobar; class Foo {} class Bar { public $foo; public $string; public function __construct( Foo $foo, $string = null ) { $this->foo = $foo; $this->string = $string; } } $container = new \WP_Syntex\Polylang_DI\Container(); // Add to the container. $container->addShared( 'foo', Foo::class ); $container->add( 'bar', Bar::class ) ->withArgument( 'foo' ) // Same identifier used for Foobar\Foo. ->withArgument( new \WP_Syntex\Polylang_DI\Argument\RawArgument( 'foo' ) // Same identifier used for Foobar\Foo, but we just want to pass a string here. ); // Instanciate and retrieve. $bar1 = $container->get( 'bar' ); $bar2 = $container->get( 'bar' ); // We created 2 different instances with the same arguments. var_dump( $bar1 === $bar2 ); // False. var_dump( $bar1->foo instanceof Foo ); // True. var_dump( $bar1->foo === $bar2->foo ); // True. var_dump( $bar1->string ); // 'foo'.