1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
4
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\LanguageAwareInterface; |
6
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface; |
7
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
8
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
9
|
|
|
|
10
|
|
|
use \eZ\Publish\API\Repository\Repository; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The core manager class that all migration action managers inherit from. |
14
|
|
|
*/ |
15
|
|
|
abstract class RepositoryExecutor extends AbstractExecutor implements LanguageAwareInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Constant defining the default language code |
19
|
|
|
*/ |
20
|
|
|
const DEFAULT_LANGUAGE_CODE = 'eng-GB'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constant defining the default Admin user ID. |
24
|
|
|
* |
25
|
|
|
* @todo inject via config parameter |
26
|
|
|
*/ |
27
|
|
|
const ADMIN_USER_ID = 14; |
28
|
|
|
|
29
|
|
|
/** @todo inject via config parameter */ |
30
|
|
|
const USER_CONTENT_TYPE = 'user'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array $dsl The parsed DSL instruction array |
34
|
|
|
*/ |
35
|
|
|
protected $dsl; |
36
|
|
|
|
37
|
|
|
/** @var array $context The context (configuration) for the execution of the current step */ |
38
|
|
|
protected $context; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The eZ Publish 5 API repository. |
42
|
|
|
* |
43
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
44
|
|
|
*/ |
45
|
|
|
protected $repository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Language code for current step. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $languageCode; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $defaultLanguageCode; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The bundle object representing the bundle the currently processed migration is in. |
61
|
|
|
* |
62
|
|
|
* @var BundleInterface |
63
|
|
|
*/ |
64
|
|
|
protected $bundle; |
65
|
|
|
|
66
|
|
|
/** @var ReferenceResolverInterface $referenceResolver */ |
67
|
|
|
protected $referenceResolver; |
68
|
|
|
|
69
|
|
|
// to redefine in subclasses if they don't support all methods, or if they support more... |
70
|
|
|
protected $supportedActions = array( |
71
|
|
|
'create', 'update', 'delete' |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
public function setRepository(Repository $repository) |
75
|
|
|
{ |
76
|
|
|
$this->repository = $repository; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setReferenceResolver(ReferenceResolverInterface $referenceResolver) |
80
|
|
|
{ |
81
|
|
|
$this->referenceResolver = $referenceResolver; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function execute(MigrationStep $step) |
85
|
|
|
{ |
86
|
|
|
// base checks |
87
|
|
|
parent::execute($step); |
88
|
|
|
|
89
|
|
|
if (!isset($step->dsl['mode'])) { |
90
|
|
|
throw new \Exception("Invalid step definition: missing 'mode'"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$action = $step->dsl['mode']; |
94
|
|
|
|
95
|
|
|
if (!in_array($action, $this->supportedActions)) { |
96
|
|
|
throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->dsl = $step->dsl; |
100
|
|
|
$this->context = $step->context; |
101
|
|
|
if (isset($this->dsl['lang'])) { |
102
|
|
|
$this->setLanguageCode($this->dsl['lang']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (method_exists($this, $action)) { |
106
|
|
|
|
107
|
|
|
$previousUserId = $this->loginUser(self::ADMIN_USER_ID); |
108
|
|
|
try { |
109
|
|
|
$this->$action(); |
110
|
|
|
} catch (\Exception $e) { |
111
|
|
|
$this->loginUser($previousUserId); |
112
|
|
|
throw $e; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// reset the environment as much as possible as we had found it before the migration |
116
|
|
|
$this->loginUser($previousUserId); |
117
|
|
|
|
118
|
|
|
} else { |
119
|
|
|
throw new \Exception("Invalid step definition: value '$action' is not a method of " . get_class($this)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Method that each executor (subclass) has to implement. |
125
|
|
|
* |
126
|
|
|
* It is used to set references based on the DSL instructions executed in the current step, for later steps to reuse. |
127
|
|
|
* |
128
|
|
|
* @throws \InvalidArgumentException when trying to set a reference to an unsupported attribute. |
129
|
|
|
* @param $object |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
|
|
abstract protected function setReferences($object); |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Helper method to log in a user that can make changes to the system. |
136
|
|
|
* @param int $userId |
137
|
|
|
* @return int id of the previously logged in user |
138
|
|
|
*/ |
139
|
|
|
protected function loginUser($userId) |
140
|
|
|
{ |
141
|
|
|
$previousUser = $this->repository->getCurrentUser(); |
142
|
|
|
|
143
|
|
|
if ($userId != $previousUser->id) { |
144
|
|
|
$this->repository->setCurrentUser($this->repository->getUserService()->loadUser($userId)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $previousUser->id; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function setLanguageCode($languageCode) |
151
|
|
|
{ |
152
|
|
|
$this->languageCode = $languageCode; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getLanguageCode() |
156
|
|
|
{ |
157
|
|
|
return $this->languageCode ?: $this->getDefaultLanguageCode(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setDefaultLanguageCode($languageCode) |
161
|
|
|
{ |
162
|
|
|
$this->defaultLanguageCode = $languageCode; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getDefaultLanguageCode() |
166
|
|
|
{ |
167
|
|
|
return $this->defaultLanguageCode ?: self::DEFAULT_LANGUAGE_CODE; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|