mpoiriert / binder-bundle
可重用库
dev-master
2014-03-26 11:57 UTC
Requires
- php: >=5.3
- mpoiriert/binder: *
- mpoiriert/nucleus-bundle: *
- symfony/framework-bundle: ~2.4
Requires (Dev)
- phpunit/phpunit: 3.7.*
- symfony/symfony: ~2.4
This package is auto-updated.
Last update: 2024-09-09 14:05:17 UTC
README
一个用于使用 binder 库的 Symfony 扩展包。
主要目的是隐藏对用户(开发者)的会话使用。
要在您的应用程序中使用它,您必须注册 2 个扩展包,因为存在对 nucleus-bundle 的依赖。
<?php
// in AppKernel::registerBundles()
$bundles = array(
// ...
new Nucleus\Bundle\CoreBundle\NucleusCoreBundle(),
new Nucleus\Bundle\BinderBundle\NucleusBinderBundle(),
// ...
);
系统使用 kernel.terminate 事件来工作。不要忘记在您的内核上使用 Symfony/Component/HttpKernel/TerminableInterface。
在任何一个服务中,您都可以在属性上放置一个注解,并且这个属性将被自动保存到会话中,并在下一次调用时恢复。
<?php
class TheService
{
/**
* @\Nucleus\Binder\Bounding
*/
protected $variable = 'default';
public function getVariable()
{
return $this->variable;
}
public function setVariable($value)
{
$this->variable = $value;
}
//...
}
这就完了!
如果 Binder 服务中没有可用变量的正确变量,将使用属性的默认值。
通过调用 TheService::setVariable(),值将改变,并在内核.terminate 的请求结束时。
如果您需要在 "AnotherService" 中访问此变量的值,您需要在 "AnotherService" 中注入 "TheService" 并调用 TheService::getVariable()。具有 Bounded 属性的服务负责所有对其的访问。
这种方法也有助于单元测试,因为您不再需要使用会话。它还强制在您的服务中实现关注点分离,因为没有其他服务可以通过直接操作它来更改会话中的值,因为其内部工作被隐藏了。