-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[DDC-3300] Added resolve entities support in discrim. map #1130
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?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 Doctrine\ORM\Tools; | ||
|
||
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | ||
use Doctrine\ORM\Mapping\ClassMetadataInfo; | ||
|
||
/** | ||
* ResolveDiscriminatorMapListener | ||
* | ||
* Mechanism to overwrite interfaces or classes specified in discrimination map | ||
* | ||
* @author Benjamin Eberlei <[email protected]> | ||
* @since 2.2 | ||
*/ | ||
class ResolveDiscriminatorMapListener | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $resolveTargetEntities = array(); | ||
|
||
/** | ||
* Adds a target-entity class name to resolve to a new class name. | ||
* | ||
* @param string $originalEntity | ||
* @param string $newEntity | ||
* @param array $mapping | ||
* | ||
* @return void | ||
*/ | ||
public function addResolveTargetEntity($originalEntity, $newEntity, array $mapping) | ||
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. Please move these to a constructor instead 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. Sorry, what do you mean? This method can be called many times. This class can be properly refactored, but... what can a constructor be useful for? 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. @mmoreram avoiding mutability at all costs: reduces bugs and makes code simpler :-) 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. @Ocramius you're right, thanks :) How about now? |
||
{ | ||
$mapping['targetEntity'] = ltrim($newEntity, "\\"); | ||
$this->resolveTargetEntities[ltrim($originalEntity, "\\")] = $mapping; | ||
} | ||
|
||
/** | ||
* Processes event and resolves new target entity names. | ||
* | ||
* @param LoadClassMetadataEventArgs $args | ||
* | ||
* @return void | ||
*/ | ||
public function loadClassMetadata(LoadClassMetadataEventArgs $args) | ||
{ | ||
$classMetadata = $args->getClassMetadata(); | ||
|
||
if (!empty($classMetadata->discriminatorMap)) { | ||
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 happens if the map is still empty after 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. @mmoreram can you clarify on this point? Is it covered by tests? |
||
|
||
$this->remapDiscriminatorMap($classMetadata); | ||
} | ||
} | ||
|
||
/** | ||
* Replaces all Interfaces in discriminator map | ||
* | ||
* @param \Doctrine\ORM\Mapping\ClassMetadataInfo $classMetadata | ||
*/ | ||
private function remapDiscriminatorMap(ClassMetadataInfo $classMetadata) | ||
{ | ||
foreach ($classMetadata->discriminatorMap as $name => $interface) { | ||
|
||
if (isset($this->resolveTargetEntities[$interface])) { | ||
|
||
$classMetadata->discriminatorMap[$name] = $this->resolveTargetEntities[$interface]['targetEntity']; | ||
} | ||
} | ||
} | ||
} |
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 should also be covered in
ClassMetadataTest