Completed
Push — master ( e72bde...c9688b )
by Filip
02:19
created

renderEntityManagerException()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 23
nc 18
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Doctrine\Diagnostics;
12
13
use Doctrine;
14
use Kdyby;
15
use Tracy\Debugger;
16
use Tracy\Dumper;
17
use Tracy\Helpers;
18
19
20
21
/**
22
 * @author Filip Procházka <[email protected]>
23
 */
24
class EntityManagerUnitOfWorkSnapshotPanel
25
{
26
27
	/**
28
	 * @var \Doctrine\ORM\EntityManager
29
	 */
30
	private $em;
31
32
	/**
33
	 * @var \Throwable[]
34
	 */
35
	private $whitelistExceptions = [];
36
37
	/**
38
	 * @var \Doctrine\ORM\UnitOfWork|NULL
39
	 */
40
	private $unitOfWorkSnapshot;
41
42
	public function markExceptionOwner(Doctrine\ORM\EntityManager $em, $exception)
43
	{
44
		if ($this->em !== $em) {
45
			return;
46
		}
47
48
		$this->whitelistExceptions[] = $exception;
49
	}
50
51
	public function snapshotUnitOfWork(Doctrine\ORM\EntityManager $em)
52
	{
53
		if ($this->em !== $em) {
54
			return;
55
		}
56
57
		$this->unitOfWorkSnapshot = clone $em->getUnitOfWork();
58
	}
59
60
	/**
61
	 * @param \Exception|\Throwable $e
62
	 * @return array|NULL
63
	 */
64
	public function renderEntityManagerException($e)
65
	{
66
		if (!in_array($e, $this->whitelistExceptions, TRUE)) {
67
			return NULL; // ignore
68
		}
69
70
		if (strpos(get_class($e), 'Doctrine\\ORM\\') !== FALSE && Helpers::findTrace($e->getTrace(), 'Doctrine\ORM\EntityManager::flush')) {
71
			$UoW = $this->unitOfWorkSnapshot ?: $this->em->getUnitOfWork();
72
73
			$panel = '<div class="inner"><p><b>IdentityMap</b></p>' .
74
				Dumper::toHtml($UoW->getIdentityMap(), [Dumper::COLLAPSE => TRUE]) .
75
				'</div>';
76
77
			if ($scheduled = $UoW->getScheduledEntityInsertions()) {
78
				$panel .= '<div class="inner"><p><b>Scheduled entity insertions</b></p>' .
79
					Dumper::toHtml($scheduled, [Dumper::COLLAPSE => TRUE]) .
80
					'</div>';
81
			}
82
83
			if ($scheduled = $UoW->getScheduledEntityDeletions()) {
84
				$panel .= '<div class="inner"><p><b>Scheduled entity deletions</b></p>' .
85
					Dumper::toHtml($scheduled, [Dumper::COLLAPSE => TRUE]) .
86
					'</div>';
87
			}
88
89
			if ($scheduled = $UoW->getScheduledEntityUpdates()) {
90
				$panel .= '<div class="inner"><p><b>Scheduled entity updates</b></p>' .
91
					Dumper::toHtml($scheduled, [Dumper::COLLAPSE => TRUE]) .
92
					'</div>';
93
			}
94
95
			return [
96
				'tab' => 'Doctrine\\ORM\\UnitOfWork',
97
				'panel' => $panel,
98
			];
99
		}
100
	}
101
102
	/**
103
	 * @param Doctrine\ORM\EntityManager $em
104
	 * @return Panel
105
	 */
106
	public function bindEntityManager(Doctrine\ORM\EntityManager $em)
107
	{
108
		if ($this->em !== NULL) {
109
			throw new Kdyby\Doctrine\InvalidStateException(sprintf('%s is already bound to an entity manager.', __CLASS__));
110
		}
111
112
		$this->em = $em;
113
		if ($this->em instanceof Kdyby\Doctrine\EntityManager) {
114
			$this->em->bindTracyPanel($this);
115
		}
116
117
		Debugger::getBlueScreen()->addPanel([$this, 'renderEntityManagerException']);
118
	}
119
120
}
121