/** * @param string $name * @param \Closure $callback * @return object|null */ public function service($name, \Closure $callback = null) { if ($callback instanceof \Closure) { $this->serviceHolder->register($name, $callback, [$this]); return null; } else { return $this->serviceHolder->get($name); } }
public function testRegister() { $this->instance->register('test', function () { return new \stdClass(); }); $this->instance->register('test', function () { return new \stdClass(); }, ['asdas', 'asd']); try { $this->instance->register('asdas', 1); $this->fail(); } catch (\ErrorException $e) { } // return int $this->instance->register('test', function () { return 1; }); try { $this->instance->get('test'); $this->fail(); } catch (ServiceHasToBeAnObjectException $e) { } // return nothing $this->instance->register('test', function () { }); try { $this->instance->get('test'); $this->fail(); } catch (ServiceHasToBeAnObjectException $e) { } try { $this->instance->register('test', function () { }, 1); $this->fail(); } catch (\InvalidArgumentException $e) { } }