PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Propel/Generator/Config/GeneratorConfig.php

http://github.com/propelorm/Propel2
PHP | 303 lines | 263 code | 15 blank | 25 comment | 11 complexity | 6c81f2ce15d659cb864f383822ee5d94 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\Generator\Config;
  8. use Propel\Common\Config\ConfigurationManager;
  9. use Propel\Common\Pluralizer\PluralizerInterface;
  10. use Propel\Generator\Exception\BuildException;
  11. use Propel\Generator\Exception\ClassNotFoundException;
  12. use Propel\Generator\Exception\InvalidArgumentException;
  13. use Propel\Generator\Model\Table;
  14. use Propel\Generator\Util\BehaviorLocator;
  15. use Propel\Runtime\Adapter\AdapterFactory;
  16. use Propel\Runtime\Connection\ConnectionFactory;
  17. use Propel\Runtime\Connection\ConnectionInterface;
  18. /**
  19. * A class that holds build properties and provide a class loading mechanism for
  20. * the generator.
  21. *
  22. * @author Hans Lellelid <hans@xmpl.org>
  23. * @author Cristiano Cinotti
  24. */
  25. class GeneratorConfig extends ConfigurationManager implements GeneratorConfigInterface
  26. {
  27. protected const PLURALIZER = PluralizerInterface::class;
  28. /**
  29. * @var \Propel\Generator\Util\BehaviorLocator
  30. */
  31. protected $behaviorLocator;
  32. /**
  33. * Connections configured in the `generator` section of the configuration file
  34. *
  35. * @var array
  36. */
  37. protected $buildConnections;
  38. /**
  39. * @inheritDoc
  40. *
  41. * @throws \Propel\Generator\Exception\ClassNotFoundException
  42. */
  43. public function getConfiguredPlatform(?ConnectionInterface $con = null, $database = null)
  44. {
  45. $platform = $this->get()['generator']['platformClass'];
  46. if ($platform === null) {
  47. if ($database) {
  48. $platform = $this->getBuildConnection($database)['adapter'];
  49. }
  50. if (!$platform) {
  51. $platform = '\\Propel\\Generator\\Platform\\MysqlPlatform';
  52. }
  53. }
  54. $classes = [
  55. $platform,
  56. '\\Propel\\Generator\\Platform\\' . $platform,
  57. '\\Propel\\Generator\\Platform\\' . ucfirst($platform),
  58. '\\Propel\\Generator\\Platform\\' . ucfirst(strtolower($platform)) . 'Platform',
  59. ];
  60. $platformClass = null;
  61. foreach ($classes as $class) {
  62. if (class_exists($class)) {
  63. $platformClass = $class;
  64. break;
  65. }
  66. }
  67. if ($platformClass === null) {
  68. throw new ClassNotFoundException(sprintf('Platform class for `%s` not found.', $platform));
  69. }
  70. /** @var \Propel\Generator\Platform\DefaultPlatform $platform */
  71. $platform = $this->getInstance($platformClass);
  72. $platform->setConnection($con);
  73. $platform->setGeneratorConfig($this);
  74. return $platform;
  75. }
  76. /**
  77. * @inheritDoc
  78. *
  79. * @throws \Propel\Generator\Exception\ClassNotFoundException
  80. */
  81. public function getConfiguredSchemaParser(?ConnectionInterface $con = null, $database = null)
  82. {
  83. $reverse = $this->get()['migrations']['parserClass'];
  84. if ($reverse === null) {
  85. if ($database) {
  86. $reverse = $this->getBuildConnection($database)['adapter'];
  87. } else {
  88. $connections = $this->getBuildConnections();
  89. $connection = $this->get()['generator']['defaultConnection'];
  90. if (isset($connections[$connection])) {
  91. $reverse = '\\Propel\\Generator\\Reverse\\' . ucfirst($connections[$connection]['adapter']) . 'SchemaParser';
  92. } else {
  93. $reverse = '\\Propel\\Generator\\Reverse\\MysqlSchemaParser';
  94. }
  95. }
  96. }
  97. $classes = [
  98. $reverse,
  99. '\\Propel\\Generator\\Reverse\\' . $reverse,
  100. '\\Propel\\Generator\\Reverse\\' . ucfirst($reverse),
  101. '\\Propel\\Generator\\Reverse\\' . ucfirst(strtolower($reverse)) . 'SchemaParser',
  102. ];
  103. $reverseClass = null;
  104. foreach ($classes as $class) {
  105. if (class_exists($class)) {
  106. $reverseClass = $class;
  107. break;
  108. }
  109. }
  110. if ($reverseClass === null) {
  111. throw new ClassNotFoundException(sprintf('Reverse SchemaParser class for `%s` not found.', $reverse));
  112. }
  113. /** @var \Propel\Generator\Reverse\AbstractSchemaParser $parser */
  114. $parser = $this->getInstance($reverseClass, null, '\\Propel\\Generator\\Reverse\\SchemaParserInterface');
  115. $parser->setConnection($con);
  116. $parser->setMigrationTable($this->get()['migrations']['tableName']);
  117. $parser->setGeneratorConfig($this);
  118. return $parser;
  119. }
  120. /**
  121. * Returns a configured data model builder class for specified table and
  122. * based on type ('object', 'query', 'tableMap' etc.).
  123. *
  124. * @param \Propel\Generator\Model\Table $table
  125. * @param string $type
  126. *
  127. * @return \Propel\Generator\Builder\DataModelBuilder
  128. */
  129. public function getConfiguredBuilder(Table $table, $type)
  130. {
  131. $classname = $this->getConfigProperty('generator.objectModel.builders.' . $type);
  132. /** @var \Propel\Generator\Builder\DataModelBuilder $builder */
  133. $builder = $this->getInstance($classname, $table);
  134. $builder->setGeneratorConfig($this);
  135. return $builder;
  136. }
  137. /**
  138. * Returns a configured Pluralizer class.
  139. *
  140. * @return \Propel\Common\Pluralizer\PluralizerInterface
  141. */
  142. public function getConfiguredPluralizer()
  143. {
  144. $classname = $this->get()['generator']['objectModel']['pluralizerClass'];
  145. /** @var \Propel\Common\Pluralizer\PluralizerInterface $pluralizer */
  146. $pluralizer = $this->getInstance($classname, null, static::PLURALIZER);
  147. return $pluralizer;
  148. }
  149. /**
  150. * Return an array of all configured connection properties, from `generator` and `reverse`
  151. * sections of the configuration.
  152. *
  153. * @return array
  154. */
  155. public function getBuildConnections()
  156. {
  157. if ($this->buildConnections === null) {
  158. $connectionNames = $this->get()['generator']['connections'];
  159. $reverseConnection = $this->getConfigProperty('reverse.connection');
  160. if ($reverseConnection !== null && !in_array($reverseConnection, $connectionNames, true)) {
  161. $connectionNames[] = $reverseConnection;
  162. }
  163. foreach ($connectionNames as $name) {
  164. if ($definition = $this->getConfigProperty('database.connections.' . $name)) {
  165. $this->buildConnections[$name] = $definition;
  166. }
  167. }
  168. }
  169. return $this->buildConnections;
  170. }
  171. /**
  172. * Return the connection properties array, of a given database name.
  173. * If the database name is null, it returns the default connection properties
  174. *
  175. * @param string|null $databaseName
  176. *
  177. * @throws \Propel\Generator\Exception\InvalidArgumentException if wrong database name
  178. *
  179. * @return array
  180. */
  181. public function getBuildConnection($databaseName = null)
  182. {
  183. if ($databaseName === null) {
  184. $databaseName = $this->get()['generator']['defaultConnection'];
  185. }
  186. if (!array_key_exists($databaseName, $this->getBuildConnections())) {
  187. throw new InvalidArgumentException("Invalid database name: no configured connection named `$databaseName`.");
  188. }
  189. return $this->getBuildConnections()[$databaseName];
  190. }
  191. /**
  192. * Return a connection object of a given database name
  193. *
  194. * @param string|null $database
  195. *
  196. * @return \Propel\Runtime\Connection\ConnectionInterface
  197. */
  198. public function getConnection($database = null)
  199. {
  200. $buildConnection = $this->getBuildConnection($database);
  201. //Still useful ?
  202. //$dsn = str_replace("@DB@", $database, $buildConnection['dsn']);
  203. $dsn = $buildConnection['dsn'];
  204. // Set user + password to null if they are empty strings or missing
  205. $username = isset($buildConnection['user']) && $buildConnection['user'] ? $buildConnection['user'] : null;
  206. $password = isset($buildConnection['password']) && $buildConnection['password'] ? $buildConnection['password'] : null;
  207. // Get options from and config and default to null
  208. $options = isset($buildConnection['options']) && is_array($buildConnection['options']) ? $buildConnection['options'] : null;
  209. $con = ConnectionFactory::create(['dsn' => $dsn, 'user' => $username, 'password' => $password, 'options' => $options], AdapterFactory::create($buildConnection['adapter']));
  210. return $con;
  211. }
  212. /**
  213. * @return \Propel\Generator\Util\BehaviorLocator
  214. */
  215. public function getBehaviorLocator()
  216. {
  217. if (!$this->behaviorLocator) {
  218. $this->behaviorLocator = new BehaviorLocator($this);
  219. }
  220. return $this->behaviorLocator;
  221. }
  222. /**
  223. * Return an instance of $className
  224. *
  225. * @param string $className The name of the class to return an instance
  226. * @param mixed|null $arguments
  227. * @param string|null $interfaceName The name of the interface to be implemented by the returned class
  228. *
  229. * @throws \Propel\Generator\Exception\ClassNotFoundException if the class doesn't exists
  230. * @throws \Propel\Generator\Exception\InvalidArgumentException if the interface doesn't exists
  231. * @throws \Propel\Generator\Exception\BuildException if the class isn't an implementation of the given interface
  232. *
  233. * @return object
  234. */
  235. private function getInstance($className, $arguments = null, $interfaceName = null)
  236. {
  237. if (!class_exists($className)) {
  238. throw new ClassNotFoundException("Class $className not found.");
  239. }
  240. $object = new $className($arguments);
  241. if ($interfaceName !== null) {
  242. if (!interface_exists($interfaceName)) {
  243. throw new InvalidArgumentException("Interface $interfaceName does not exists.");
  244. }
  245. if (!$object instanceof $interfaceName) {
  246. throw new BuildException("Specified class ($className) does not implement $interfaceName interface.");
  247. }
  248. }
  249. return $object;
  250. }
  251. }