Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Updated Zend/Form/Factory to allow for custom injection of factories …
Browse files Browse the repository at this point in the history
…when a fieldset is created and configured
  • Loading branch information
tklever committed Jun 14, 2013
1 parent b6fc1bc commit a1abd0c
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions library/Zend/Form/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ public function configureFieldset(FieldsetInterface $fieldset, $spec)
$this->prepareAndInjectFieldsets($spec['fieldsets'], $fieldset, __METHOD__);
}

$factory = (isset($spec['factory']) ? $spec['factory'] : $this);
$this->prepareAndInjectFactory($factory, $fieldset, __METHOD__);

return $fieldset;
}

Expand Down Expand Up @@ -450,6 +453,51 @@ protected function prepareAndInjectHydrator($hydratorOrName, FieldsetInterface $
$fieldset->setHydrator($hydrator);
}

/**
* Prepare and inject a named factory
*
* Takes a string indicating a factory class name (or a concrete instance), try first to instantiates the class
* by pulling it from service manager, and injects the factory instance into the fieldset.
*
* @param string|array|Factory $factoryOrName
* @param FieldsetInterface $fieldset
* @param string $method
* @return void
* @throws Exception\DomainException If $factoryOrName is not a string, does not resolve to a known class, or
* the class does not extend Form\Factory
*/
protected function prepareAndInjectFactory($factoryOrName, FieldsetInterface $fieldset, $method)
{
if ($factoryOrName instanceof Factory) {
$fieldset->setFormFactory($factoryOrName);
return;
}

if (is_array($factoryOrName)) {
if (!isset($factoryOrName['type'])) {
throw new Exception\DomainException(sprintf(
'%s expects array specification to have a type value',
$method
));
}
$factoryOrName = $factoryOrName['type'];
}

if (is_string($factoryOrName)) {
$factory = $this->getFactoryFromName($factoryOrName);
}

if (!$factory instanceof Factory) {
throw new Exception\DomainException(sprintf(
'%s expects a valid extention of Zend\Form\Factory; received "%s"',
$method,
$factoryOrName
));
}

$fieldset->setFormFactory($factory);
}

/**
* Prepare an input filter instance and inject in the provided form
*
Expand Down Expand Up @@ -559,4 +607,30 @@ protected function getHydratorFromName($hydratorName)
$hydrator = new $hydratorName;
return $hydrator;
}

/**
* Try to pull factory from service manager, or instantiates it from its name
*
* @param string $factoryName
* @return mixed
* @throws Exception\DomainException
*/
protected function getFactoryFromName($factoryName)
{
$services = $this->getFormElementManager()->getServiceLocator();

if ($services && $services->has($factoryName)) {
return $services->get($factoryName);
}

if (!class_exists($factoryName)) {
throw new Exception\DomainException(sprintf(
'Expects string factory name to be a valid class name; received "%s"',
$factoryName
));
}

$factory = new $factoryName;
return $factory;
}
}

0 comments on commit a1abd0c

Please sign in to comment.