1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/manager package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\Manager\Discovery\Binding; |
13
|
|
|
|
14
|
|
|
use LogicException; |
15
|
|
|
use Puli\Discovery\Api\Binding\Binding; |
16
|
|
|
use Puli\Discovery\Api\EditableDiscovery; |
17
|
|
|
use Puli\Manager\Api\Discovery\BindingDescriptor; |
18
|
|
|
use Puli\Manager\Transaction\AtomicOperation; |
19
|
|
|
use Rhumsaa\Uuid\Uuid; |
20
|
|
|
use Webmozart\Expression\Expr; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Synchronizes a binding descriptor with the discovery. |
24
|
|
|
* |
25
|
|
|
* The method {@link takeSnapshot()} must be called before executing the |
26
|
|
|
* operation. This method will record whether the binding descriptor is currently |
27
|
|
|
* enabled (i.e. loaded in the discovery). |
28
|
|
|
* |
29
|
|
|
* Once the operation is executed, another snapshot is taken. If the snapshots |
30
|
|
|
* differ, the binding descriptor is then either added to or removed from the |
31
|
|
|
* discovery, depending on the outcome. |
32
|
|
|
* |
33
|
|
|
* @since 1.0 |
34
|
|
|
* |
35
|
|
|
* @author Bernhard Schussek <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class SyncBinding implements AtomicOperation |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var Binding |
41
|
|
|
*/ |
42
|
|
|
private $binding; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var EditableDiscovery |
46
|
|
|
*/ |
47
|
|
|
private $discovery; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var BindingDescriptor |
51
|
|
|
*/ |
52
|
|
|
private $enabledBindingBefore; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var BindingDescriptor |
56
|
|
|
*/ |
57
|
|
|
private $enabledBindingAfter; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var BindingDescriptorCollection |
61
|
|
|
*/ |
62
|
|
|
private $bindingDescriptors; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
private $snapshotTaken = false; |
68
|
|
|
|
69
|
6 |
|
public function __construct(Binding $binding, EditableDiscovery $discovery, BindingDescriptorCollection $bindingDescriptors) |
70
|
|
|
{ |
71
|
6 |
|
$this->binding = $binding; |
72
|
6 |
|
$this->discovery = $discovery; |
73
|
6 |
|
$this->bindingDescriptors = $bindingDescriptors; |
74
|
6 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Records whether the UUID is currently enabled. |
78
|
|
|
*/ |
79
|
6 |
|
public function takeSnapshot() |
80
|
|
|
{ |
81
|
6 |
|
$this->enabledBindingBefore = null; |
82
|
|
|
|
83
|
6 |
|
foreach ($this->bindingDescriptors->listByBinding($this->binding) as $bindingDescriptor) { |
84
|
3 |
|
if ($bindingDescriptor->isEnabled()) { |
85
|
|
|
// Clone so that rollback() works if the binding is unloaded |
86
|
3 |
|
$this->enabledBindingBefore = clone $bindingDescriptor; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
$this->snapshotTaken = true; |
91
|
6 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
5 |
|
public function execute() |
97
|
|
|
{ |
98
|
5 |
|
if (!$this->snapshotTaken) { |
99
|
|
|
throw new LogicException('takeSnapshot() was not called'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Remember for rollback() |
|
|
|
|
103
|
5 |
|
$this->enabledBindingAfter = null; |
104
|
|
|
|
105
|
5 |
|
foreach ($this->bindingDescriptors->listByBinding($this->binding) as $bindingDescriptor) { |
106
|
5 |
|
if ($bindingDescriptor->isEnabled()) { |
107
|
|
|
// Clone so that rollback() works if the binding is unloaded |
108
|
5 |
|
$this->enabledBindingAfter = clone $bindingDescriptor; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
$this->syncBinding($this->enabledBindingBefore, $this->enabledBindingAfter); |
|
|
|
|
113
|
5 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
2 |
|
public function rollback() |
119
|
|
|
{ |
120
|
2 |
|
$this->syncBinding($this->enabledBindingAfter, $this->enabledBindingBefore); |
121
|
2 |
|
} |
122
|
|
|
|
123
|
5 |
|
private function syncBinding(BindingDescriptor $enabledBefore = null, BindingDescriptor $enabledAfter = null) |
124
|
|
|
{ |
125
|
5 |
|
if (!$enabledBefore && $enabledAfter) { |
126
|
|
|
$this->discovery->addBinding($this->binding); |
127
|
5 |
|
} elseif ($enabledBefore && !$enabledAfter) { |
128
|
|
|
$this->discovery->removeBindings( |
129
|
|
|
$this->binding->getTypeName(), |
130
|
|
|
Expr::method('equals', $this->binding, Expr::same(true)) |
131
|
|
|
); |
132
|
|
|
// } elseif ($enabledBefore && $enabledAfter && !$enabledBefore->getBinding()->equals($enabledAfter->getBinding())) { |
|
|
|
|
133
|
|
|
// $this->discovery->removeBindings( |
134
|
|
|
// $enabledBefore->getTypeName(), |
135
|
|
|
// Expr::method('equals', $enabledBefore->getBinding(), Expr::same(true)) |
136
|
|
|
// ); |
137
|
|
|
// $this->discovery->addBinding($enabledAfter->getBinding()); |
138
|
|
|
} |
139
|
5 |
|
} |
140
|
|
|
} |
141
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.