Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for AnnotationBuilder. #233

Merged
merged 14 commits into from
Jun 11, 2013
127 changes: 97 additions & 30 deletions src/DoctrineORMModule/Form/Annotation/AnnotationBuilder.php
Original file line number Diff line number Diff line change
@@ -1,66 +1,133 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace DoctrineORMModule\Form\Annotation;

use Doctrine\ORM\EntityManager;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\Code\Annotation\AnnotationManager;
use Zend\Code\Annotation\Parser\DoctrineAnnotationParser;
use Zend\EventManager\EventManagerInterface;
use Zend\Form\Annotation\AnnotationBuilder as ZendAnnotationBuilder;

/**
* @author Kyle Spraggs <[email protected]>
*/
class AnnotationBuilder extends ZendAnnotationBuilder
{
const EVENT_CONFIGURE_FIELD = 'configureField';
const EVENT_CONFIGURE_ASSOCIATION = 'configureAssociation';
const EVENT_EXCLUDE_FIELD = 'excludeField';
const EVENT_EXCLUDE_ASSOCIATION = 'excludeAssociation';

/**
* @var \Doctrine\ORM\EntityManager
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $entityManager;
protected $objectManager;

/**
* Constructor. Ensures EntityManager is present.
* Constructor. Ensures ObjectManager is present.
*
* @param \Doctrine\ORM\EntityManager $entityManager
* @param \Doctrine\Common\Persistence\ObjectManager $objectManager
*/
public function __construct(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* {@inheritDoc}
*/
public function __construct(EntityManager $entityManager)
public function setEventManager(EventManagerInterface $events)
{
$this->entityManager = $entityManager;
parent::setEventManager($events);

$this->getEventManager()->attach(new ElementAnnotationsListener($this->objectManager));

return $this;
}

/**
* Set annotation manager to use when building form from annotations
* Overrides the base getFormSpecification() to additionally iterate through each
* field/association in the metadata and trigger the associated event.
*
* This allows building of a form from metadata instead of requiring annotations.
* Annotations are still allowed through the ElementAnnotationsListener.
*
* @param AnnotationManager $annotationManager
* @return AnnotationBuilder
* {@inheritDoc}
*/
public function setAnnotationManager(AnnotationManager $annotationManager)
public function getFormSpecification($entity)
{
parent::setAnnotationManager($annotationManager);
$formSpec = parent::getFormSpecification($entity);
$metadata = $this->objectManager->getClassMetadata(get_class($entity));

$parser = new DoctrineAnnotationParser($this->entityManager);
$inputSpec = $formSpec['input_filter'];
foreach ($formSpec['elements'] as $key => $elementSpec) {
$name = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null;

$parser->registerAnnotation('Doctrine\ORM\Mapping\Column');
$parser->registerAnnotation('Doctrine\ORM\Mapping\GeneratedValue');
$parser->registerAnnotation('Doctrine\ORM\Mapping\OneToMany');
$parser->registerAnnotation('Doctrine\ORM\Mapping\OneToOne');
$parser->registerAnnotation('Doctrine\ORM\Mapping\ManyToMany');
$parser->registerAnnotation('Doctrine\ORM\Mapping\ManyToOne');
if (!$name) {
continue;
}

$this->annotationManager->attach($parser);
$params = array(
'metadata' => $metadata,
'name' => $name,
'elementSpec' => $elementSpec,
'inputSpec' => isset($inputSpec[$name]) ? $inputSpec[$name] : new \ArrayObject()
);

return $this;
if ($this->checkForExcludeElementFromMetadata($metadata, $name)) {
unset($formSpec['elements'][$key]);
unset($inputSpec[$name]);
continue;
}

if ($metadata->hasField($name)) {
$this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params);
} elseif ($metadata->hasAssociation($name)) {
$this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params);
}
}

return $formSpec;
}

/**
* Set event manager instance
*
* @param EventManagerInterface $events
* @return self
* @param ClassMetadata $metadata
* @param $name
* @return bool
*/
public function setEventManager(EventManagerInterface $events)
protected function checkForExcludeElementFromMetadata(ClassMetadata $metadata, $name)
{
parent::setEventManager($events);
$params = array('metadata' => $metadata, 'name' => $name);
$test = function($r) { return (true === $r); };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does PSR-2 allow this? Also, don't shorten values this much. Parenthetical is also not needed around true === $r

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a direct copy/paste from the excludeElement() from the root AnnotationBuilder.

$result = false;

$this->getEventManager()->attach(new ElementAnnotationsListener($this->entityManager));
if ($metadata->hasField($name)) {
$result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_FIELD, $this, $params, $test);
} elseif ($metadata->hasAssociation($name)) {
$result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_ASSOCIATION, $this, $params, $test);
}

return $this;
if ($result) {
$result = (bool) $result->last();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the case for this? Why not returning always a boolean?

}

return $result;
}
}
Loading