|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Manipulator; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Marek Stipek <[email protected]> |
|
20
|
|
|
* @author Simon Cosandey <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class ServicesManipulator |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $file; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $template = ' %s: |
|
33
|
|
|
class: %s |
|
34
|
|
|
arguments: [~, %s, %s] |
|
35
|
|
|
tags: |
|
36
|
|
|
- { name: sonata.admin, manager_type: %s, group: admin, label: %s } |
|
37
|
|
|
public: true |
|
38
|
|
|
'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $file |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($file) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->file = (string) $file; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $serviceId |
|
50
|
|
|
* @param string $modelClass |
|
51
|
|
|
* @param string $adminClass |
|
52
|
|
|
* @param string $controllerName |
|
53
|
|
|
* @param string $managerType |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \RuntimeException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function addResource($serviceId, $modelClass, $adminClass, $controllerName, $managerType): void |
|
58
|
|
|
{ |
|
59
|
|
|
$code = "services:\n"; |
|
60
|
|
|
|
|
61
|
|
|
if (is_file($this->file)) { |
|
62
|
|
|
$code = rtrim(file_get_contents($this->file)); |
|
63
|
|
|
$data = (array) Yaml::parse($code); |
|
64
|
|
|
|
|
65
|
|
|
if ('' !== $code) { |
|
66
|
|
|
$code .= "\n"; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (\array_key_exists('services', $data)) { |
|
70
|
|
|
if (\array_key_exists($serviceId, (array) $data['services'])) { |
|
71
|
|
|
throw new \RuntimeException(sprintf( |
|
72
|
|
|
'The service "%s" is already defined in the file "%s".', |
|
73
|
|
|
$serviceId, |
|
74
|
|
|
realpath($this->file) |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (null !== $data['services']) { |
|
79
|
|
|
$code .= "\n"; |
|
80
|
|
|
} |
|
81
|
|
|
} else { |
|
82
|
|
|
$code .= '' === $code ? '' : "\n"."services:\n"; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$code .= sprintf( |
|
87
|
|
|
$this->template, |
|
88
|
|
|
$serviceId, |
|
89
|
|
|
$adminClass, |
|
90
|
|
|
$modelClass, |
|
91
|
|
|
$controllerName, |
|
92
|
|
|
$managerType, |
|
93
|
|
|
current(\array_slice(explode('\\', $modelClass), -1)) |
|
94
|
|
|
); |
|
95
|
|
|
@mkdir(\dirname($this->file), 0777, true); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
if (false === @file_put_contents($this->file, $code)) { |
|
98
|
|
|
throw new \RuntimeException(sprintf( |
|
99
|
|
|
'Unable to append service "%s" to the file "%s". You will have to do it manually.', |
|
100
|
|
|
$serviceId, |
|
101
|
|
|
$this->file |
|
102
|
|
|
)); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: