slidesworker/servicelocator
Composer 安装命令:
composer require slidesworker/servicelocator
包简介
A basic ServiceLocator for PHP inspired by ZendFramework2
README 文档
README
How to use
simple use
You can simple register your service to this ServiceLocator Fore more example see this file
namespace Example; use SlidesWorker\ServiceLocator\ServiceLocator; class Service1 { } class Service2 { } // setup ServiceLocator $serviceLocator = new ServiceLocator(); $serviceLocator->set('service1', new Service1()); $serviceLocator->set('service2', new Service2()); $service1 = $serviceLocator->get('service1'); $service2 = $serviceLocator->get('service2');
with factory
The ServiceLocator can work with a some few factories. Classes that implements SlidesWorker\ServiceLocator\FactoryInterface and function or closure.
Fore more example see this file
namespace Example; use SlidesWorker\ServiceLocator\ServiceLocator; use SlidesWorker\ServiceLocator\ServiceLocatorInterface; class Service {} // setup ServiceLocator $serviceLocator = new ServiceLocator(); // factory as closure $serviceLocator->setFactory('service', function (ServiceLocatorInterface $locator) { return Service(); }); // get a service $service = $serviceLocator->get('service');
Initialzer and ServiceLocator
If you have the need that one objects must hold some other object Initializer is the right feature. In the case that ServiceLocator create the Object for you it run a Stack of few Initializer. You can add your own Initializer to this system, too.
A working example you find here
give service the ability to handle the ServiceLocator
ServiceLocator has the ability to add him self to a object. For this feature the object must be a instance of
SlidesWorker\ServiceLocator\ServiceLocatorAwareTrait or SlidesWorker\ServiceLocator\ServiceLocatorAwareInterface
For more example see this file
namespace Example; use SlidesWorker\ServiceLocator\ServiceLocator; use SlidesWorker\ServiceLocator\ServiceLocatorInterface; use SlidesWorker\ServiceLocator\ServiceLocatorAwareInterface; use SlidesWorker\ServiceLocator\ServiceLocatorAwareTrait; // only php 5.4 and higher class ServiceCanHoldServiceLocator1 { use ServiceLocatorAwareTrait; } // setup ServiceLocator $serviceLocator = new ServiceLocator(); // factory as closure $serviceLocator->setFactory('service', function (ServiceLocatorInterface $locator) { return new ServiceCanHoldServiceLocator1(); }); $serviceLocator->get('service');
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0
- 更新时间: 2014-03-03