Configuration.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Bundle\MonologBundle\DependencyInjection;
  11. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  12. use Symfony\Component\Config\Definition\ConfigurationInterface;
  13. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  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 Jordi Boggiano <j.boggiano@seld.be>
  21. * @author Christophe Coevoet <stof@notk.org>
  22. */
  23. class Configuration implements ConfigurationInterface
  24. {
  25. /**
  26. * Generates the configuration tree builder.
  27. *
  28. * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
  29. */
  30. public function getConfigTreeBuilder()
  31. {
  32. $treeBuilder = new TreeBuilder();
  33. $rootNode = $treeBuilder->root('monolog');
  34. $rootNode
  35. ->fixXmlConfig('handler')
  36. ->children()
  37. ->arrayNode('handlers')
  38. ->canBeUnset()
  39. ->useAttributeAsKey('name')
  40. ->prototype('array')
  41. ->fixXmlConfig('member')
  42. ->canBeUnset()
  43. ->children()
  44. ->scalarNode('type')
  45. ->isRequired()
  46. ->treatNullLike('null')
  47. ->beforeNormalization()
  48. ->always()
  49. ->then(function($v) { return strtolower($v); })
  50. ->end()
  51. ->end()
  52. ->scalarNode('id')->end()
  53. ->scalarNode('priority')->defaultValue(0)->end()
  54. ->scalarNode('level')->defaultValue('DEBUG')->end()
  55. ->booleanNode('bubble')->defaultTrue()->end()
  56. ->scalarNode('path')->defaultValue('%kernel.logs_dir%/%kernel.environment%.log')->end() // stream and rotating
  57. ->scalarNode('ident')->defaultFalse()->end() // syslog
  58. ->scalarNode('logopts')->defaultValue(LOG_PID)->end() // syslog
  59. ->scalarNode('facility')->defaultValue('user')->end() // syslog
  60. ->scalarNode('max_files')->defaultValue(0)->end() // rotating
  61. ->scalarNode('action_level')->defaultValue('WARNING')->end() // fingers_crossed
  62. ->scalarNode('activation_strategy')->defaultNull()->end() // fingers_crossed
  63. ->booleanNode('stop_buffering')->defaultTrue()->end()// fingers_crossed
  64. ->scalarNode('buffer_size')->defaultValue(0)->end() // fingers_crossed and buffer
  65. ->scalarNode('handler')->end() // fingers_crossed and buffer
  66. ->arrayNode('publisher')
  67. ->canBeUnset()
  68. ->beforeNormalization()
  69. ->ifString()
  70. ->then(function($v) { return array('id'=> $v); })
  71. ->end()
  72. ->children()
  73. ->scalarNode('id')->end()
  74. ->scalarNode('hostname')->end()
  75. ->scalarNode('port')->defaultValue(12201)->end()
  76. ->scalarNode('chunk_size')->defaultValue(1420)->end()
  77. ->end()
  78. ->validate()
  79. ->ifTrue(function($v) {
  80. return !isset($v['id']) && !isset($v['hostname']);
  81. })
  82. ->thenInvalid('What must be set is either the hostname or the id.')
  83. ->end()
  84. ->end() // gelf
  85. ->arrayNode('members') // group
  86. ->canBeUnset()
  87. ->performNoDeepMerging()
  88. ->prototype('scalar')->end()
  89. ->end()
  90. ->scalarNode('from_email')->end() // swift_mailer and native_mailer
  91. ->arrayNode('to_email') // swift_mailer and native_mailer
  92. ->prototype('scalar')->end()
  93. ->beforeNormalization()
  94. ->ifString()
  95. ->then(function($v) { return array($v); })
  96. ->end()
  97. ->end()
  98. ->scalarNode('subject')->end() // swift_mailer and native_mailer
  99. ->arrayNode('email_prototype') // swift_mailer
  100. ->canBeUnset()
  101. ->beforeNormalization()
  102. ->ifString()
  103. ->then(function($v) { return array('id' => $v); })
  104. ->end()
  105. ->children()
  106. ->scalarNode('id')->isRequired()->end()
  107. ->scalarNode('factory-method')->defaultNull()->end()
  108. ->end()
  109. ->end()
  110. ->scalarNode('connection_string')->end() // socket_handler
  111. ->scalarNode('timeout')->end() // socket_handler
  112. ->scalarNode('connection_timeout')->end() // socket_handler
  113. ->booleanNode('persistent')->end() // socket_handler
  114. ->arrayNode('channels')
  115. ->fixXmlConfig('channel', 'elements')
  116. ->canBeUnset()
  117. ->beforeNormalization()
  118. ->ifString()
  119. ->then(function($v) { return array('elements' => array($v)); })
  120. ->end()
  121. ->beforeNormalization()
  122. ->ifTrue(function($v) { return is_array($v) && is_numeric(key($v)); })
  123. ->then(function($v) { return array('elements' => $v); })
  124. ->end()
  125. ->validate()
  126. ->ifTrue(function($v) { return empty($v); })
  127. ->thenUnset()
  128. ->end()
  129. ->validate()
  130. ->always(function ($v) {
  131. $isExclusive = null;
  132. if (isset($v['type'])) {
  133. $isExclusive = 'exclusive' === $v['type'];
  134. }
  135. $elements = array();
  136. foreach ($v['elements'] as $element) {
  137. if (0 === strpos($element, '!')) {
  138. if (false === $isExclusive) {
  139. throw new InvalidConfigurationException('Cannot combine exclusive/inclusive definitions in channels list.');
  140. }
  141. $elements[] = substr($element, 1);
  142. $isExclusive = true;
  143. } else {
  144. if (true === $isExclusive) {
  145. throw new InvalidConfigurationException('Cannot combine exclusive/inclusive definitions in channels list');
  146. }
  147. $elements[] = $element;
  148. $isExclusive = false;
  149. }
  150. }
  151. return array('type' => $isExclusive ? 'exclusive' : 'inclusive', 'elements' => $elements);
  152. })
  153. ->end()
  154. ->children()
  155. ->scalarNode('type')
  156. ->validate()
  157. ->ifNotInArray(array('inclusive', 'exclusive'))
  158. ->thenInvalid('The type of channels has to be inclusive or exclusive')
  159. ->end()
  160. ->end()
  161. ->arrayNode('elements')
  162. ->prototype('scalar')->end()
  163. ->end()
  164. ->end()
  165. ->end()
  166. ->scalarNode('formatter')->end()
  167. ->end()
  168. ->validate()
  169. ->ifTrue(function($v) { return ('fingers_crossed' === $v['type'] || 'buffer' === $v['type']) && 1 !== count($v['handler']); })
  170. ->thenInvalid('The handler has to be specified to use a FingersCrossedHandler or BufferHandler')
  171. ->end()
  172. ->validate()
  173. ->ifTrue(function($v) { return 'swift_mailer' === $v['type'] && empty($v['email_prototype']) && (empty($v['from_email']) || empty($v['to_email']) || empty($v['subject'])); })
  174. ->thenInvalid('The sender, recipient and subject or an email prototype have to be specified to use a SwiftMailerHandler')
  175. ->end()
  176. ->validate()
  177. ->ifTrue(function($v) { return 'native_mailer' === $v['type'] && (empty($v['from_email']) || empty($v['to_email']) || empty($v['subject'])); })
  178. ->thenInvalid('The sender, recipient and subject have to be specified to use a NativeMailerHandler')
  179. ->end()
  180. ->validate()
  181. ->ifTrue(function($v) { return 'service' === $v['type'] && !isset($v['id']); })
  182. ->thenInvalid('The id has to be specified to use a service as handler')
  183. ->end()
  184. ->validate()
  185. ->ifTrue(function($v) { return 'gelf' === $v['type'] && !isset($v['publisher']); })
  186. ->thenInvalid('The publisher has to be specified to use a GelfHandler')
  187. ->end()
  188. ->validate()
  189. ->ifTrue(function($v) { return 'socket' === $v['type'] && !isset($v['connection_string']); })
  190. ->thenInvalid('The connection_string has to be specified to use a SocketHandler')
  191. ->end()
  192. ->end()
  193. ->validate()
  194. ->ifTrue(function($v) { return isset($v['debug']); })
  195. ->thenInvalid('The "debug" name cannot be used as it is reserved for the handler of the profiler')
  196. ->end()
  197. ->example(array(
  198. 'syslog' => array(
  199. 'type' => 'stream',
  200. 'path' => '/var/log/symfony.log',
  201. 'level' => 'ERROR',
  202. 'bubble' => 'false',
  203. 'formatter' => 'my_formatter',
  204. 'processors' => array('some_callable')
  205. ),
  206. 'main' => array(
  207. 'type' => 'fingers_crossed',
  208. 'action_level' => 'WARNING',
  209. 'buffer_size' => 30,
  210. 'handler' => 'custom',
  211. ),
  212. 'custom' => array(
  213. 'type' => 'service',
  214. 'id' => 'my_handler'
  215. )
  216. ))
  217. ->end()
  218. ->end()
  219. ;
  220. return $treeBuilder;
  221. }
  222. }