Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / DebugUnitOfWorkListener.php
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
20 namespace Doctrine\ORM\Tools;
21
22 use Doctrine\ORM\Event\OnFlushEventArgs;
23 use Doctrine\ORM\Mapping\ClassMetadata;
24 use Doctrine\ORM\PersistentCollection;
25 use Doctrine\ORM\UnitOfWork;
26
27 /**
28  * Use this logger to dump the identity map during the onFlush event. This is useful for debugging
29  * weird UnitOfWork behavior with complex operations.
30  */
31 class DebugUnitOfWorkListener
32 {
33     private $file;
34     private $context;
35
36     /**
37      * Pass a stream and contet information for the debugging session.
38      *
39      * The stream can be php://output to print to the screen.
40      *
41      * @param string $file
42      * @param string $context
43      */
44     public function __construct($file = 'php://output', $context = '')
45     {
46         $this->file = $file;
47         $this->context = $context;
48     }
49
50     public function onFlush(OnFlushEventArgs $args)
51     {
52         $this->dumpIdentityMap($args->getEntityManager());
53     }
54
55     /**
56      * Dump the contents of the identity map into a stream.
57      *
58      * @param EntityManager $em
59      * @return void
60      */
61     public function dumpIdentityMap(EntityManager $em)
62     {
63         $uow = $em->getUnitOfWork();
64         $identityMap = $uow->getIdentityMap();
65
66         $fh = fopen($this->file, "x+");
67         if (count($identityMap) == 0) {
68             fwrite($fh, "Flush Operation [".$this->context."] - Empty identity map.\n");
69             return;
70         }
71
72         fwrite($fh, "Flush Operation [".$this->context."] - Dumping identity map:\n");
73         foreach ($identityMap as $className => $map) {
74             fwrite($fh, "Class: ". $className . "\n");
75             foreach ($map as $entity) {
76                 fwrite($fh, " Entity: " . $this->getIdString($entity, $uow) . " " . spl_object_hash($entity)."\n");
77                 fwrite($fh, "  Associations:\n");
78
79                 $cm = $em->getClassMetadata($className);
80                 foreach ($cm->associationMappings as $field => $assoc) {
81                     fwrite($fh, "   " . $field . " ");
82                     $value = $cm->reflFields[$field]->getValue($entity);
83
84                     if ($assoc['type'] & ClassMetadata::TO_ONE) {
85                         if ($value === null) {
86                             fwrite($fh, " NULL\n");
87                         } else {
88                             if ($value instanceof Proxy && !$value->__isInitialized__) {
89                                 fwrite($fh, "[PROXY] ");
90                             }
91
92                             fwrite($fh, $this->getIdString($value, $uow) . " " . spl_object_hash($value) . "\n");
93                         }
94                     } else {
95                         $initialized = !($value instanceof PersistentCollection) || $value->isInitialized();
96                         if ($value === null) {
97                             fwrite($fh, " NULL\n");
98                         } else if ($initialized) {
99                             fwrite($fh, "[INITIALIZED] " . $this->getType($value). " " . count($value) . " elements\n");
100                             foreach ($value as $obj) {
101                                 fwrite($fh, "    " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
102                             }
103                         } else {
104                             fwrite($fh, "[PROXY] " . $this->getType($value) . " unknown element size\n");
105                             foreach ($value->unwrap() as $obj) {
106                                 fwrite($fh, "    " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
107                             }
108                         }
109                     }
110                 }
111             }
112         }
113         fclose($fh);
114     }
115
116     private function getType($var)
117     {
118         if (is_object($var)) {
119             $refl = new \ReflectionObject($var);
120             return $refl->getShortname();
121         } else {
122             return gettype($var);
123         }
124     }
125
126     private function getIdString($entity, $uow)
127     {
128         if ($uow->isInIdentityMap($entity)) {
129             $ids = $uow->getEntityIdentifier($entity);
130             $idstring = "";
131             foreach ($ids as $k => $v) {
132                 $idstring .= $k."=".$v;
133             }
134         } else {
135             $idstring = "NEWOBJECT ";
136         }
137
138         $state = $uow->getEntityState($entity);
139         if ($state == UnitOfWork::STATE_NEW) {
140             $idstring .= " [NEW]";
141         } else if ($state == UnitOfWork::STATE_REMOVED) {
142             $idstring .= " [REMOVED]";
143         } else if ($state == UnitOfWork::STATE_MANAGED) {
144             $idstring .= " [MANAGED]";
145         } else if ($state == UnitOfwork::STATE_DETACHED) {
146             $idstring .= " [DETACHED]";
147         }
148
149         return $idstring;
150     }
151 }