|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\LanguageCollection; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Implements the actions for managing (create/update/delete) Languages in the system through |
|
9
|
|
|
* migrations and abstracts away the eZ Publish Public API. |
|
10
|
|
|
*/ |
|
11
|
|
|
class LanguageManager extends RepositoryExecutor |
|
12
|
|
|
{ |
|
13
|
|
|
protected $supportedStepTypes = array('language'); |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handle the content create migration action |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function create() |
|
19
|
|
|
{ |
|
20
|
|
|
$languageService = $this->repository->getContentLanguageService(); |
|
21
|
|
|
|
|
22
|
|
|
if (!isset($this->dsl['lang'])) { |
|
23
|
|
|
throw new \Exception("The 'lang' key is required to create a new language."); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$languageCreateStruct = $languageService->newLanguageCreateStruct(); |
|
27
|
|
|
$languageCreateStruct->languageCode = $this->dsl['lang']; |
|
28
|
|
|
if (isset($this->dsl['name'])) { |
|
29
|
|
|
$languageCreateStruct->name = $this->dsl['name']; |
|
30
|
|
|
} |
|
31
|
|
|
if (isset($this->dsl['enabled'])) { |
|
32
|
|
|
$languageCreateStruct->name = (bool)$this->dsl['enabled']; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
$language = $languageService->createLanguage($languageCreateStruct); |
|
35
|
|
|
|
|
36
|
|
|
$this->setReferences($language); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handle the language update migration action |
|
41
|
|
|
* |
|
42
|
|
|
* @todo use a matcher for flexible matching? |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function update() |
|
45
|
|
|
{ |
|
46
|
|
|
throw new \Exception('Language update is not implemented yet'); |
|
47
|
|
|
|
|
48
|
|
|
/*$languageService = $this->repository->getContentLanguageService(); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
if (!isset($this->dsl['lang'])) { |
|
51
|
|
|
throw new \Exception("The 'lang' key is required to update a language."); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->setReferences($language);*/ |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Handle the language delete migration action |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function delete() |
|
61
|
|
|
{ |
|
62
|
|
|
if (!isset($this->dsl['lang'])) { |
|
63
|
|
|
throw new \Exception("The 'lang' key is required to delete a language."); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$languageService = $this->repository->getContentLanguageService(); |
|
67
|
|
|
$language = $languageService->loadLanguage($this->dsl['lang']); |
|
68
|
|
|
|
|
69
|
|
|
$languageService->deleteLanguage($language); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Sets references to certain content attributes. |
|
74
|
|
|
* The Content Manager currently supports setting references to object_id and location_id |
|
75
|
|
|
* |
|
76
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Language|LanguageCollection $language |
|
77
|
|
|
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute |
|
78
|
|
|
* @return boolean |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function setReferences($language) |
|
81
|
|
|
{ |
|
82
|
|
|
if (!array_key_exists('references', $this->dsl)) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($language instanceof LanguageCollection) { |
|
87
|
|
|
if (count($language) > 1) { |
|
88
|
|
|
throw new \InvalidArgumentException('Content Manager does not support setting references for creating/updating of multiple languages'); |
|
89
|
|
|
} |
|
90
|
|
|
$language = reset($language); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
foreach ($this->dsl['references'] as $reference) { |
|
94
|
|
|
|
|
95
|
|
|
switch ($reference['attribute']) { |
|
96
|
|
|
case 'language_id': |
|
97
|
|
|
case 'id': |
|
98
|
|
|
$value = $language->id; |
|
99
|
|
|
break; |
|
100
|
|
|
default: |
|
101
|
|
|
throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute']); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->referenceResolver->addReference($reference['identifier'], $value); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.