/** * Set a service * * @param string $name * @param mixed $call * @param mixed $params * @return \Pop\Project\Project */ public function setService($name, $call, $params = null) { $this->services->set($name, $call, $params); return $this; }
<?php require_once '../../bootstrap.php'; use Pop\Service\Locator; class Foo { public function bar($val) { return new \Pop\Config(array('test' => $val)); } public static function baz($val) { return new \Pop\Config(array('test' => $val)); } } try { $locator = new Locator(); // Load the services manually via the setter $locator->set('config1', 'Foo->bar', array(123))->set('config2', 'Foo::baz', array(456)); // Get a service print_r($locator->get('config1')); print_r($locator->get('config2')); print_r($locator); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }
public function testClassWithParams() { $l = new Locator(); $l->set('config1', 'PopTest\\Service\\Foo', array(123))->set('config2', 'PopTest\\Service\\Foo->bar', array(456))->set('config3', 'PopTest\\Service\\Foo::baz', array(789)); $c1 = $l->get('config1'); $c2 = $l->get('config2'); $c3 = $l->get('config3'); $this->assertInstanceOf('PopTest\\Service\\Foo', $c1); $this->assertInstanceOf('Pop\\Config', $c2); $this->assertInstanceOf('Pop\\Config', $c3); }
<?php require_once '../../bootstrap.php'; use Pop\Service\Locator; try { $locator = new Locator(); // Load the services manually via the setter $locator->set('config', 'Pop\\Config', array(array('test' => 123)))->set('color', 'Pop\\Color\\Color', array(new \Pop\Color\Rgb(255, 0, 0))); // Get a service print_r($locator->get('config')); // Remove a service $locator->remove('color'); print_r($locator); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }