|
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
|
|
|
protected $supportedActions = array('create', 'delete'); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Handle the content create migration action |
|
18
|
|
|
*/ |
|
19
|
1 |
|
protected function create() |
|
20
|
|
|
{ |
|
21
|
1 |
|
$languageService = $this->repository->getContentLanguageService(); |
|
22
|
|
|
|
|
23
|
1 |
|
if (!isset($this->dsl['lang'])) { |
|
24
|
|
|
throw new \Exception("The 'lang' key is required to create a new language."); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
$languageCreateStruct = $languageService->newLanguageCreateStruct(); |
|
28
|
1 |
|
$languageCreateStruct->languageCode = $this->dsl['lang']; |
|
29
|
1 |
|
if (isset($this->dsl['name'])) { |
|
30
|
1 |
|
$languageCreateStruct->name = $this->dsl['name']; |
|
31
|
1 |
|
} |
|
32
|
1 |
|
if (isset($this->dsl['enabled'])) { |
|
33
|
|
|
$languageCreateStruct->enabled = (bool)$this->dsl['enabled']; |
|
34
|
|
|
} |
|
35
|
1 |
|
$language = $languageService->createLanguage($languageCreateStruct); |
|
36
|
|
|
|
|
37
|
1 |
|
$this->setReferences($language); |
|
38
|
|
|
|
|
39
|
1 |
|
return $language; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Handle the language update migration action |
|
44
|
|
|
* |
|
45
|
|
|
* @todo use a matcher for flexible matching? |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function update() |
|
48
|
|
|
{ |
|
49
|
|
|
throw new \Exception('Language update is not implemented yet'); |
|
50
|
|
|
|
|
51
|
|
|
/*$languageService = $this->repository->getContentLanguageService(); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
if (!isset($this->dsl['lang'])) { |
|
54
|
|
|
throw new \Exception("The 'lang' key is required to update a language."); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->setReferences($language);*/ |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Handle the language delete migration action |
|
62
|
|
|
*/ |
|
63
|
1 |
|
protected function delete() |
|
64
|
|
|
{ |
|
65
|
1 |
|
if (!isset($this->dsl['lang'])) { |
|
66
|
|
|
throw new \Exception("The 'lang' key is required to delete a language."); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
$languageService = $this->repository->getContentLanguageService(); |
|
70
|
1 |
|
$language = $languageService->loadLanguage($this->dsl['lang']); |
|
71
|
|
|
|
|
72
|
1 |
|
$languageService->deleteLanguage($language); |
|
73
|
|
|
|
|
74
|
1 |
|
return $language; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets references to certain content attributes. |
|
79
|
|
|
* The Content Manager currently supports setting references to object_id and location_id |
|
80
|
|
|
* |
|
81
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Language|LanguageCollection $language |
|
82
|
|
|
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute |
|
83
|
|
|
* @return boolean |
|
84
|
|
|
*/ |
|
85
|
1 |
|
protected function setReferences($language) |
|
86
|
|
|
{ |
|
87
|
1 |
|
if (!array_key_exists('references', $this->dsl)) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
if ($language instanceof LanguageCollection) { |
|
92
|
|
|
if (count($language) > 1) { |
|
93
|
|
|
throw new \InvalidArgumentException('Content Manager does not support setting references for creating/updating of multiple languages'); |
|
94
|
|
|
} |
|
95
|
|
|
$language = reset($language); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
foreach ($this->dsl['references'] as $reference) { |
|
99
|
|
|
|
|
100
|
1 |
|
switch ($reference['attribute']) { |
|
101
|
1 |
|
case 'language_id': |
|
102
|
1 |
|
case 'id': |
|
103
|
1 |
|
$value = $language->id; |
|
104
|
1 |
|
break; |
|
105
|
|
|
default: |
|
106
|
|
|
throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute']); |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
$this->referenceResolver->addReference($reference['identifier'], $value); |
|
|
|
|
|
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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.