|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Blast Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) 2015-2017 Libre Informatique |
|
7
|
|
|
* |
|
8
|
|
|
* This file is licenced under the GNU LGPL v3. |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Blast\Bundle\CoreBundle\Generator; |
|
14
|
|
|
|
|
15
|
|
|
use Sonata\AdminBundle\Manipulator\ServicesManipulator as BaseManipulator; |
|
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ServicesManipulator. |
|
20
|
|
|
*/ |
|
21
|
|
|
class ServicesManipulator extends BaseManipulator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $template = ' %s: |
|
|
|
|
|
|
27
|
|
|
class: %s |
|
28
|
|
|
arguments: [~, %s, %s] |
|
29
|
|
|
tags: |
|
30
|
|
|
- name: sonata.admin |
|
31
|
|
|
manager_type: orm |
|
32
|
|
|
group: admin |
|
33
|
|
|
label: %s |
|
34
|
|
|
|
|
35
|
|
|
'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $file |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($file) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->file = (string) $file; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $serviceId |
|
47
|
|
|
* @param string $modelClass |
|
48
|
|
|
* @param string $adminClass |
|
49
|
|
|
* @param string $controllerName |
|
50
|
|
|
* @param string $managerType |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \RuntimeException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function addResource($serviceId, $modelClass, $adminClass, $controllerName, $managerType) |
|
55
|
|
|
{ |
|
56
|
|
|
$rc = new \ReflectionClass($modelClass); |
|
57
|
|
|
$modelClassShortName = $rc->getShortName(); |
|
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 ($data['services'] !== null) { |
|
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
|
|
|
$modelClassShortName, |
|
93
|
|
|
$managerType, |
|
94
|
|
|
current(array_slice(explode('\\', $modelClass), -1)) |
|
95
|
|
|
); |
|
96
|
|
|
@mkdir(dirname($this->file), 0777, true); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
if (@file_put_contents($this->file, $code) === false) { |
|
|
|
|
|
|
99
|
|
|
throw new \RuntimeException(sprintf( |
|
100
|
|
|
'Unable to append service "%s" to the file "%s". You will have to do it manually.', |
|
101
|
|
|
$serviceId, |
|
102
|
|
|
$this->file |
|
|
|
|
|
|
103
|
|
|
)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|