|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHP Translation package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) PHP Translation team <[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 Translation\Bundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
18
|
|
|
use Translation\Bundle\Exception\MessageValidationException; |
|
19
|
|
|
use Translation\Bundle\Service\CacheClearer; |
|
20
|
|
|
use Translation\Bundle\Service\StorageManager; |
|
21
|
|
|
use Translation\Bundle\Service\StorageService; |
|
22
|
|
|
use Translation\Common\Model\Message; |
|
23
|
|
|
use Translation\Common\Model\MessageInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @author Damien Alexandre <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class EditInPlaceController extends AbstractController |
|
29
|
|
|
{ |
|
30
|
|
|
private $storageManager; |
|
31
|
|
|
private $cacheClearer; |
|
32
|
|
|
private $validator; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(StorageManager $storageManager, CacheClearer $cacheClearer, ValidatorInterface $validator) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->storageManager = $storageManager; |
|
37
|
|
|
$this->cacheClearer = $cacheClearer; |
|
38
|
|
|
$this->validator = $validator; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function editAction(Request $request, string $configName, string $locale): Response |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
|
|
$messages = $this->getMessages($request, $locale, ['Edit']); |
|
45
|
|
|
} catch (MessageValidationException $e) { |
|
46
|
|
|
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @var StorageService $storage */ |
|
50
|
|
|
$storage = $this->storageManager->getStorage($configName); |
|
51
|
|
|
foreach ($messages as $message) { |
|
52
|
|
|
$storage->update($message); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->cacheClearer->clearAndWarmUp($locale); |
|
56
|
|
|
|
|
57
|
|
|
return new Response(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get and validate messages from the request. |
|
62
|
|
|
* |
|
63
|
|
|
* @return MessageInterface[] |
|
64
|
|
|
* |
|
65
|
|
|
* @throws MessageValidationException |
|
66
|
|
|
*/ |
|
67
|
|
|
private function getMessages(Request $request, string $locale, array $validationGroups = []): array |
|
68
|
|
|
{ |
|
69
|
|
|
$json = $request->getContent(); |
|
70
|
|
|
$data = \json_decode($json, true); |
|
71
|
|
|
$messages = []; |
|
72
|
|
|
|
|
73
|
|
|
foreach ($data as $key => $value) { |
|
74
|
|
|
[$domain, $translationKey] = \explode('|', $key); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$message = new Message($translationKey, $domain, $locale, $value); |
|
77
|
|
|
|
|
78
|
|
|
$errors = $this->validator->validate($message, null, $validationGroups); |
|
79
|
|
|
if (\count($errors) > 0) { |
|
80
|
|
|
throw MessageValidationException::create(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$messages[] = $message; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $messages; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.