SonataAdminExtension.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\DependencyInjection;
  11. use Symfony\Component\Config\FileLocator;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  14. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  17. /**
  18. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  19. * @author Michael Williams <michael.williams@funsational.com>
  20. */
  21. class SonataAdminExtension extends Extension implements PrependExtensionInterface
  22. {
  23. /**
  24. * @param array $configs An array of configuration settings
  25. * @param ContainerBuilder $container A ContainerBuilder instance
  26. */
  27. public function load(array $configs, ContainerBuilder $container)
  28. {
  29. $bundles = $container->getParameter('kernel.bundles');
  30. if (isset($bundles['SonataUserBundle'])) {
  31. // integrate the SonataUserBundle / FOSUserBundle if the bundle exists
  32. array_unshift($configs, array(
  33. 'templates' => array(
  34. 'user_block' => 'SonataUserBundle:Admin/Core:user_block.html.twig',
  35. ),
  36. ));
  37. }
  38. if (isset($bundles['SonataIntlBundle'])) {
  39. // integrate the SonataUserBundle if the bundle exists
  40. array_unshift($configs, array(
  41. 'templates' => array(
  42. 'history_revision_timestamp' => 'SonataIntlBundle:CRUD:history_revision_timestamp.html.twig',
  43. ),
  44. ));
  45. }
  46. $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  47. $loader->load('templates.xml');
  48. $loader->load('twig.xml');
  49. $loader->load('core.xml');
  50. $loader->load('form_types.xml');
  51. $loader->load('validator.xml');
  52. $loader->load('route.xml');
  53. $loader->load('block.xml');
  54. $loader->load('menu.xml');
  55. if (isset($bundles['SonataExporterBundle'])) {
  56. $loader->load('exporter.xml');
  57. }
  58. // NEXT_MAJOR : remove this block
  59. if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setDeprecated')) {
  60. $container->getDefinition('sonata.admin.exporter')->setDeprecated(
  61. 'The service "%service_id%" is deprecated in favor of the "sonata.exporter.exporter" service'
  62. );
  63. }
  64. // TODO: Go back on xml configuration when bumping requirements to SF 2.6+
  65. $sidebarMenu = $container->getDefinition('sonata.admin.sidebar_menu');
  66. if (method_exists($sidebarMenu, 'setFactory')) {
  67. $sidebarMenu->setFactory(array(new Reference('sonata.admin.menu_builder'), 'createSidebarMenu'));
  68. } else {
  69. $sidebarMenu->setFactoryService('sonata.admin.menu_builder');
  70. $sidebarMenu->setFactoryMethod('createSidebarMenu');
  71. }
  72. $configuration = $this->getConfiguration($configs, $container);
  73. $config = $this->processConfiguration($configuration, $configs);
  74. $config['options']['javascripts'] = $config['assets']['javascripts'];
  75. $config['options']['stylesheets'] = $config['assets']['stylesheets'];
  76. $pool = $container->getDefinition('sonata.admin.pool');
  77. $pool->replaceArgument(1, $config['title']);
  78. $pool->replaceArgument(2, $config['title_logo']);
  79. $pool->replaceArgument(3, $config['options']);
  80. if (false === $config['options']['lock_protection']) {
  81. $container->removeDefinition('sonata.admin.lock.extension');
  82. }
  83. $container->setParameter('sonata.admin.configuration.global_search.empty_boxes', $config['global_search']['empty_boxes']);
  84. $container->setParameter('sonata.admin.configuration.templates', $config['templates'] + array(
  85. 'user_block' => 'SonataAdminBundle:Core:user_block.html.twig',
  86. 'add_block' => 'SonataAdminBundle:Core:add_block.html.twig',
  87. 'layout' => 'SonataAdminBundle::standard_layout.html.twig',
  88. 'ajax' => 'SonataAdminBundle::ajax_layout.html.twig',
  89. 'dashboard' => 'SonataAdminBundle:Core:dashboard.html.twig',
  90. 'list' => 'SonataAdminBundle:CRUD:list.html.twig',
  91. 'filter' => 'SonataAdminBundle:Form:filter_admin_fields.html.twig',
  92. 'show' => 'SonataAdminBundle:CRUD:show.html.twig',
  93. 'show_compare' => 'SonataAdminBundle:CRUD:show_compare.html.twig',
  94. 'edit' => 'SonataAdminBundle:CRUD:edit.html.twig',
  95. 'history' => 'SonataAdminBundle:CRUD:history.html.twig',
  96. 'history_revision_timestamp' => 'SonataAdminBundle:CRUD:history_revision_timestamp.html.twig',
  97. 'acl' => 'SonataAdminBundle:CRUD:acl.html.twig',
  98. 'action' => 'SonataAdminBundle:CRUD:action.html.twig',
  99. 'short_object_description' => 'SonataAdminBundle:Helper:short-object-description.html.twig',
  100. 'preview' => 'SonataAdminBundle:CRUD:preview.html.twig',
  101. 'list_block' => 'SonataAdminBundle:Block:block_admin_list.html.twig',
  102. 'delete' => 'SonataAdminBundle:CRUD:delete.html.twig',
  103. 'batch' => 'SonataAdminBundle:CRUD:list__batch.html.twig',
  104. 'select' => 'SonataAdminBundle:CRUD:list__select.html.twig',
  105. 'batch_confirmation' => 'SonataAdminBundle:CRUD:batch_confirmation.html.twig',
  106. 'inner_list_row' => 'SonataAdminBundle:CRUD:list_inner_row.html.twig',
  107. 'base_list_field' => 'SonataAdminBundle:CRUD:base_list_field.html.twig',
  108. 'pager_links' => 'SonataAdminBundle:Pager:links.html.twig',
  109. 'pager_results' => 'SonataAdminBundle:Pager:results.html.twig',
  110. 'tab_menu_template' => 'SonataAdminBundle:Core:tab_menu_template.html.twig',
  111. 'knp_menu_template' => 'SonataAdminBundle:Menu:sonata_menu.html.twig',
  112. 'outer_list_rows_mosaic' => 'SonataAdminBundle:CRUD:list_outer_rows_mosaic.html.twig',
  113. 'outer_list_rows_list' => 'SonataAdminBundle:CRUD:list_outer_rows_list.html.twig',
  114. 'outer_list_rows_tree' => 'SonataAdminBundle:CRUD:list_outer_rows_tree.html.twig',
  115. ));
  116. $container->setParameter('sonata.admin.configuration.admin_services', $config['admin_services']);
  117. $container->setParameter('sonata.admin.configuration.dashboard_groups', $config['dashboard']['groups']);
  118. $container->setParameter('sonata.admin.configuration.dashboard_blocks', $config['dashboard']['blocks']);
  119. $container->setParameter('sonata.admin.configuration.sort_admins', $config['options']['sort_admins']);
  120. $container->setParameter('sonata.admin.configuration.breadcrumbs', $config['breadcrumbs']);
  121. if (null === $config['security']['acl_user_manager'] && isset($bundles['FOSUserBundle'])) {
  122. $container->setParameter('sonata.admin.security.acl_user_manager', 'fos_user.user_manager');
  123. } else {
  124. $container->setParameter('sonata.admin.security.acl_user_manager', $config['security']['acl_user_manager']);
  125. }
  126. $container->setAlias('sonata.admin.security.handler', $config['security']['handler']);
  127. switch ($config['security']['handler']) {
  128. case 'sonata.admin.security.handler.role':
  129. if (count($config['security']['information']) === 0) {
  130. $config['security']['information'] = array(
  131. 'EDIT' => array('EDIT'),
  132. 'LIST' => array('LIST'),
  133. 'CREATE' => array('CREATE'),
  134. 'VIEW' => array('VIEW'),
  135. 'DELETE' => array('DELETE'),
  136. 'EXPORT' => array('EXPORT'),
  137. 'ALL' => array('ALL'),
  138. );
  139. }
  140. break;
  141. case 'sonata.admin.security.handler.acl':
  142. if (count($config['security']['information']) === 0) {
  143. $config['security']['information'] = array(
  144. 'GUEST' => array('VIEW', 'LIST'),
  145. 'STAFF' => array('EDIT', 'LIST', 'CREATE'),
  146. 'EDITOR' => array('OPERATOR', 'EXPORT'),
  147. 'ADMIN' => array('MASTER'),
  148. );
  149. }
  150. break;
  151. }
  152. $container->setParameter('sonata.admin.configuration.security.information', $config['security']['information']);
  153. $container->setParameter('sonata.admin.configuration.security.admin_permissions', $config['security']['admin_permissions']);
  154. $container->setParameter('sonata.admin.configuration.security.object_permissions', $config['security']['object_permissions']);
  155. $loader->load('security.xml');
  156. // Set the SecurityContext for Symfony <2.6
  157. // NEXT_MAJOR: Go back to simple xml configuration when bumping requirements to SF 2.6+
  158. if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
  159. $tokenStorageReference = new Reference('security.token_storage');
  160. $authorizationCheckerReference = new Reference('security.authorization_checker');
  161. } else {
  162. $tokenStorageReference = new Reference('security.context');
  163. $authorizationCheckerReference = new Reference('security.context');
  164. }
  165. $container
  166. ->getDefinition('sonata.admin.security.handler.role')
  167. ->replaceArgument(0, $authorizationCheckerReference)
  168. ;
  169. $container
  170. ->getDefinition('sonata.admin.security.handler.acl')
  171. ->replaceArgument(0, $tokenStorageReference)
  172. ->replaceArgument(1, $authorizationCheckerReference)
  173. ;
  174. $container
  175. ->getDefinition('sonata.admin.menu.group_provider')
  176. ->replaceArgument(2, $authorizationCheckerReference)
  177. ;
  178. $container->setParameter('sonata.admin.extension.map', $config['extensions']);
  179. /*
  180. * This is a work in progress, so for now it is hardcoded
  181. */
  182. $classes = array(
  183. 'email' => '',
  184. 'textarea' => '',
  185. 'text' => '',
  186. 'choice' => '',
  187. 'integer' => '',
  188. 'datetime' => 'sonata-medium-date',
  189. 'date' => 'sonata-medium-date',
  190. // SF3+
  191. 'Symfony\Component\Form\Extension\Core\Type\ChoiceType' => '',
  192. 'Symfony\Component\Form\Extension\Core\Type\DateType' => 'sonata-medium-date',
  193. 'Symfony\Component\Form\Extension\Core\Type\DateTimeType' => 'sonata-medium-date',
  194. 'Symfony\Component\Form\Extension\Core\Type\EmailType' => '',
  195. 'Symfony\Component\Form\Extension\Core\Type\IntegerType' => '',
  196. 'Symfony\Component\Form\Extension\Core\Type\TextareaType' => '',
  197. 'Symfony\Component\Form\Extension\Core\Type\TextType' => '',
  198. );
  199. $container->getDefinition('sonata.admin.form.extension.field')
  200. ->replaceArgument(0, $classes)
  201. ->replaceArgument(1, $config['options']);
  202. // remove non used service
  203. if (!isset($bundles['JMSTranslationBundle'])) {
  204. $container->removeDefinition('sonata.admin.translator.extractor.jms_translator_bundle');
  205. }
  206. //remove non-Mopa compatibility layer
  207. if (isset($bundles['MopaBootstrapBundle'])) {
  208. $container->removeDefinition('sonata.admin.form.extension.field.mopa');
  209. }
  210. // set filter persistence
  211. $container->setParameter('sonata.admin.configuration.filters.persist', $config['persist_filters']);
  212. $container->setParameter('sonata.admin.configuration.show.mosaic.button', $config['show_mosaic_button']);
  213. $container->setParameter('sonata.admin.configuration.translate_group_label', $config['translate_group_label']);
  214. if (\PHP_VERSION_ID < 70000) {
  215. $this->configureClassesToCompile();
  216. }
  217. $this->replacePropertyAccessor($container);
  218. }
  219. /**
  220. * Allow an extension to prepend the extension configurations.
  221. *
  222. * NEXT_MAJOR: remove all code that deals with JMSDiExtraBundle
  223. *
  224. * @param ContainerBuilder $container
  225. */
  226. public function prepend(ContainerBuilder $container)
  227. {
  228. $bundles = $container->getParameter('kernel.bundles');
  229. if (!isset($bundles['JMSDiExtraBundle'])) {
  230. return;
  231. }
  232. $configs = $container->getExtensionConfig($this->getAlias());
  233. $config = $this->processConfiguration(new Configuration(), $configs);
  234. if (!$config['options']['enable_jms_di_extra_autoregistration']) {
  235. return;
  236. }
  237. $sonataAdminPattern = 'Sonata\AdminBundle\Annotation';
  238. $annotationPatternsConfigured = false;
  239. $diExtraConfigs = $container->getExtensionConfig('jms_di_extra');
  240. foreach ($diExtraConfigs as $diExtraConfig) {
  241. if (isset($diExtraConfig['annotation_patterns'])) {
  242. // don't add our own pattern if user has already done so
  243. if (array_search($sonataAdminPattern, $diExtraConfig['annotation_patterns']) !== false) {
  244. return;
  245. }
  246. $annotationPatternsConfigured = true;
  247. break;
  248. }
  249. }
  250. @trigger_error(
  251. 'Automatic registration of annotations is deprecated since 3.14, to be removed in 4.0.',
  252. E_USER_DEPRECATED
  253. );
  254. if ($annotationPatternsConfigured) {
  255. $annotationPatterns = array($sonataAdminPattern);
  256. } else {
  257. // get annotation_patterns default from DiExtraBundle configuration
  258. $diExtraConfigDefinition = new \JMS\DiExtraBundle\DependencyInjection\Configuration();
  259. // FIXME: this will break if DiExtraBundle adds any mandatory configuration
  260. $diExtraConfig = $this->processConfiguration($diExtraConfigDefinition, array());
  261. $annotationPatterns = $diExtraConfig['annotation_patterns'];
  262. $annotationPatterns[] = $sonataAdminPattern;
  263. }
  264. $container->prependExtensionConfig(
  265. 'jms_di_extra',
  266. array(
  267. 'annotation_patterns' => $annotationPatterns,
  268. )
  269. );
  270. }
  271. public function configureClassesToCompile()
  272. {
  273. $this->addClassesToCompile(array(
  274. 'Sonata\\AdminBundle\\Admin\\AbstractAdmin',
  275. 'Sonata\\AdminBundle\\Admin\\AbstractAdminExtension',
  276. 'Sonata\\AdminBundle\\Admin\\AdminExtensionInterface',
  277. 'Sonata\\AdminBundle\\Admin\\AdminHelper',
  278. 'Sonata\\AdminBundle\\Admin\\AdminInterface',
  279. 'Sonata\\AdminBundle\\Admin\\BaseFieldDescription',
  280. 'Sonata\\AdminBundle\\Admin\\FieldDescriptionCollection',
  281. 'Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface',
  282. 'Sonata\\AdminBundle\\Admin\\Pool',
  283. 'Sonata\\AdminBundle\\Block\\AdminListBlockService',
  284. 'Sonata\\AdminBundle\\Builder\\DatagridBuilderInterface',
  285. 'Sonata\\AdminBundle\\Builder\\FormContractorInterface',
  286. 'Sonata\\AdminBundle\\Builder\\ListBuilderInterface',
  287. 'Sonata\\AdminBundle\\Builder\\RouteBuilderInterface',
  288. 'Sonata\\AdminBundle\\Builder\\ShowBuilderInterface',
  289. 'Sonata\\AdminBundle\\Datagrid\\Datagrid',
  290. 'Sonata\\AdminBundle\\Datagrid\\DatagridInterface',
  291. 'Sonata\\AdminBundle\\Datagrid\\DatagridMapper',
  292. 'Sonata\\AdminBundle\\Datagrid\\ListMapper',
  293. 'Sonata\\AdminBundle\\Datagrid\\Pager',
  294. 'Sonata\\AdminBundle\\Datagrid\\PagerInterface',
  295. 'Sonata\\AdminBundle\\Datagrid\\ProxyQueryInterface',
  296. 'Sonata\\AdminBundle\\Exception\\ModelManagerException',
  297. 'Sonata\\AdminBundle\\Exception\\NoValueException',
  298. 'Sonata\\AdminBundle\\Filter\\Filter',
  299. 'Sonata\\AdminBundle\\Filter\\FilterFactory',
  300. 'Sonata\\AdminBundle\\Filter\\FilterFactoryInterface',
  301. 'Sonata\\AdminBundle\\Filter\\FilterInterface',
  302. 'Sonata\\AdminBundle\\Form\\DataTransformer\\ArrayToModelTransformer',
  303. 'Sonata\\AdminBundle\\Form\\DataTransformer\\ModelsToArrayTransformer',
  304. 'Sonata\\AdminBundle\\Form\\DataTransformer\\ModelToIdTransformer',
  305. 'Sonata\\AdminBundle\\Form\\EventListener\\MergeCollectionListener',
  306. 'Sonata\\AdminBundle\\Form\\Extension\\Field\\Type\\FormTypeFieldExtension',
  307. 'Sonata\\AdminBundle\\Form\\FormMapper',
  308. 'Sonata\\AdminBundle\\Form\\Type\\AdminType',
  309. 'Sonata\\AdminBundle\\Form\\Type\\Filter\\ChoiceType',
  310. 'Sonata\\AdminBundle\\Form\\Type\\Filter\\DateRangeType',
  311. 'Sonata\\AdminBundle\\Form\\Type\\Filter\\DateTimeRangeType',
  312. 'Sonata\\AdminBundle\\Form\\Type\\Filter\\DateTimeType',
  313. 'Sonata\\AdminBundle\\Form\\Type\\Filter\\DateType',
  314. 'Sonata\\AdminBundle\\Form\\Type\\Filter\\DefaultType',
  315. 'Sonata\\AdminBundle\\Form\\Type\\Filter\\NumberType',
  316. 'Sonata\\AdminBundle\\Form\\Type\\ModelReferenceType',
  317. 'Sonata\\AdminBundle\\Form\\Type\\ModelType',
  318. 'Sonata\\AdminBundle\\Form\\Type\\ModelListType',
  319. 'Sonata\\AdminBundle\\Guesser\\TypeGuesserChain',
  320. 'Sonata\\AdminBundle\\Guesser\\TypeGuesserInterface',
  321. 'Sonata\\AdminBundle\\Model\\AuditManager',
  322. 'Sonata\\AdminBundle\\Model\\AuditManagerInterface',
  323. 'Sonata\\AdminBundle\\Model\\AuditReaderInterface',
  324. 'Sonata\\AdminBundle\\Model\\ModelManagerInterface',
  325. 'Sonata\\AdminBundle\\Route\\AdminPoolLoader',
  326. 'Sonata\\AdminBundle\\Route\\DefaultRouteGenerator',
  327. 'Sonata\\AdminBundle\\Route\\PathInfoBuilder',
  328. 'Sonata\\AdminBundle\\Route\\QueryStringBuilder',
  329. 'Sonata\\AdminBundle\\Route\\RouteCollection',
  330. 'Sonata\\AdminBundle\\Route\\RouteGeneratorInterface',
  331. 'Sonata\\AdminBundle\\Security\\Acl\\Permission\\AdminPermissionMap',
  332. 'Sonata\\AdminBundle\\Security\\Acl\\Permission\\MaskBuilder',
  333. 'Sonata\\AdminBundle\\Security\\Handler\\AclSecurityHandler',
  334. 'Sonata\\AdminBundle\\Security\\Handler\\AclSecurityHandlerInterface',
  335. 'Sonata\\AdminBundle\\Security\\Handler\\NoopSecurityHandler',
  336. 'Sonata\\AdminBundle\\Security\\Handler\\RoleSecurityHandler',
  337. 'Sonata\\AdminBundle\\Security\\Handler\\SecurityHandlerInterface',
  338. 'Sonata\\AdminBundle\\Show\\ShowMapper',
  339. 'Sonata\\AdminBundle\\Translator\\BCLabelTranslatorStrategy',
  340. 'Sonata\\AdminBundle\\Translator\\FormLabelTranslatorStrategy',
  341. 'Sonata\\AdminBundle\\Translator\\LabelTranslatorStrategyInterface',
  342. 'Sonata\\AdminBundle\\Translator\\NativeLabelTranslatorStrategy',
  343. 'Sonata\\AdminBundle\\Translator\\NoopLabelTranslatorStrategy',
  344. 'Sonata\\AdminBundle\\Translator\\UnderscoreLabelTranslatorStrategy',
  345. 'Sonata\\AdminBundle\\Twig\\Extension\\SonataAdminExtension',
  346. 'Sonata\\AdminBundle\\Util\\AdminAclManipulator',
  347. 'Sonata\\AdminBundle\\Util\\AdminAclManipulatorInterface',
  348. 'Sonata\\AdminBundle\\Util\\FormBuilderIterator',
  349. 'Sonata\\AdminBundle\\Util\\FormViewIterator',
  350. 'Sonata\\AdminBundle\\Util\\ObjectAclManipulator',
  351. 'Sonata\\AdminBundle\\Util\\ObjectAclManipulatorInterface',
  352. ));
  353. }
  354. /**
  355. * {@inheritdoc}
  356. */
  357. public function getNamespace()
  358. {
  359. return 'https://sonata-project.org/schema/dic/admin';
  360. }
  361. private function replacePropertyAccessor(ContainerBuilder $container)
  362. {
  363. if (!$container->has('form.property_accessor')) {
  364. return;
  365. }
  366. $pool = $container->getDefinition('sonata.admin.pool');
  367. $pool->replaceArgument(4, new Reference('form.property_accessor'));
  368. $modelChoice = $container->getDefinition('sonata.admin.form.type.model_choice');
  369. $modelChoice->replaceArgument(0, new Reference('form.property_accessor'));
  370. }
  371. }