<?php 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');
public function setService2($service2); public function getService2(); } class Service2 { public function foo() { } } class Service2Initializer implements InitializerInterface { public function initialize($intance, ServiceLocatorInterface $locator) { if ($intance instanceof ServiceTwoAwareInterface) { $intance->setService2($locator->get('service2')); } } } // setup ServiceLocator $serviceLocator = new ServiceLocator(); // Hold with trait $serviceLocator->addInitializer(new Service2Initializer()); // Hold with trait $serviceLocator->setInvokable('service1', '\\Example\\ServiceCanHoldService2'); $serviceLocator->setInvokable('service2', '\\Example\\Service2'); // get Services $service1 = $serviceLocator->get('service1'); if ($service1->getService2() !== $serviceLocator->get('service2')) { die('fail'); } echo "done";
class FactoryFoo implements FactoryInterface { public function createService(ServiceLocatorInterface $locator, $cName, $rName) { return new Service(); } } class Service { } function FactoryFunction(ServiceLocatorInterface $locator, $cName, $rName) { return new Service(); } // setup ServiceLocator $serviceLocator = new ServiceLocator(); // factory as closure $serviceLocator->setFactory('service1', function (ServiceLocatorInterface $locator) { return Service(); }); // factory as closure $serviceLocator->setFactory('service2', 'FactoryFunction'); // factory as class $serviceLocator->setFactory('service3', '\\Example\\FactoryFoo'); // factory as instance $serviceLocator->setFactory('service4', new FactoryFoo()); // get a service $service1 = $serviceLocator->get('service1'); $service2 = $serviceLocator->get('service2'); $service3 = $serviceLocator->get('service3'); $service4 = $serviceLocator->get('service4');
} // php 5.3 class ServiceCanHoldServiceLocator3 { protected $serviceLocator; public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this->serviceLocator = $serviceLocator; } public function getServiceLocator() { return $this->serviceLocator; } } // setup ServiceLocator $serviceLocator = new ServiceLocator(); // Hold with trait $serviceLocator->setFactory('service1', function (ServiceLocatorInterface $locator) { return new ServiceCanHoldServiceLocator1(); }); // Hold with interface $serviceLocator->setFactory('service2', function (ServiceLocatorInterface $locator) { return new ServiceCanHoldServiceLocator1(); }); // Hold with custom $serviceLocator->setFactory('service3', function (ServiceLocatorInterface $locator) { $service = new ServiceCanHoldServiceLocator3(); $service->setServiceLocator($locator); return $service; }); $serviceLocator->get('service1');