Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Added fluent interface for class/method creation
Browse files Browse the repository at this point in the history
- BuilderDefinition now adds "createClass()" method, returning a
  Builder\PhpClass object
- Builder\PhpClass now adds "createInjectionMethod()" method, returning a
  Builder\InjectionMethod object
  • Loading branch information
weierophinney committed Jul 1, 2011
1 parent 5784352 commit 0df2db0
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 4 deletions.
54 changes: 51 additions & 3 deletions src/Definition/Builder/PhpClass.php
Expand Up @@ -4,9 +4,10 @@

class PhpClass
{
protected $name = null;
protected $instantiator = '__construct';
protected $injectionMethods = array();
protected $defaultMethodBuilder = 'Zend\Di\Definition\Builder\InjectionMethod';
protected $name = null;
protected $instantiator = '__construct';
protected $injectionMethods = array();

public function setName($name)
{
Expand Down Expand Up @@ -46,6 +47,53 @@ public function addInjectionMethod(InjectionMethod $injectionMethod)
$this->injectionMethods[] = $injectionMethod;
return $this;
}

/**
* Create and register an injection method
*
* Optionally takes the method name.
*
* This method may be used in lieu of addInjectionMethod() in
* order to provide a more fluent interface for building classes with
* injection methods.
*
* @param null|string $name
* @return InjectionMethod
*/
public function createInjectionMethod($name = null)
{
$builder = $this->defaultMethodBuilder;
$method = new $builder();
if (null !== $name) {
$method->setName($name);
}
$this->addInjectionMethod($method);
return $method;
}

/**
* Override which class will be used by {@link createInjectionMethod()}
*
* @param string $class
* @return PhpClass
*/
public function setMethodBuilder($class)
{
$this->defaultMethodBuilder = $class;
return $this;
}

/**
* Determine what class will be used by {@link createInjectionMethod()}
*
* Mainly to provide the ability to temporarily override the class used.
*
* @return string
*/
public function getMethodBuilder()
{
return $this->defaultMethodBuilder;
}

public function getInjectionMethods()
{
Expand Down
47 changes: 46 additions & 1 deletion src/Definition/BuilderDefinition.php
Expand Up @@ -7,7 +7,7 @@

class BuilderDefinition implements Definition
{

protected $defaultClassBuilder = 'Zend\Di\Definition\Builder\PhpClass';
protected $classes = array();

public function createClassesFromArray(array $builderData)
Expand Down Expand Up @@ -49,6 +49,51 @@ public function addClass(Builder\PhpClass $phpClass)
$this->classes[] = $phpClass;
return $this;
}

/**
* Create a class builder object using default class builder class
*
* This method is a factory that can be used in place of addClass().
*
* @param null|string $name Optional name of class to assign
* @return Builder\PhpClass
*/
public function createClass($name = null)
{
$builderClass = $this->defaultClassBuilder;
$class = new $builderClass();
if (null !== $name) {
$class->setName($name);
}

$this->addClass($class);
return $class;
}

/**
* Set the class to use with {@link createClass()}
*
* @param string $class
* @return BuilderDefinition
*/
public function setClassBuilder($class)
{
$this->defaultClassBuilder = $class;
return $this;
}

/**
* Get the class used for {@link createClass()}
*
* This is primarily to allow developers to temporarily override
* the builder strategy.
*
* @return string
*/
public function getClassBuilder()
{
return $this->defaultClassBuilder;
}

public function getClasses()
{
Expand Down
45 changes: 45 additions & 0 deletions test/Definition/BuilderDefinitionTest.php
Expand Up @@ -71,5 +71,50 @@ public function testBuilderCanBuildFromArray()
);

}

public function testCanCreateClassFromFluentInterface()
{
$builder = new BuilderDefinition();
$class = $builder->createClass('Foo');

$this->assertTrue($builder->hasClass('Foo'));
}

public function testCanCreateInjectionMethodsAndPopulateFromFluentInterface()
{
$builder = new BuilderDefinition();
$foo = $builder->createClass('Foo');
$foo->setName('Foo');
$foo->createInjectionMethod('setBar')
->addParameter('bar', 'Bar');
$foo->createInjectionMethod('setConfig')
->addParameter('config', null);

$this->assertTrue($builder->hasClass('Foo'));
$this->assertTrue($builder->hasInjectionMethod('Foo', 'setBar'));
$this->assertTrue($builder->hasInjectionMethod('Foo', 'setConfig'));

$this->assertEquals(array('bar' => 'Bar'), $builder->getInjectionMethodParameters('Foo', 'setBar'));
$this->assertEquals(array('config' => null), $builder->getInjectionMethodParameters('Foo', 'setConfig'));
}

public function testBuilderCanSpecifyClassToUseWithCreateClass()
{
$builder = new BuilderDefinition();
$this->assertEquals('Zend\Di\Definition\Builder\PhpClass', $builder->getClassBuilder());

$builder->setClassBuilder('Foo');
$this->assertEquals('Foo', $builder->getClassBuilder());
}

public function testClassBuilderCanSpecifyClassToUseWhenCreatingInjectionMethods()
{
$builder = new BuilderDefinition();
$class = $builder->createClass('Foo');

$this->assertEquals('Zend\Di\Definition\Builder\InjectionMethod', $class->getMethodBuilder());

$class->setMethodBuilder('Foo');
$this->assertEquals('Foo', $class->getMethodBuilder());
}
}

0 comments on commit 0df2db0

Please sign in to comment.