定制 andyleap/popple 二次开发

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

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

andyleap/popple

Composer 安装命令:

composer require andyleap/popple

包简介

Simple Dependency Injection container inspired by Pimple

README 文档

README

Develop

Build Status

Simple DI container for PHP inspired by Pimple.

What does Popple offer over the simplicity of Pimple?

Quite simply, Popple tries to maintain the same level of simplicity, while offering additional functionality to make it easier to work with.

For instance, extending services with Pimple requires several steps and a little bit of work around, as you have to extend the service, and then share it.

With Popple, that's handled for you. Popple takes the viewpoint that everything is shared, so, first, you create the service.

$popple = new Popple();

$popple->Share('db', function($p)
{
  $db = new Popple();
	$db->Extend('Connect', function()
	{
		if(!isset($this['username']))
		{
			die('Username required!');
		}
		echo 'Connecting as ' . $this['username'];
	});
	return $db;
});

This creates a simple service of the main $popple instance. It's accessed and used rather simply

$popple['db']['username'] = 'guest';
$popple['db']->Connect();

This also showcases another feature of Popple, it's ability to quickly extend out and form a service on it's own, without requiring formalized classes, while still allowing easy transitioning to a class structure. Hence, the above is roughly equivalent to

public class DB extends Popple
{
  public function Connect()
  {
    if(!isset($this['username']))
  	{
			die('Username required!');
		}
		echo 'Connecting as ' . $this['username'];
  }
}

$popple->Share('db', function($p)
{
  return new DB();
});

Then, all you do is Mutate the service to alter it

$popple->Mutate('db', function($db)
{
  $oldconnect = $db->Connect;
	$db->Extend('Connect', function() use ($oldconnect)
	{
		$oldconnect();
		if(!isset($this['password']))
		{
			die('Password required!');
		}
		echo ' with password ' . $this['password'];
	});
	return $db;
});

Then, all you have to do to use the new Connect method is

$popple['db']['username'] = 'admin';
$popple['db']['password'] = 'password';
$popple['db']->Connect();

Note that the Mutation can occur at any point. The service may have already been instantiated, it may be registered and the mutation queued to occur on instantiation, or the service may not even be registered yet. No matter what, it will work out. This is useful for services that mutually alter each other. Just register the Mutate on the other, and let it all work out. You can even register Mutates for services that never get added without any ill effects.

The final additional feature of Popple is it's ability to be "Popped", hence the naming.

Popping a Popple causes all of it's shared services to be instantiated, and this instantiation walks down the tree, in turn popping any additional Popples or anything that implements Poppable.

This seems rather backwards, as part of the advantage of a system like this is to delay or prevent instantiation as much as possible, but some use cases have slack time when loading could occur, and loading while processing requests later could be minimized by preloading all services

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPLv2
  • 更新时间: 2013-08-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固