Ergo\Registry::factory PHP Method

factory() public method

Registers a {@link Factory} or closure to be invoked on the first lookup
public factory ( $key, $factory )
    function factory($key, $factory)
    {
        if ($factory instanceof Factory) {
            $this->_registry[$key] = function () use($factory) {
                return $factory->create();
            };
        } else {
            if (is_callable($factory)) {
                $this->_registry[$key] = $factory;
            } else {
                throw new RegistryException("Parameter must be a Factory or Closure");
            }
        }
        return $this;
    }

Usage Example

Example #1
0
 public function testRegisteringAClosureAsAFactory()
 {
     $registry = new Registry();
     $registry->factory('my_key', function () {
         return (object) array('test' => 'blargh');
     });
     $this->assertTrue($registry->isRegistered('my_key'));
     $this->assertEquals($registry->lookup('my_key')->test, 'blargh');
 }