PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/DebugUnitOfWorkListener.php

https://gitlab.com/mario.uriarte/doctrine2.5-tutorial
PHP | 184 lines | 102 code | 24 blank | 58 comment | 19 complexity | 0ddddc895254b1e062182c02aa0cc9dc MD5 | raw file
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Tools;
  20. use Doctrine\Common\Persistence\Proxy;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Doctrine\ORM\Event\OnFlushEventArgs;
  23. use Doctrine\ORM\Mapping\ClassMetadata;
  24. use Doctrine\ORM\PersistentCollection;
  25. use Doctrine\ORM\UnitOfWork;
  26. /**
  27. * Use this logger to dump the identity map during the onFlush event. This is useful for debugging
  28. * weird UnitOfWork behavior with complex operations.
  29. */
  30. class DebugUnitOfWorkListener
  31. {
  32. /**
  33. * @var string
  34. */
  35. private $file;
  36. /**
  37. * @var string
  38. */
  39. private $context;
  40. /**
  41. * Pass a stream and context information for the debugging session.
  42. *
  43. * The stream can be php://output to print to the screen.
  44. *
  45. * @param string $file
  46. * @param string $context
  47. */
  48. public function __construct($file = 'php://output', $context = '')
  49. {
  50. $this->file = $file;
  51. $this->context = $context;
  52. }
  53. /**
  54. * @param \Doctrine\ORM\Event\OnFlushEventArgs $args
  55. *
  56. * @return void
  57. */
  58. public function onFlush(OnFlushEventArgs $args)
  59. {
  60. $this->dumpIdentityMap($args->getEntityManager());
  61. }
  62. /**
  63. * Dumps the contents of the identity map into a stream.
  64. *
  65. * @param EntityManagerInterface $em
  66. *
  67. * @return void
  68. */
  69. public function dumpIdentityMap(EntityManagerInterface $em)
  70. {
  71. $uow = $em->getUnitOfWork();
  72. $identityMap = $uow->getIdentityMap();
  73. $fh = fopen($this->file, "x+");
  74. if (count($identityMap) == 0) {
  75. fwrite($fh, "Flush Operation [".$this->context."] - Empty identity map.\n");
  76. return;
  77. }
  78. fwrite($fh, "Flush Operation [".$this->context."] - Dumping identity map:\n");
  79. foreach ($identityMap as $className => $map) {
  80. fwrite($fh, "Class: ". $className . "\n");
  81. foreach ($map as $entity) {
  82. fwrite($fh, " Entity: " . $this->getIdString($entity, $uow) . " " . spl_object_hash($entity)."\n");
  83. fwrite($fh, " Associations:\n");
  84. $cm = $em->getClassMetadata($className);
  85. foreach ($cm->associationMappings as $field => $assoc) {
  86. fwrite($fh, " " . $field . " ");
  87. $value = $cm->getFieldValue($entity, $field);
  88. if ($assoc['type'] & ClassMetadata::TO_ONE) {
  89. if ($value === null) {
  90. fwrite($fh, " NULL\n");
  91. } else {
  92. if ($value instanceof Proxy && !$value->__isInitialized()) {
  93. fwrite($fh, "[PROXY] ");
  94. }
  95. fwrite($fh, $this->getIdString($value, $uow) . " " . spl_object_hash($value) . "\n");
  96. }
  97. } else {
  98. $initialized = !($value instanceof PersistentCollection) || $value->isInitialized();
  99. if ($value === null) {
  100. fwrite($fh, " NULL\n");
  101. } elseif ($initialized) {
  102. fwrite($fh, "[INITIALIZED] " . $this->getType($value). " " . count($value) . " elements\n");
  103. foreach ($value as $obj) {
  104. fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
  105. }
  106. } else {
  107. fwrite($fh, "[PROXY] " . $this->getType($value) . " unknown element size\n");
  108. foreach ($value->unwrap() as $obj) {
  109. fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. fclose($fh);
  117. }
  118. /**
  119. * @param mixed $var
  120. *
  121. * @return string
  122. */
  123. private function getType($var)
  124. {
  125. if (is_object($var)) {
  126. $refl = new \ReflectionObject($var);
  127. return $refl->getShortname();
  128. }
  129. return gettype($var);
  130. }
  131. /**
  132. * @param object $entity
  133. * @param UnitOfWork $uow
  134. *
  135. * @return string
  136. */
  137. private function getIdString($entity, UnitOfWork $uow)
  138. {
  139. if ($uow->isInIdentityMap($entity)) {
  140. $ids = $uow->getEntityIdentifier($entity);
  141. $idstring = "";
  142. foreach ($ids as $k => $v) {
  143. $idstring .= $k."=".$v;
  144. }
  145. } else {
  146. $idstring = "NEWOBJECT ";
  147. }
  148. $state = $uow->getEntityState($entity);
  149. if ($state == UnitOfWork::STATE_NEW) {
  150. $idstring .= " [NEW]";
  151. } elseif ($state == UnitOfWork::STATE_REMOVED) {
  152. $idstring .= " [REMOVED]";
  153. } elseif ($state == UnitOfWork::STATE_MANAGED) {
  154. $idstring .= " [MANAGED]";
  155. } elseif ($state == UnitOfwork::STATE_DETACHED) {
  156. $idstring .= " [DETACHED]";
  157. }
  158. return $idstring;
  159. }
  160. }