承接 stuartwakefield/platter 相关项目开发

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

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

stuartwakefield/platter

Composer 安装命令:

composer require stuartwakefield/platter

包简介

Light-touch dependency injection

README 文档

README

Light-touch dependency injection.

Build status

Usage

To install add to your composer.json:

"require": {
	"stuartwakefield/platter": "0.1.0"
}

After you have run composer install you will be able to use the Platter DI manager.

Create a new Platter instance with your definitions:

$factory = new Platter(array(
	'dbuser' => 'admin',
	'dbpassword' => 'password',
	'dbname' => 'test',
	'dbhost' => '0.0.0.0',
	'connectionstring' => function ($container) {
		return "mysql:dbname={$container->get('dbname')};host={$container->get('dbhost')}";
	},
	'pdo' => function ($container) {
		return new PDO(
			$container->get('connectionstring'),
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	},
	'repository' => function ($container) {
		return new Repository($container->get('pdo'));
	}
));

To retrieve an object from your container, just use the get method:

$repository = $factory->get('repository');

You can also link Platter containers together, one container is considered the child and the other the parent.

$parent = new Platter(array(
	'dbuser' => 'xyz',
	'dbpassword' => 'abc'
));

$factory = new Platter(array(
	'DataSource' => function ($container) {
		return new DataSource(
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	}
), $parent);

$ds = $factory->get('DataSource');

When resolving dependencies if the child does not have a definition for the dependency the dependency will be obtained from the parent.

Note: The direction in which dependencies are resolved is unidirectional, a parent will not check it's children for a dependency. Therefore, definitions attached to a container can only access other definitions within that container and it's parents but not it's children.

The following will not work:

$parent = new Platter(array(
	'DataSource' => function ($container) {
		return new DataSource(
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	}
));

$factory = new Platter(array(
	'dbuser' => 'xyz',
	'dbpassword' => 'abc'
), $parent);

$ds = $factory->get('DataSource'); // "Identifier 'dbuser' is not defined"

This may cause confusion if not understood:

$parent = new Platter(array(
	'name' => 'Joe',
	'example' => function ($container) {
		return $container->get('name');
	}
));

$factory = new Platter(array(
	'name' => 'Michael'
), $parent);

echo $factory->get('name'); // "Michael"
echo $factory->get('example'); // "Joe"

The example definition does not have access to the name definition from the child and so the name definition from the parent container is returned instead.

Builder interface

Platter also comes with a builder interface to register items using a friendly and expressive builder interface. Once built the platter is immutable.

$builder = new Platter\Builder;
$parent = $builder
	->register('dbuser', 'xyz')
	->register('dbpassword', 'abc')
	->build();

$builder = new Platter\Builder;
$factory = $builder
	->register('DataSource', function ($container) {
		return new DataSource(
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	})
	->connect($parent)
	->build();

To show all available items served by the platter:

$factory->available(); // array('DataSource', 'dbpassword', 'dbuser');

To show the items that the platter instance itself defines:

$factory->defined(); // array('DataSource');

To create a singleton use the Singleton definition object:

$builder
	->register('DataSource', new Platter\Definition\Singleton(function ($container) {
		return new DataSource(
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	}))
	->build();

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-08-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固