-
-
Notifications
You must be signed in to change notification settings - Fork 229
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
721e3cf
Updated for AnnotationBuilder.
07574ae
Added missing callback for handleToOneAnnotation.
a644c60
Additional metadata updates.
dcaa32e
Updated with more robust tests.
1a1ff78
Added test entities.
55221f9
Updated for CS.
c2e9fda
Completely removed annotations in favor of metadata.
3994aae
Fixed a notice with inputSpec.
2fa2e7d
Updated AnnotationBuilder to use metadata *only*
56f0fbb
First round of updates per @ocramius.
e6ecfa2
Second round of ocra-dates.
2246e15
Added missing header/author tags.
2aa3034
Fixing @Author => @author docblock.
bcfdd4b
Updated for CS.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 97 additions & 30 deletions
127
src/DoctrineORMModule/Form/Annotation/AnnotationBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); }; | ||
$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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after
function
There was a problem hiding this comment.
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.