|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\ContextStorageHandlerInterface; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\ContextProviderInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Takes care of storing / restoring all necessary migration execution context information, acting as a "multiplexer": |
|
10
|
|
|
* services which hold context information have to implement the ContextProviderInterface interface |
|
11
|
|
|
*/ |
|
12
|
|
|
class ContextHandler |
|
13
|
|
|
{ |
|
14
|
|
|
protected $storageHandler; |
|
15
|
|
|
/** @var ContextProviderInterface[] $providers */ |
|
16
|
|
|
protected $providers = array(); |
|
17
|
|
|
|
|
18
|
1 |
|
public function __construct(ContextStorageHandlerInterface $storageHandler) |
|
19
|
|
|
{ |
|
20
|
1 |
|
$this->storageHandler = $storageHandler; |
|
21
|
1 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param ContextProviderInterface $contextProvider |
|
25
|
|
|
* @param string $label |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function addProvider(ContextProviderInterface $contextProvider, $label) |
|
28
|
|
|
{ |
|
29
|
1 |
|
$this->providers[$label] = $contextProvider; |
|
30
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $migrationName |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function storeCurrentContext($migrationName) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$context = array(); |
|
38
|
1 |
|
foreach($this->providers as $label => $provider) { |
|
39
|
1 |
|
$context[$label] = $provider->getCurrentContext($migrationName); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
$this->storageHandler->storeMigrationContext($migrationName, $context); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $migrationName |
|
47
|
|
|
* @throws \Exception |
|
48
|
|
|
*/ |
|
49
|
|
|
public function restoreCurrentContext($migrationName) |
|
50
|
|
|
{ |
|
51
|
|
|
$context = $this->storageHandler->loadMigrationContext($migrationName); |
|
52
|
|
|
if (!is_array($context)) { |
|
53
|
|
|
throw new \Exception("No execution context found associated with migration '$migrationName'"); |
|
54
|
|
|
} |
|
55
|
|
|
foreach($this->providers as $label => $provider) { |
|
56
|
|
|
if (isset($context[$label])) { |
|
57
|
|
|
$provider->restoreContext($migrationName, $context[$label]); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function deleteContext($migrationName) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->storageHandler->deleteMigrationContext($migrationName); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|