PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/MwbExporter/Formatter/Formatter.php

http://github.com/johmue/mysql-workbench-schema-exporter
PHP | 432 lines | 208 code | 42 blank | 182 comment | 8 complexity | 03f911303081a6c9db3266ac33345572 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright (c) 2010 Johannes Mueller <circus2(at)web.de>
  6. * Copyright (c) 2012-2014 Toha <tohenk@yahoo.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. namespace MwbExporter\Formatter;
  27. use MwbExporter\Registry\Registry;
  28. use MwbExporter\Model\Base;
  29. use MwbExporter\Model\Catalog;
  30. use MwbExporter\Model\Schemas;
  31. use MwbExporter\Model\Schema;
  32. use MwbExporter\Model\Tables;
  33. use MwbExporter\Model\Table;
  34. use MwbExporter\Model\ForeignKeys;
  35. use MwbExporter\Model\ForeignKey;
  36. use MwbExporter\Model\Indices;
  37. use MwbExporter\Model\Index;
  38. use MwbExporter\Model\Columns;
  39. use MwbExporter\Model\Column;
  40. use MwbExporter\Model\Views;
  41. use MwbExporter\Model\View;
  42. use MwbExporter\Helper\Comment;
  43. use MwbExporter\Validator\ChoiceValidator;
  44. abstract class Formatter implements FormatterInterface
  45. {
  46. /**
  47. * @var string
  48. */
  49. private $name;
  50. /**
  51. * @var \MwbExporter\Registry\Registry
  52. */
  53. private $registry = null;
  54. /**
  55. * @var \MwbExporter\Formatter\DatatypeConverterInterface
  56. */
  57. private $datatypeConverter = null;
  58. /**
  59. * Constructor.
  60. *
  61. * @param string $name Formatter name
  62. */
  63. public function __construct($name = null)
  64. {
  65. $this->name = $name;
  66. $this->registry = new Registry();
  67. $this->addConfigurations(array(
  68. static::CFG_LOG_TO_CONSOLE => false,
  69. static::CFG_LOG_FILE => '',
  70. static::CFG_FILENAME => '%entity%.%extension%',
  71. static::CFG_INDENTATION => 2,
  72. static::CFG_USE_TABS => false,
  73. static::CFG_EOL => FormatterInterface::EOL_WIN,
  74. static::CFG_BACKUP_FILE => true,
  75. static::CFG_ADD_COMMENT => true,
  76. static::CFG_SKIP_PLURAL => false,
  77. static::CFG_USE_LOGGED_STORAGE => false,
  78. static::CFG_SORT_TABLES_AND_VIEWS => true,
  79. static::CFG_EXPORT_TABLE_CATEGORY => '',
  80. static::CFG_ENHANCE_M2M_DETECTION => true,
  81. static::CFG_SKIP_M2M_TABLES => true,
  82. static::CFG_STRIP_MULTIPLE_UNDERSCORES => false,
  83. ));
  84. $this->addValidators(array(
  85. static::CFG_EOL => new ChoiceValidator(array(FormatterInterface::EOL_WIN, FormatterInterface::EOL_UNIX)),
  86. ));
  87. $this->setDatatypeConverter($this->createDatatypeConverter());
  88. $this->init();
  89. }
  90. /**
  91. * Initialization.
  92. */
  93. protected function init()
  94. {
  95. }
  96. /**
  97. * Add configurations data.
  98. *
  99. * @param array $configurations Configurations data
  100. * @return \MwbExporter\Formatter\Formatter
  101. */
  102. protected function addConfigurations($configurations = array())
  103. {
  104. foreach ($configurations as $key => $value) {
  105. $this->registry->config->set($key, $value);
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Get all configurations.
  111. *
  112. * @return array
  113. */
  114. public function getConfigurations()
  115. {
  116. return $this->registry->config->getAll();
  117. }
  118. /**
  119. * Add configuration validators.
  120. *
  121. * @param array $validators Configuration validators
  122. * @return \MwbExporter\Formatter\Formatter
  123. */
  124. protected function addValidators($validators = array())
  125. {
  126. foreach ($validators as $key => $validator) {
  127. $this->registry->validator->set($key, $validator);
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Get configuration validators.
  133. *
  134. * @return array
  135. */
  136. public function getValidators()
  137. {
  138. return $this->registry->validator->getAll();
  139. }
  140. /**
  141. * Setup formatter.
  142. *
  143. * @param array $configurations
  144. * @throws \RuntimeException
  145. * @return \MwbExporter\Formatter\Formatter
  146. */
  147. public function setup($configurations = array())
  148. {
  149. foreach ($configurations as $key => $value) {
  150. if (!$this->registry->config->has($key)) {
  151. throw new \RuntimeException(sprintf('Unknown setup key %s.', $key));
  152. }
  153. if ($this->registry->validator->has($key)) {
  154. $validator = $this->registry->validator->get($key);
  155. if (!$validator->isValid($value)) {
  156. if (count($choices = $validator->getChoices())) {
  157. throw new \RuntimeException(sprintf('Invalid value %s for %s, values are %s.', var_export($value, true), $key, implode(', ', $choices)));
  158. } else {
  159. throw new \RuntimeException(sprintf('Invalid value %s for %s.', var_export($value, true), $key));
  160. }
  161. }
  162. }
  163. $this->registry->config->set($key, $value);
  164. }
  165. return $this;
  166. }
  167. /**
  168. * Create datatype converter instance.
  169. *
  170. * @return \MwbExporter\Formatter\DatatypeConverterInterface
  171. */
  172. protected function createDatatypeConverter()
  173. {
  174. }
  175. /**
  176. * Set data type converter.
  177. *
  178. * @param \MwbExporter\Formatter\DatatypeConverterInterface $datatypeConverter
  179. * @return \MwbExporter\Formatter\Formatter
  180. */
  181. protected function setDatatypeConverter(DatatypeConverterInterface $datatypeConverter)
  182. {
  183. if (null == $datatypeConverter) {
  184. throw new \RuntimeException('DatatypeConverter can\'t be null.');
  185. }
  186. $this->datatypeConverter = $datatypeConverter;
  187. $this->datatypeConverter->setup();
  188. return $this;
  189. }
  190. /**
  191. * Get formatter name.
  192. *
  193. * @return string
  194. */
  195. public function getName()
  196. {
  197. return $this->name;
  198. }
  199. /**
  200. * Get registry object.
  201. *
  202. * @return \MwbExporter\Registry\Registry
  203. */
  204. public function getRegistry()
  205. {
  206. return $this->registry;
  207. }
  208. /**
  209. * Get data type converter.
  210. *
  211. * @return \MwbExporter\Formatter\DatatypeConverterInterface
  212. */
  213. public function getDatatypeConverter()
  214. {
  215. if (null === $this->datatypeConverter) {
  216. throw new \RuntimeException('DatatypeConverter has not been set.');
  217. }
  218. return $this->datatypeConverter;
  219. }
  220. /**
  221. * (non-PHPdoc)
  222. * @see \MwbExporter\Formatter\FormatterInterface::createCatalog()
  223. */
  224. public function createCatalog(Base $parent, $node)
  225. {
  226. return new Catalog($parent, $node);
  227. }
  228. /**
  229. * (non-PHPdoc)
  230. * @see \MwbExporter\Formatter\FormatterInterface::createSchemas()
  231. */
  232. public function createSchemas(Base $parent, $node)
  233. {
  234. return new Schemas($parent, $node);
  235. }
  236. /**
  237. * (non-PHPdoc)
  238. * @see \MwbExporter\Formatter\FormatterInterface::createSchema()
  239. */
  240. public function createSchema(Base $parent, $node)
  241. {
  242. return new Schema($parent, $node);
  243. }
  244. /**
  245. * (non-PHPdoc)
  246. * @see \MwbExporter\Formatter\FormatterInterface::createTables()
  247. */
  248. public function createTables(Base $parent, $node)
  249. {
  250. return new Tables($parent, $node);
  251. }
  252. /**
  253. * (non-PHPdoc)
  254. * @see \MwbExporter\Formatter\FormatterInterface::createTable()
  255. */
  256. public function createTable(Base $parent, $node)
  257. {
  258. return new Table($parent, $node);
  259. }
  260. /**
  261. * (non-PHPdoc)
  262. * @see \MwbExporter\Formatter\FormatterInterface::createForeignKeys()
  263. */
  264. public function createForeignKeys(Base $parent, $node)
  265. {
  266. return new ForeignKeys($parent, $node);
  267. }
  268. /**
  269. * (non-PHPdoc)
  270. * @see \MwbExporter\Formatter\FormatterInterface::createForeignKey()
  271. */
  272. public function createForeignKey(Base $parent, $node)
  273. {
  274. return new ForeignKey($parent, $node);
  275. }
  276. /**
  277. * (non-PHPdoc)
  278. * @see \MwbExporter\Formatter\FormatterInterface::createIndices()
  279. */
  280. public function createIndices(Base $parent, $node)
  281. {
  282. return new Indices($parent, $node);
  283. }
  284. /**
  285. * (non-PHPdoc)
  286. * @see \MwbExporter\Formatter\FormatterInterface::createIndex()
  287. */
  288. public function createIndex(Base $parent, $node)
  289. {
  290. return new Index($parent, $node);
  291. }
  292. /**
  293. * (non-PHPdoc)
  294. * @see \MwbExporter\Formatter\FormatterInterface::createColumns()
  295. */
  296. public function createColumns(Base $parent, $node)
  297. {
  298. return new Columns($parent, $node);
  299. }
  300. /**
  301. * (non-PHPdoc)
  302. * @see \MwbExporter\Formatter\FormatterInterface::createColumn()
  303. */
  304. public function createColumn(Base $parent, $node)
  305. {
  306. return new Column($parent, $node);
  307. }
  308. /**
  309. * (non-PHPdoc)
  310. * @see \MwbExporter\Formatter\FormatterInterface::createViews()
  311. */
  312. public function createViews(Base $parent, $node)
  313. {
  314. return new Views($parent, $node);
  315. }
  316. /**
  317. * (non-PHPdoc)
  318. * @see \MwbExporter\Formatter\FormatterInterface::createView()
  319. */
  320. public function createView(Base $parent, $node)
  321. {
  322. return new View($parent, $node);
  323. }
  324. /**
  325. * (non-PHPdoc)
  326. * @see \MwbExporter\Formatter\FormatterInterface::getPreferredWriter()
  327. */
  328. public function getPreferredWriter()
  329. {
  330. return 'default';
  331. }
  332. /**
  333. * Get comment tag prefixes.
  334. *
  335. * @return array
  336. */
  337. protected function getCommentTagPrefixes()
  338. {
  339. return array('MwbExporter');
  340. }
  341. /**
  342. * (non-PHPdoc)
  343. * @see \MwbExporter\Formatter\FormatterInterface::getCommentTagPrefix()
  344. */
  345. public function getCommentTagPrefix()
  346. {
  347. return implode('|', $this->getCommentTagPrefixes());
  348. }
  349. /**
  350. * Get generated comment format.
  351. *
  352. * Variable placeholders supported:
  353. * %VERSION% Exporter version
  354. * %FORMATTER% Formatter name
  355. * %TIME% The date and time of code generation
  356. *
  357. * @return string
  358. */
  359. public function getCommentFormat()
  360. {
  361. return <<<EOF
  362. Auto generated by MySQL Workbench Schema Exporter.
  363. Version %VERSION% (%FORMATTER%) on %TIME%.
  364. Goto https://github.com/mysql-workbench-schema-exporter/mysql-workbench-schema-exporter for more information.
  365. EOF;
  366. }
  367. /**
  368. * Get comment variable substitution.
  369. *
  370. * @return array
  371. */
  372. public function getCommentVars()
  373. {
  374. return array(
  375. '%VERSION%' => static::VERSION,
  376. '%FORMATTER%' => $this->getName(),
  377. '%TIME%' => date('Y-m-d H:i:s'),
  378. );
  379. }
  380. /**
  381. * Get comment.
  382. *
  383. * @param string $format Comment wrapper format
  384. * @return string
  385. */
  386. public function getComment($format)
  387. {
  388. return implode("\n", Comment::wrap(strtr($this->getCommentFormat(), $this->getCommentVars()), $format));
  389. }
  390. }