1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123:
<?php
namespace Inlm\SchemaGenerator;
use CzProject\SqlSchema;
class ConfigurationFactory
{
public function __construct()
{
throw new StaticClassException('This is static class.');
}
public static function fromArray(array $config)
{
$schema = new SqlSchema\Schema;
$configuration = new Configuration($schema);
if (isset($config['options']) && is_array($config['options'])) {
$configuration->setOptions($config['options']);
}
if (isset($config['schema']) && is_array($config['schema'])) {
foreach ($config['schema'] as $definition) {
self::createTable($schema, $definition);
}
}
return $configuration;
}
private static function createTable(SqlSchema\Schema $schema, array $definition)
{
$table = $schema->addTable($definition['name']);
if (isset($definition['comment'])) {
$table->setComment($definition['comment']);
}
if (isset($definition['columns'])) {
foreach ($definition['columns'] as $column) {
$table->addColumn(self::createTableColumn($column));
}
}
if (isset($definition['indexes'])) {
foreach ($definition['indexes'] as $index) {
$table->addIndex(self::createTableIndex($index));
}
}
if (isset($definition['foreignKeys'])) {
foreach ($definition['foreignKeys'] as $foreignKey) {
$table->addForeignKey(self::createTableForeignKey($foreignKey));
}
}
if (isset($definition['options'])) {
foreach ($definition['options'] as $option => $optionValue) {
$table->setOption($option, $optionValue);
}
}
}
private static function createTableColumn(array $definition)
{
$column = new SqlSchema\Column(
$definition['name'],
$definition['type'],
isset($definition['parameters']) ? $definition['parameters'] : array(),
isset($definition['options']) ? $definition['options'] : array()
);
$column->setNullable(isset($definition['nullable']) ? $definition['nullable'] : FALSE);
$column->setAutoIncrement(isset($definition['autoIncrement']) ? $definition['autoIncrement'] : FALSE);
$column->setComment(isset($definition['comment']) ? $definition['comment'] : NULL);
return $column;
}
private static function createTableIndex(array $definition)
{
$index = new SqlSchema\Index($definition['name'], $definition['type']);
foreach ($definition['columns'] as $column) {
$index->addColumn(self::createTableIndexColumn($column));
}
return $index;
}
private static function createTableIndexColumn(array $definition)
{
$order = isset($definition['order']) ? $definition['order'] : 'ASC';
$column = new SqlSchema\IndexColumn($definition['name'], $order);
$column->setLength(isset($definition['length']) ? $definition['length'] : NULL);
return $column;
}
private static function createTableForeignKey(array $definition)
{
$foreignKey = new SqlSchema\ForeignKey($definition['name'], $definition['columns'], $definition['targetTable'], $definition['targetColumns']);
if (isset($definition['onUpdateAction'])) {
$foreignKey->setOnUpdateAction($definition['onUpdateAction']);
}
if (isset($definition['onDeleteAction'])) {
$foreignKey->setOnDeleteAction($definition['onDeleteAction']);
}
return $foreignKey;
}
}