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\NewsBundle\DependencyInjection; |
15
|
|
|
|
16
|
|
|
use Sonata\EasyExtendsBundle\Mapper\DoctrineCollector; |
17
|
|
|
use Symfony\Component\Config\Definition\Processor; |
18
|
|
|
use Symfony\Component\Config\FileLocator; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
20
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
22
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
23
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Thomas Rabaix <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class SonataNewsExtension extends Extension |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @throws \InvalidArgumentException |
32
|
|
|
*/ |
33
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
34
|
|
|
{ |
35
|
|
|
$processor = new Processor(); |
36
|
|
|
$configuration = new Configuration(); |
37
|
|
|
$config = $processor->processConfiguration($configuration, $configs); |
38
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
39
|
|
|
|
40
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
41
|
|
|
$loader->load('actions.xml'); |
42
|
|
|
$loader->load('twig.xml'); |
43
|
|
|
$loader->load('form.xml'); |
44
|
|
|
$loader->load('core.xml'); |
45
|
|
|
$loader->load('serializer.xml'); |
46
|
|
|
$loader->load('command.xml'); |
47
|
|
|
|
48
|
|
|
if (isset($bundles['SonataBlockBundle'])) { |
49
|
|
|
$loader->load('block.xml'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (isset($bundles['FOSRestBundle'], $bundles['NelmioApiDocBundle'])) { |
53
|
|
|
$loader->load(sprintf('api_form_%s.xml', $config['db_driver'])); |
54
|
|
|
if ('doctrine_orm' === $config['db_driver']) { |
55
|
|
|
$loader->load('api_controllers.xml'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$loader->load(sprintf('%s.xml', $config['db_driver'])); |
60
|
|
|
|
61
|
|
|
if (isset($bundles['SonataAdminBundle'])) { |
62
|
|
|
$loader->load(sprintf('%s_admin.xml', $config['db_driver'])); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!isset($config['salt'])) { |
66
|
|
|
throw new \InvalidArgumentException('The configuration node "salt" is not set for the SonataNewsBundle (sonata_news)'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!isset($config['comment'])) { |
70
|
|
|
throw new \InvalidArgumentException('The configuration node "comment" is not set for the SonataNewsBundle (sonata_news)'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$container->getDefinition('sonata.news.hash.generator') |
74
|
|
|
->replaceArgument(0, $config['salt']); |
75
|
|
|
|
76
|
|
|
$container->getDefinition('sonata.news.permalink.date') |
77
|
|
|
->replaceArgument(0, $config['permalink']['date']); |
78
|
|
|
|
79
|
|
|
$container->setAlias('sonata.news.permalink.generator', $config['permalink_generator']); |
80
|
|
|
|
81
|
|
|
$container->setDefinition('sonata.news.blog', new Definition('Sonata\NewsBundle\Model\Blog', [ |
82
|
|
|
$config['title'], |
83
|
|
|
$config['link'], |
84
|
|
|
$config['description'], |
85
|
|
|
new Reference('sonata.news.permalink.generator'), |
86
|
|
|
])); |
87
|
|
|
|
88
|
|
|
$container->getDefinition('sonata.news.hash.generator') |
89
|
|
|
->replaceArgument(0, $config['salt']); |
90
|
|
|
|
91
|
|
|
$container->getDefinition('sonata.news.mailer') |
92
|
|
|
->replaceArgument(5, [ |
93
|
|
|
'notification' => $config['comment']['notification'], |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
if ('doctrine_orm' === $config['db_driver']) { |
97
|
|
|
$this->registerDoctrineMapping($config); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->configureClass($config, $container); |
101
|
|
|
$this->configureAdmin($config, $container); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $config |
106
|
|
|
*/ |
107
|
|
|
public function configureClass($config, ContainerBuilder $container): void |
108
|
|
|
{ |
109
|
|
|
// admin configuration |
110
|
|
|
$container->setParameter('sonata.news.admin.post.entity', $config['class']['post']); |
111
|
|
|
$container->setParameter('sonata.news.admin.comment.entity', $config['class']['comment']); |
112
|
|
|
|
113
|
|
|
// manager configuration |
114
|
|
|
$container->setParameter('sonata.news.manager.post.entity', $config['class']['post']); |
115
|
|
|
$container->setParameter('sonata.news.manager.comment.entity', $config['class']['comment']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $config |
120
|
|
|
*/ |
121
|
|
|
public function configureAdmin($config, ContainerBuilder $container): void |
122
|
|
|
{ |
123
|
|
|
$container->setParameter('sonata.news.admin.post.class', $config['admin']['post']['class']); |
124
|
|
|
$container->setParameter('sonata.news.admin.post.controller', $config['admin']['post']['controller']); |
125
|
|
|
$container->setParameter('sonata.news.admin.post.translation_domain', $config['admin']['post']['translation']); |
126
|
|
|
|
127
|
|
|
$container->setParameter('sonata.news.admin.comment.class', $config['admin']['comment']['class']); |
128
|
|
|
$container->setParameter('sonata.news.admin.comment.controller', $config['admin']['comment']['controller']); |
129
|
|
|
$container->setParameter('sonata.news.admin.comment.translation_domain', $config['admin']['comment']['translation']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function registerDoctrineMapping(array $config): void |
133
|
|
|
{ |
134
|
|
|
$collector = DoctrineCollector::getInstance(); |
135
|
|
|
|
136
|
|
|
foreach ($config['class'] as $type => $class) { |
137
|
|
|
if (!class_exists($class)) { |
138
|
|
|
/* |
139
|
|
|
* NEXT_MAJOR: |
140
|
|
|
* Throw an exception if the class is not defined |
141
|
|
|
*/ |
142
|
|
|
@trigger_error(sprintf( |
|
|
|
|
143
|
|
|
'The "%s" class is not defined or does not exist. This is tolerated now but will be forbidden in 4.0', |
144
|
|
|
$class |
145
|
|
|
), E_USER_DEPRECATED); |
146
|
|
|
|
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$collector->addAssociation($config['class']['post'], 'mapOneToMany', [ |
152
|
|
|
'fieldName' => 'comments', |
153
|
|
|
'targetEntity' => $config['class']['comment'], |
154
|
|
|
'cascade' => [ |
155
|
|
|
0 => 'remove', |
156
|
|
|
1 => 'persist', |
157
|
|
|
], |
158
|
|
|
'mappedBy' => 'post', |
159
|
|
|
'orphanRemoval' => true, |
160
|
|
|
'orderBy' => [ |
161
|
|
|
'createdAt' => 'DESC', |
162
|
|
|
], |
163
|
|
|
]); |
164
|
|
|
|
165
|
|
|
$collector->addAssociation($config['class']['post'], 'mapManyToOne', [ |
166
|
|
|
'fieldName' => 'image', |
167
|
|
|
'targetEntity' => $config['class']['media'], |
168
|
|
|
'cascade' => [ |
169
|
|
|
0 => 'remove', |
170
|
|
|
1 => 'persist', |
171
|
|
|
2 => 'refresh', |
172
|
|
|
3 => 'merge', |
173
|
|
|
4 => 'detach', |
174
|
|
|
], |
175
|
|
|
'mappedBy' => null, |
176
|
|
|
'inversedBy' => null, |
177
|
|
|
'joinColumns' => [ |
178
|
|
|
[ |
179
|
|
|
'name' => 'image_id', |
180
|
|
|
'referencedColumnName' => 'id', |
181
|
|
|
], |
182
|
|
|
], |
183
|
|
|
'orphanRemoval' => false, |
184
|
|
|
]); |
185
|
|
|
|
186
|
|
|
$collector->addAssociation($config['class']['post'], 'mapManyToOne', [ |
187
|
|
|
'fieldName' => 'author', |
188
|
|
|
'targetEntity' => $config['class']['user'], |
189
|
|
|
'cascade' => [ |
190
|
|
|
1 => 'persist', |
191
|
|
|
], |
192
|
|
|
'mappedBy' => null, |
193
|
|
|
'inversedBy' => null, |
194
|
|
|
'joinColumns' => [ |
195
|
|
|
[ |
196
|
|
|
'name' => 'author_id', |
197
|
|
|
'referencedColumnName' => 'id', |
198
|
|
|
], |
199
|
|
|
], |
200
|
|
|
'orphanRemoval' => false, |
201
|
|
|
]); |
202
|
|
|
|
203
|
|
|
$collector->addAssociation($config['class']['post'], 'mapManyToOne', [ |
204
|
|
|
'fieldName' => 'collection', |
205
|
|
|
'targetEntity' => $config['class']['collection'], |
206
|
|
|
'cascade' => [ |
207
|
|
|
1 => 'persist', |
208
|
|
|
], |
209
|
|
|
'mappedBy' => null, |
210
|
|
|
'inversedBy' => null, |
211
|
|
|
'joinColumns' => [ |
212
|
|
|
[ |
213
|
|
|
'name' => 'collection_id', |
214
|
|
|
'referencedColumnName' => 'id', |
215
|
|
|
], |
216
|
|
|
], |
217
|
|
|
'orphanRemoval' => false, |
218
|
|
|
]); |
219
|
|
|
|
220
|
|
|
$collector->addAssociation($config['class']['post'], 'mapManyToMany', [ |
221
|
|
|
'fieldName' => 'tags', |
222
|
|
|
'targetEntity' => $config['class']['tag'], |
223
|
|
|
'cascade' => [ |
224
|
|
|
1 => 'persist', |
225
|
|
|
], |
226
|
|
|
'joinTable' => [ |
227
|
|
|
'name' => $config['table']['post_tag'], |
228
|
|
|
'joinColumns' => [ |
229
|
|
|
[ |
230
|
|
|
'name' => 'post_id', |
231
|
|
|
'referencedColumnName' => 'id', |
232
|
|
|
], |
233
|
|
|
], |
234
|
|
|
'inverseJoinColumns' => [ |
235
|
|
|
[ |
236
|
|
|
'name' => 'tag_id', |
237
|
|
|
'referencedColumnName' => 'id', |
238
|
|
|
], |
239
|
|
|
], |
240
|
|
|
], |
241
|
|
|
]); |
242
|
|
|
|
243
|
|
|
$collector->addAssociation($config['class']['comment'], 'mapManyToOne', [ |
244
|
|
|
'fieldName' => 'post', |
245
|
|
|
'targetEntity' => $config['class']['post'], |
246
|
|
|
'cascade' => [ |
247
|
|
|
], |
248
|
|
|
'mappedBy' => null, |
249
|
|
|
'inversedBy' => 'comments', |
250
|
|
|
'joinColumns' => [ |
251
|
|
|
[ |
252
|
|
|
'name' => 'post_id', |
253
|
|
|
'referencedColumnName' => 'id', |
254
|
|
|
'nullable' => false, |
255
|
|
|
], |
256
|
|
|
], |
257
|
|
|
'orphanRemoval' => false, |
258
|
|
|
]); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: