Configuration.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  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\Definition\Builder\TreeBuilder;
  12. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  13. use Symfony\Component\Config\Definition\ConfigurationInterface;
  14. /**
  15. * This class contains the configuration information for the bundle
  16. *
  17. * This information is solely responsible for how the different configuration
  18. * sections are normalized, and merged.
  19. *
  20. * @author Michael Williams <mtotheikle@gmail.com>
  21. */
  22. class Configuration
  23. {
  24. /**
  25. * Generates the configuration tree.
  26. *
  27. * @return \Symfony\Component\Config\Definition\NodeInterface
  28. */
  29. public function getConfigTree($kernelDebug)
  30. {
  31. $treeBuilder = new TreeBuilder();
  32. $rootNode = $treeBuilder->root('sonata_admin', 'array');
  33. $this->addTemplateSection($rootNode);
  34. $this->addAccessControlSection($rootNode);
  35. $this->addRoleHierarchySection($rootNode);
  36. return $treeBuilder->buildTree();
  37. }
  38. private function addTemplateSection(ArrayNodeDefinition $rootNode)
  39. {
  40. $rootNode
  41. ->children()
  42. ->arrayNode('templates')
  43. ->children()
  44. ->scalarNode('layout')->cannotBeEmpty()->end()
  45. ->scalarNode('ajax')->cannotBeEmpty()->end()
  46. ->end()
  47. ->end()
  48. ->end()
  49. ->end();
  50. }
  51. private function addAccessControlSection(ArrayNodeDefinition $rootNode)
  52. {
  53. $rootNode
  54. ->fixXmlConfig('rule', 'access_control')
  55. ->children()
  56. ->arrayNode('access_control')
  57. ->cannotBeOverwritten()
  58. ->prototype('array')
  59. ->children()
  60. ->scalarNode('requires_channel')->defaultNull()->end()
  61. ->scalarNode('path')->defaultNull()->end()
  62. ->scalarNode('host')->defaultNull()->end()
  63. ->scalarNode('ip')->defaultNull()->end()
  64. ->arrayNode('methods')
  65. ->beforeNormalization()->ifString()->then(function($v) { return preg_split('/\s*,\s*/', $v); })->end()
  66. ->prototype('scalar')->end()
  67. ->end()
  68. ->end()
  69. ->fixXmlConfig('role')
  70. ->children()
  71. ->arrayNode('roles')
  72. ->beforeNormalization()->ifString()->then(function($v) { return preg_split('/\s*,\s*/', $v); })->end()
  73. ->prototype('scalar')->end()
  74. ->end()
  75. ->end()
  76. ->end()
  77. ->end()
  78. ->end()
  79. ;
  80. }
  81. private function addRoleHierarchySection(ArrayNodeDefinition $rootNode)
  82. {
  83. $rootNode
  84. ->fixXmlConfig('role', 'role_hierarchy')
  85. ->children()
  86. ->arrayNode('role_hierarchy')
  87. ->useAttributeAsKey('id')
  88. ->prototype('array')
  89. ->performNoDeepMerging()
  90. ->beforeNormalization()->ifString()->then(function($v) { return array('value' => $v); })->end()
  91. ->beforeNormalization()
  92. ->ifTrue(function($v) { return is_array($v) && isset($v['value']); })
  93. ->then(function($v) { return preg_split('/\s*,\s*/', $v['value']); })
  94. ->end()
  95. ->prototype('scalar')->end()
  96. ->end()
  97. ->end()
  98. ->end()
  99. ;
  100. }
  101. }