PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Propel/Common/Config/PropelConfiguration.php

http://github.com/propelorm/Propel2
PHP | 421 lines | 349 code | 16 blank | 56 comment | 3 complexity | 57b20d4c7bdcccd567479113fc494cac MD5 | raw file
  1. <?php
  2. /**
  3. * MIT License. This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. */
  7. namespace Propel\Common\Config;
  8. use InvalidArgumentException;
  9. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  10. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  11. use Symfony\Component\Config\Definition\ConfigurationInterface;
  12. /**
  13. * Class PropelConfiguration
  14. *
  15. * This class performs validation of configuration array and assign default values
  16. */
  17. class PropelConfiguration implements ConfigurationInterface
  18. {
  19. /**
  20. * Generates the configuration tree builder.
  21. *
  22. * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
  23. */
  24. public function getConfigTreeBuilder(): TreeBuilder
  25. {
  26. $treeBuilder = new TreeBuilder('propel');
  27. if (method_exists($treeBuilder, 'getRootNode')) {
  28. $rootNode = $treeBuilder->getRootNode();
  29. } else {
  30. // BC layer for symfony/config 4.1 and older
  31. $rootNode = $treeBuilder->root('propel');
  32. }
  33. $this->addGeneralSection($rootNode);
  34. $this->addExcludeTablesSection($rootNode);
  35. $this->addPathsSection($rootNode);
  36. $this->addDatabaseSection($rootNode);
  37. $this->addMigrationsSection($rootNode);
  38. $this->addReverseSection($rootNode);
  39. $this->addRuntimeSection($rootNode);
  40. $this->addGeneratorSection($rootNode);
  41. return $treeBuilder;
  42. }
  43. /**
  44. * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
  45. *
  46. * @return void
  47. */
  48. protected function addGeneralSection(ArrayNodeDefinition $node)
  49. {
  50. $node
  51. ->children()
  52. ->arrayNode('general')
  53. ->addDefaultsIfNotSet()
  54. ->children()
  55. ->scalarNode('project')->defaultValue('')->end()
  56. ->scalarNode('version')->defaultValue('2.0.0-dev')->end()
  57. ->end()
  58. ->end()
  59. ->end();
  60. }
  61. /**
  62. * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
  63. *
  64. * @return void
  65. */
  66. protected function addPathsSection(ArrayNodeDefinition $node)
  67. {
  68. $node
  69. ->children()
  70. ->arrayNode('paths')
  71. ->addDefaultsIfNotSet()
  72. ->children()
  73. ->scalarNode('projectDir')->defaultValue(getcwd())->end()
  74. ->scalarNode('schemaDir')->defaultValue(getcwd())->end()
  75. ->scalarNode('outputDir')->defaultValue(getcwd())->end()
  76. ->scalarNode('phpDir')->defaultValue(getcwd() . '/generated-classes')->end()
  77. ->scalarNode('phpConfDir')->defaultValue(getcwd() . '/generated-conf')->end()
  78. ->scalarNode('loaderScriptDir')->end()
  79. ->scalarNode('sqlDir')->defaultValue(getcwd() . '/generated-sql')->end()
  80. ->scalarNode('migrationDir')->defaultValue(getcwd() . '/generated-migrations')->end()
  81. ->scalarNode('composerDir')->defaultNull()->end()
  82. ->end()
  83. ->end()
  84. ->end();
  85. }
  86. /**
  87. * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
  88. *
  89. * @return void
  90. */
  91. protected function addDatabaseSection(ArrayNodeDefinition $node)
  92. {
  93. $node
  94. ->children()
  95. ->arrayNode('database')
  96. ->isRequired()
  97. ->addDefaultsIfNotSet()
  98. ->children()
  99. ->arrayNode('connections')
  100. ->isRequired()
  101. ->validate()
  102. ->always()
  103. ->then(function ($connections) {
  104. foreach ($connections as $name => $connection) {
  105. if (strpos($name, '.') !== false) {
  106. throw new InvalidArgumentException('Dots are not allowed in connection names');
  107. }
  108. }
  109. return $connections;
  110. })
  111. ->end()
  112. ->requiresAtLeastOneElement()
  113. ->normalizeKeys(false)
  114. ->prototype('array')
  115. ->fixXmlConfig('slave')
  116. ->fixXmlConfig('model_path')
  117. ->children()
  118. ->scalarNode('classname')->defaultValue('\Propel\Runtime\Connection\ConnectionWrapper')->end()
  119. ->enumNode('adapter')
  120. ->isRequired()
  121. ->cannotBeEmpty()
  122. ->values(['mysql', 'pgsql', 'sqlite', 'mssql', 'sqlsrv', 'oracle'])
  123. ->end()
  124. ->scalarNode('dsn')->isRequired()->cannotBeEmpty()->end()
  125. ->scalarNode('user')->isRequired()->end()
  126. ->scalarNode('password')->isRequired()->treatNullLike('')->end()
  127. ->arrayNode('options')
  128. ->children()
  129. ->booleanNode('ATTR_PERSISTENT')->defaultFalse()->end()
  130. ->scalarNode('MYSQL_ATTR_SSL_CA')->end()
  131. ->scalarNode('MYSQL_ATTR_SSL_CERT')->end()
  132. ->scalarNode('MYSQL_ATTR_SSL_KEY')->end()
  133. ->booleanNode('MYSQL_ATTR_SSL_VERIFY_SERVER_CERT')->end()
  134. ->scalarNode('MYSQL_ATTR_MAX_BUFFER_SIZE')->end()
  135. ->end()
  136. ->end()
  137. ->arrayNode('attributes')
  138. ->children()
  139. ->booleanNode('ATTR_EMULATE_PREPARES')->defaultFalse()->end()
  140. ->integerNode('ATTR_TIMEOUT')->min(1)->defaultValue(30)->end()
  141. ->end()
  142. ->end()
  143. ->arrayNode('settings')
  144. ->fixXmlConfig('query', 'queries')
  145. ->children()
  146. ->scalarNode('charset')->defaultValue('utf8')->end()
  147. ->arrayNode('queries')
  148. ->prototype('scalar')->end()
  149. ->end()
  150. ->end()
  151. ->end()
  152. ->arrayNode('model_paths')
  153. ->defaultValue(['src', 'vendor'])
  154. ->prototype('scalar')->end()
  155. ->end()
  156. ->arrayNode('slaves')
  157. ->prototype('array')
  158. ->children()
  159. ->scalarNode('dsn')->end()
  160. ->scalarNode('user')->end()
  161. ->scalarNode('password')->end()
  162. ->end()
  163. ->end()
  164. ->end()
  165. ->end()
  166. ->end()
  167. ->end()
  168. ->arrayNode('adapters')
  169. ->addDefaultsIfNotSet()
  170. ->children()
  171. ->arrayNode('mysql')
  172. ->addDefaultsIfNotSet()
  173. ->children()
  174. ->scalarNode('tableType')->defaultValue('InnoDB')->treatNullLike('InnoDB')->end()
  175. ->scalarNode('tableEngineKeyword')->defaultValue('ENGINE')->end()
  176. ->end()
  177. ->end()
  178. ->arrayNode('sqlite')
  179. ->addDefaultsIfNotSet()
  180. ->children()
  181. ->scalarNode('foreignKey')->end()
  182. ->scalarNode('tableAlteringWorkaround')->end()
  183. ->end()
  184. ->end()
  185. ->arrayNode('oracle')
  186. ->addDefaultsIfNotSet()
  187. ->children()
  188. ->scalarNode('autoincrementSequencePattern')->defaultValue('${table}_SEQ')->end()
  189. ->end()
  190. ->end()
  191. ->end()
  192. ->end() //adapters
  193. ->end()
  194. ->end() //database
  195. ->end();
  196. }
  197. /**
  198. * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
  199. *
  200. * @return void
  201. */
  202. protected function addMigrationsSection(ArrayNodeDefinition $node)
  203. {
  204. $node
  205. ->children()
  206. ->arrayNode('migrations')
  207. ->addDefaultsIfNotSet()
  208. ->children()
  209. ->booleanNode('samePhpName')->defaultFalse()->end()
  210. ->booleanNode('addVendorInfo')->defaultFalse()->end()
  211. ->scalarNode('tableName')->defaultValue('propel_migration')->end()
  212. ->scalarNode('parserClass')->defaultNull()->end()
  213. ->end()
  214. ->end() //migrations
  215. ->end();
  216. }
  217. /**
  218. * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
  219. *
  220. * @return void
  221. */
  222. protected function addReverseSection(ArrayNodeDefinition $node)
  223. {
  224. $node
  225. ->children()
  226. ->arrayNode('reverse')
  227. ->children()
  228. ->scalarNode('connection')->end()
  229. ->scalarNode('parserClass')->end()
  230. ->end()
  231. ->end() //reverse
  232. ->end();
  233. }
  234. /**
  235. * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
  236. *
  237. * @return void
  238. */
  239. protected function addExcludeTablesSection(ArrayNodeDefinition $node)
  240. {
  241. $node
  242. ->children()
  243. ->arrayNode('exclude_tables')
  244. ->prototype('scalar')->end()
  245. ->end()
  246. ->end();
  247. }
  248. /**
  249. * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
  250. *
  251. * @return void
  252. */
  253. protected function addRuntimeSection(ArrayNodeDefinition $node)
  254. {
  255. $node
  256. ->children()
  257. ->arrayNode('runtime')
  258. ->addDefaultsIfNotSet()
  259. ->fixXmlConfig('connection')
  260. ->children()
  261. ->scalarNode('defaultConnection')->end()
  262. ->arrayNode('connections')
  263. ->prototype('scalar')->end()
  264. ->end()
  265. ->arrayNode('log')
  266. ->useAttributeAsKey('name')
  267. ->prototype('array')
  268. ->children()
  269. ->scalarNode('type')->end()
  270. ->scalarNode('facility')->end()
  271. ->scalarNode('ident')->end()
  272. ->scalarNode('path')->end()
  273. ->enumNode('level')->values([100, 200, 250, 300, 400, 500, 550, 600])->end()
  274. ->end()
  275. ->end()
  276. ->end()
  277. ->arrayNode('profiler')
  278. ->children()
  279. ->scalarNode('classname')->defaultValue('\Propel\Runtime\Util\Profiler')->end()
  280. ->floatNode('slowTreshold')->defaultValue(0.1)->end()
  281. ->arrayNode('details')
  282. ->children()
  283. ->arrayNode('time')
  284. ->addDefaultsIfNotSet()
  285. ->children()
  286. ->scalarNode('name')->defaultValue('Time')->end()
  287. ->integerNode('precision')->min(0)->defaultValue(3)->end()
  288. ->integerNode('pad')->min(0)->defaultValue(8)->end()
  289. ->end()
  290. ->end()
  291. ->arrayNode('mem')
  292. ->addDefaultsIfNotSet()
  293. ->children()
  294. ->scalarNode('name')->defaultValue('Memory')->end()
  295. ->integerNode('precision')->min(0)->defaultValue(3)->end()
  296. ->integerNode('pad')->min(0)->defaultValue(8)->end()
  297. ->end()
  298. ->end()
  299. ->arrayNode('memDelta')
  300. ->addDefaultsIfNotSet()
  301. ->children()
  302. ->scalarNode('name')->defaultValue('Memory Delta')->end()
  303. ->integerNode('precision')->min(0)->defaultValue(3)->end()
  304. ->integerNode('pad')->min(0)->defaultValue(8)->end()
  305. ->end()
  306. ->end()
  307. ->arrayNode('memPeak')
  308. ->addDefaultsIfNotSet()
  309. ->children()
  310. ->scalarNode('name')->defaultValue('Memory Peak')->end()
  311. ->integerNode('precision')->min(0)->defaultValue(3)->end()
  312. ->integerNode('pad')->min(0)->defaultValue(8)->end()
  313. ->end()
  314. ->end()
  315. ->end()
  316. ->end()
  317. ->scalarNode('innerGlue')->defaultValue(': ')->end()
  318. ->scalarNode('outerGlue')->defaultValue(' | ')->end()
  319. ->end()
  320. ->end()
  321. ->end()
  322. ->end() //runtime
  323. ->end();
  324. }
  325. /**
  326. * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
  327. *
  328. * @return void
  329. */
  330. protected function addGeneratorSection(ArrayNodeDefinition $node)
  331. {
  332. $node
  333. ->children()
  334. ->arrayNode('generator')
  335. ->addDefaultsIfNotSet()
  336. ->fixXmlConfig('connection')
  337. ->children()
  338. ->scalarNode('defaultConnection')->end()
  339. ->scalarNode('tablePrefix')->end()
  340. ->scalarNode('platformClass')->defaultNull()->end()
  341. ->scalarNode('targetPackage')->end()
  342. ->booleanNode('packageObjectModel')->defaultTrue()->end()
  343. ->booleanNode('namespaceAutoPackage')->defaultTrue()->end()
  344. ->booleanNode('recursive')->defaultFalse()->end()
  345. ->arrayNode('connections')
  346. ->prototype('scalar')->end()
  347. ->end()
  348. ->arrayNode('schema')
  349. ->addDefaultsIfNotSet()
  350. ->children()
  351. ->scalarNode('basename')->defaultValue('schema')->end()
  352. ->booleanNode('autoPrefix')->defaultFalse()->end()
  353. ->booleanNode('autoPackage')->defaultFalse()->end()
  354. ->booleanNode('autoNamespace')->defaultFalse()->end()
  355. ->booleanNode('transform')->defaultFalse()->end()
  356. ->end()
  357. ->end() //schema
  358. ->arrayNode('dateTime')
  359. ->addDefaultsIfNotSet()
  360. ->children()
  361. ->booleanNode('useDateTimeClass')->defaultTrue()->end()
  362. ->scalarNode('dateTimeClass')->defaultValue('DateTime')->end()
  363. ->scalarNode('defaultTimeStampFormat')->end()
  364. ->scalarNode('defaultTimeFormat')->end()
  365. ->scalarNode('defaultDateFormat')->end()
  366. ->end()
  367. ->end()
  368. ->arrayNode('objectModel')
  369. ->addDefaultsIfNotSet()
  370. ->children()
  371. ->booleanNode('addGenericAccessors')->defaultTrue()->end()
  372. ->booleanNode('addGenericMutators')->defaultTrue()->end()
  373. ->booleanNode('emulateForeignKeyConstraints')->defaultFalse()->end()
  374. ->booleanNode('addClassLevelComment')->defaultTrue()->end()
  375. ->scalarNode('defaultKeyType')->defaultValue('phpName')->end()
  376. ->booleanNode('addSaveMethod')->defaultTrue()->end()
  377. ->scalarNode('namespaceMap')->defaultValue('Map')->end()
  378. ->booleanNode('addTimeStamp')->defaultFalse()->end()
  379. ->booleanNode('addHooks')->defaultTrue()->end()
  380. ->scalarNode('classPrefix')->defaultNull()->end()
  381. ->booleanNode('useLeftJoinsInDoJoinMethods')->defaultTrue()->end()
  382. ->scalarNode('pluralizerClass')->defaultValue('\Propel\Common\Pluralizer\StandardEnglishPluralizer')->end()
  383. ->scalarNode('entityNotFoundExceptionClass')->defaultValue('\Propel\Runtime\Exception\EntityNotFoundException')->end()
  384. ->arrayNode('builders')
  385. ->addDefaultsIfNotSet()
  386. ->children()
  387. ->scalarNode('object')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\ObjectBuilder')->end()
  388. ->scalarNode('objectstub')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\ExtensionObjectBuilder')->end()
  389. ->scalarNode('objectmultiextend')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\MultiExtendObjectBuilder')->end()
  390. ->scalarNode('query')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\QueryBuilder')->end()
  391. ->scalarNode('querystub')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\ExtensionQueryBuilder')->end()
  392. ->scalarNode('queryinheritance')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\QueryInheritanceBuilder')->end()
  393. ->scalarNode('queryinheritancestub')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\ExtensionQueryInheritanceBuilder')->end()
  394. ->scalarNode('tablemap')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\TableMapBuilder')->end()
  395. ->scalarNode('interface')->cannotBeEmpty()->defaultValue('\Propel\Generator\Builder\Om\InterfaceBuilder')->end()
  396. ->scalarNode('datasql')->cannotBeEmpty()->end()
  397. ->end()
  398. ->end()
  399. ->end()
  400. ->end() //objectModel
  401. ->end()
  402. ->end() //generator
  403. ->end();
  404. }
  405. }