├── LICENSE ├── README.md ├── composer.json └── lib └── MwbExporter └── Formatter └── Laravel5 ├── DatatypeConverter.php ├── Migration ├── DatatypeConverter.php ├── Formatter.php └── Model │ ├── ForeignKey.php │ └── Table.php └── Model ├── DatatypeConverter.php ├── Formatter.php └── Model ├── ForeignKey.php └── Table.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010 Johannes Mueller 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel 5.0 Exporter 2 | [![Latest Stable Version](https://poser.pugx.org/vitalibr/laravel5-exporter/v/stable)](https://packagist.org/packages/vitalibr/laravel5-exporter) [![Total Downloads](https://poser.pugx.org/vitalibr/laravel5-exporter/downloads)](https://packagist.org/packages/vitalibr/laravel5-exporter) [![Latest Unstable Version](https://poser.pugx.org/vitalibr/laravel5-exporter/v/unstable)](https://packagist.org/packages/vitalibr/laravel5-exporter) [![License](https://poser.pugx.org/vitalibr/laravel5-exporter/license)](https://packagist.org/packages/vitalibr/laravel5-exporter) 3 | 4 | This is an exporter to convert [MySQL Workbench](http://www.mysql.com/products/workbench/) Models (\*.mwb) to Laravel Framework 5 Model and Migration Schema. 5 | 6 | ## Prerequisites 7 | 8 | * PHP 5.4+ 9 | * Composer to install the dependencies 10 | 11 | ## Installation 12 | 13 | ``` 14 | php composer.phar require --dev vitalibr/laravel5-exporter 15 | ``` 16 | 17 | This will install the exporter and also require [mysql-workbench-schema-exporter](https://github.com/mysql-workbench-schema-exporter/mysql-workbench-schema-exporter). 18 | 19 | You then can invoke the CLI script using `vendor/bin/mysql-workbench-schema-export`. 20 | 21 | ## Formatter Setup Options 22 | 23 | Additionally to the [common options](https://github.com/mysql-workbench-schema-exporter/mysql-workbench-schema-exporter#configuring-mysql-workbench-schema-exporter) of mysql-workbench-schema-exporter these options are supported: 24 | 25 | ### Laravel Model 26 | 27 | #### Setup Options 28 | 29 | * `namespace` 30 | 31 | Namespace for generated class. 32 | 33 | Default is `App\Models`. 34 | 35 | * `parentTable` 36 | 37 | Ancestor class, the class to extend for generated class. 38 | 39 | Default is `Model`. 40 | 41 | * `generateFillable` 42 | 43 | Generate variable fillable with all columns. 44 | 45 | Default is `false`. 46 | 47 | ### Laravel Migration 48 | 49 | #### Setup Options 50 | 51 | * `tablePrefix` 52 | 53 | Table prefix for generated class. 54 | 55 | Default is `Create`. 56 | 57 | * `tableSuffix` 58 | 59 | Table suffix for generated class. 60 | 61 | Default is `Table`. 62 | 63 | * `parentTable` 64 | 65 | See above. 66 | 67 | Default is `Migration`. 68 | 69 | * `generateTimestamps` 70 | 71 | Generate `created_at` and `updated_at` columns to all Tables. 72 | 73 | Default is `false`. 74 | 75 | ## Command Line Interface (CLI) 76 | 77 | See documentation for [mysql-workbench-schema-exporter](https://github.com/mysql-workbench-schema-exporter/mysql-workbench-schema-exporter#command-line-interface-cli) 78 | 79 | ## Examples (v3.0.3) 80 | 81 | #### Workbench Schema 82 | ![alt tag](http://s33.postimg.org/xdhdnf7qn/model.png) 83 | 84 | #### Model 85 | ![alt tag](http://s33.postimg.org/j2azui227/migrations.png) 86 | 87 | #### Migration 88 | ![alt tag](http://s33.postimg.org/muhdy952n/migrations.png) 89 | 90 | ## Links 91 | 92 | * [MySQL Workbench](http://wb.mysql.com/) 93 | * [Laravel Project](https://laravel.com/) 94 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "vitalibr/laravel5-exporter", 3 | "type" : "library", 4 | "description" : "MySQL Workbench Schema Exporter for Laravel 5.0", 5 | "keywords" : [ 6 | "mysql", 7 | "mysql workbench", 8 | "database", 9 | "cli", 10 | "laravel" 11 | ], 12 | "homepage" : "https://github.com/vitalibr/laravel5-exporter", 13 | "license" : "MIT", 14 | "authors" : [{ 15 | "name" : "Mateus Vitali", 16 | "email" : "mateus.c.vitali@gmail.com", 17 | "role" : "Developer" 18 | } 19 | ], 20 | "require" : { 21 | "php" : ">=5.4.0", 22 | "mysql-workbench-schema-exporter/mysql-workbench-schema-exporter": "^3.0.0" 23 | }, 24 | "autoload" : { 25 | "psr-4" : { 26 | "MwbExporter\\Formatter\\Laravel5\\" : "lib/MwbExporter/Formatter/Laravel5/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/DatatypeConverter.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5; 29 | 30 | use MwbExporter\Formatter\DatatypeConverter as BaseDatatypeConverter; 31 | 32 | class DatatypeConverter extends BaseDatatypeConverter 33 | { 34 | public function setup() 35 | { 36 | $this->register(array( 37 | static::DATATYPE_TINYINT => 'tinyInteger', 38 | static::DATATYPE_SMALLINT => 'smallInteger', 39 | static::DATATYPE_MEDIUMINT => 'mediumInteger', 40 | static::DATATYPE_INT => 'integer', 41 | static::DATATYPE_BIGINT => 'bigInteger', 42 | static::DATATYPE_FLOAT => 'float', 43 | static::DATATYPE_DOUBLE => 'double', 44 | static::DATATYPE_DECIMAL => 'decimal', 45 | static::DATATYPE_CHAR => 'char', 46 | static::DATATYPE_VARCHAR => 'string', 47 | static::DATATYPE_BINARY => 'binary', 48 | static::DATATYPE_VARBINARY => '', // ???? 49 | static::DATATYPE_TINYTEXT => '', // ???? 50 | static::DATATYPE_TEXT => 'text', 51 | static::DATATYPE_MEDIUMTEXT => 'mediumText', 52 | static::DATATYPE_LONGTEXT => 'longText', 53 | static::DATATYPE_TINYBLOB => '', // ???? 54 | static::DATATYPE_BLOB => 'binary', 55 | static::DATATYPE_MEDIUMBLOB => '', // ???? 56 | static::DATATYPE_LONGBLOB => '', // ???? 57 | static::DATATYPE_DATETIME => 'dateTime', 58 | static::DATATYPE_DATETIME_F => 'dateTime', 59 | static::DATATYPE_DATE => 'date', 60 | static::DATATYPE_DATE_F => 'date', 61 | static::DATATYPE_TIME => 'time', 62 | static::DATATYPE_TIME_F => 'time', 63 | static::DATATYPE_TIMESTAMP => 'timestamp', 64 | static::DATATYPE_TIMESTAMP_F => 'timestamp', 65 | static::DATATYPE_YEAR => 'smallInteger', 66 | static::DATATYPE_GEOMETRY => '', // ???? 67 | static::DATATYPE_LINESTRING => '', // ???? 68 | static::DATATYPE_POLYGON => '', // ???? 69 | static::DATATYPE_MULTIPOINT => '', // ???? 70 | static::DATATYPE_MULTILINESTRING => '', // ???? 71 | static::DATATYPE_MULTIPOLYGON => '', // ???? 72 | static::DATATYPE_GEOMETRYCOLLECTION => '', // ???? 73 | static::DATATYPE_BIT => '', // ???? 74 | static::DATATYPE_ENUM => 'enum', 75 | static::DATATYPE_SET => '', // ???? 76 | static::USERDATATYPE_BOOLEAN => 'boolean', 77 | static::USERDATATYPE_BOOL => 'boolean', 78 | static::USERDATATYPE_FIXED => '', // ???? 79 | static::USERDATATYPE_FLOAT4 => '', // ???? 80 | static::USERDATATYPE_FLOAT8 => '', // ???? 81 | static::USERDATATYPE_INT1 => 'tinyInteger', 82 | static::USERDATATYPE_INT2 => 'smallInteger', 83 | static::USERDATATYPE_INT3 => 'mediumInteger', 84 | static::USERDATATYPE_INT4 => 'integer', 85 | static::USERDATATYPE_INT8 => 'bigint', 86 | static::USERDATATYPE_INTEGER => 'integer', 87 | static::USERDATATYPE_LONGVARBINARY => '', // ???? 88 | static::USERDATATYPE_LONGVARCHAR => '', // ???? 89 | static::USERDATATYPE_LONG => '', // ???? 90 | static::USERDATATYPE_MIDDLEINT => 'mediumInteger', 91 | static::USERDATATYPE_NUMERIC => 'decimal', 92 | static::USERDATATYPE_DEC => 'decimal', 93 | static::USERDATATYPE_CHARACTER => 'char', 94 | )); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/Migration/DatatypeConverter.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5\Migration; 29 | 30 | use MwbExporter\Formatter\Laravel5\DatatypeConverter as BaseDatatypeConverter; 31 | 32 | class DatatypeConverter extends BaseDatatypeConverter 33 | { 34 | } 35 | -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/Migration/Formatter.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5\Migration; 29 | 30 | use MwbExporter\Formatter\Formatter as BaseFormatter; 31 | use MwbExporter\Model\Base; 32 | 33 | class Formatter extends BaseFormatter 34 | { 35 | const CFG_TABLE_PREFIX = 'tablePrefix'; 36 | const CFG_TABLE_SUFFIX = 'tableSuffix'; 37 | const CFG_PARENT_TABLE = 'parentTable'; 38 | const CFG_GENERATE_TIMESTAPMS = 'generateTimestamps'; 39 | 40 | protected function init() 41 | { 42 | parent::init(); 43 | $dataName = (new \DateTime())->format('Y_m_d_His'); 44 | $this->addConfigurations(array( 45 | static::CFG_INDENTATION => 4, 46 | static::CFG_FILENAME => 'Migrations/%schema%/' . $dataName . '_create_%table%_table.%extension%', 47 | static::CFG_PARENT_TABLE => 'Migration', 48 | static::CFG_TABLE_PREFIX => 'Create', 49 | static::CFG_TABLE_SUFFIX => 'Table', 50 | static::CFG_GENERATE_TIMESTAPMS => false, 51 | )); 52 | } 53 | 54 | /** 55 | * (non-PHPdoc) 56 | * @see \MwbExporter\Formatter\Formatter::createDatatypeConverter() 57 | */ 58 | protected function createDatatypeConverter() 59 | { 60 | return new DatatypeConverter(); 61 | } 62 | 63 | /** 64 | * (non-PHPdoc) 65 | * @see \MwbExporter\Formatter\Formatter::createTable() 66 | */ 67 | public function createTable(Base $parent, $node) 68 | { 69 | return new Model\Table($parent, $node); 70 | } 71 | 72 | /** 73 | * (non-PHPdoc) 74 | * @see \MwbExporter\Formatter\Formatter::createForeignKey() 75 | */ 76 | public function createForeignKey(Base $parent, $node) 77 | { 78 | return new Model\ForeignKey($parent, $node); 79 | } 80 | 81 | public function getTitle() 82 | { 83 | return 'Laravel5 Migration'; 84 | } 85 | 86 | public function getFileExtension() 87 | { 88 | return 'php'; 89 | } 90 | } -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/Migration/Model/ForeignKey.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5\Migration\Model; 29 | 30 | use MwbExporter\Model\ForeignKey as BaseForeignKey; 31 | use MwbExporter\Writer\WriterInterface; 32 | 33 | class ForeignKey extends BaseForeignKey 34 | { 35 | public function write(WriterInterface $writer) 36 | { 37 | $writer 38 | ->write('$table->foreign(\'' . $this->getLocal()->getColumnName() . 39 | '\')->references(\'' . $this->getForeign()->getColumnName() . 40 | '\')->on(\'' . $this->getReferencedTable()->getRawTableName() . '\');'); 41 | ; 42 | 43 | return $this; 44 | } 45 | } -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/Migration/Model/Table.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5\Migration\Model; 29 | 30 | use MwbExporter\Model\Table as BaseTable; 31 | use MwbExporter\Formatter\Laravel5\Migration\Formatter; 32 | use MwbExporter\Writer\WriterInterface; 33 | use MwbExporter\Helper\Comment; 34 | 35 | class Table extends BaseTable 36 | { 37 | 38 | public function getTablePrefix() 39 | { 40 | return $this->translateVars($this->getConfig()->get(Formatter::CFG_TABLE_PREFIX)); 41 | } 42 | 43 | public function getTableSuffix() 44 | { 45 | return $this->translateVars($this->getConfig()->get(Formatter::CFG_TABLE_PREFIX)); 46 | } 47 | 48 | public function getParentTable() 49 | { 50 | return $this->translateVars($this->getConfig()->get(Formatter::CFG_PARENT_TABLE)); 51 | } 52 | 53 | public function writeTable(WriterInterface $writer) 54 | { 55 | if (!$this->isExternal()) { 56 | // $this->getModelName() return singular form with correct camel case 57 | // $this->getRawTableName() return original form with no camel case 58 | $writer 59 | ->open($this->getTableFileName()) 60 | ->write('write('') 62 | ->write('use Illuminate\Database\Schema\Blueprint;') 63 | ->write('use Illuminate\Database\Migrations\Migration;') 64 | ->write('') 65 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 66 | if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { 67 | $writer 68 | ->write($_this->getFormatter()->getComment(Comment::FORMAT_PHP)) 69 | ->write('') 70 | ; 71 | } 72 | }) 73 | ->write('class ' . $this->getTablePrefix() . $this->beautify($this->getRawTableName()) . $this->getTableSuffix() . ' extends '. $this->getParentTable()) 74 | ->write('{') 75 | ->indent() 76 | ->write('') 77 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 78 | $_this->writeUp($writer); 79 | }) 80 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 81 | $_this->writeDown($writer); 82 | }) 83 | ->outdent() 84 | ->write('}') 85 | ->write('') 86 | ->close() 87 | ; 88 | 89 | return self::WRITE_OK; 90 | } 91 | 92 | return self::WRITE_EXTERNAL; 93 | } 94 | 95 | public function writeUp(WriterInterface $writer) 96 | { 97 | $writer 98 | ->write('/**') 99 | ->write(' * Run the migrations.') 100 | ->write(' *') 101 | ->write(' * @return void') 102 | ->write(' */') 103 | ->write('public function up()') 104 | ->write('{') 105 | ->indent() 106 | ->write('Schema::create(\''. $this->getRawTableName() . '\', function(Blueprint $table)') 107 | ->write('{') 108 | ->indent() 109 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 110 | $_this->writeColumns($writer); 111 | }) 112 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 113 | $_this->writeForeignKeys($writer); 114 | }) 115 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 116 | if ($_this->getConfig()->get(Formatter::CFG_GENERATE_TIMESTAPMS)) { 117 | $writer->write('$table->timestamps();'); 118 | } 119 | }) 120 | ->outdent() 121 | ->write('});') 122 | ->outdent() 123 | ->write('}') 124 | ->write('') 125 | ; 126 | 127 | return $this; 128 | } 129 | 130 | public function writeColumns(WriterInterface $writer) 131 | { 132 | $writer 133 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 134 | if (count($_this->getColumns())) { 135 | foreach ($_this->getColumns() as $column) { 136 | $line = '$table->'; 137 | $type = $this->getFormatter()->getDatatypeConverter()->getType($column); 138 | 139 | if ($column->isPrimary()) { 140 | if($type == 'bigInteger') { 141 | $writer->write('$table->bigIncrements(\'' . $column->getColumnName() . '\');'); 142 | } else { 143 | $writer->write('$table->increments(\'' . $column->getColumnName() . '\');'); 144 | } 145 | continue; 146 | } 147 | 148 | /* 149 | * TODO: 'isUnique' it is not provided by BaseTable and it is necessary to check for size in columns, like decimal(12,5). 150 | */ 151 | 152 | $precision = ''; 153 | if($column->getLength() != -1) { 154 | $precision = ', ' . $column->getLength(); 155 | } else if($column->getParameter('precision') != -1 && $column->getParameter('scale') != -1) { 156 | $precision = ', ' . $column->getParameter('precision') . ', ' . $column->getParameter('scale'); 157 | } 158 | $line .= $type . '(\'' . $column->getColumnName() . '\'' . $precision . ')'; 159 | if (!$column->isNotNull()) { 160 | $line .= '->nullable()'; 161 | } 162 | if ($column->isUnsigned()) { 163 | $line .= '->unsigned()'; 164 | } 165 | $line .= ';'; 166 | $writer->write($line); 167 | } 168 | } 169 | }) 170 | ; 171 | 172 | return $this; 173 | } 174 | 175 | public function writeForeignKeys(WriterInterface $writer) 176 | { 177 | $writer 178 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 179 | if (count($_this->getForeignKeys())) { 180 | foreach ($_this->getForeignKeys() as $foreignKey) { 181 | $foreignKey->write($writer); 182 | } 183 | } 184 | }) 185 | ; 186 | 187 | return $this; 188 | } 189 | 190 | public function writeDown(WriterInterface $writer) 191 | { 192 | $writer 193 | ->write('/**') 194 | ->write(' * Reverse the migrations.') 195 | ->write(' *') 196 | ->write(' * @return void') 197 | ->write(' */') 198 | ->write('public function down()') 199 | ->write('{') 200 | ->indent() 201 | ->write('Schema::drop(\''. $this->getRawTableName() . '\');') 202 | ->outdent() 203 | ->write('}') 204 | ->write('') 205 | ; 206 | 207 | return $this; 208 | } 209 | } -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/Model/DatatypeConverter.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5\Model; 29 | 30 | use MwbExporter\Formatter\Laravel5\DatatypeConverter as BaseDatatypeConverter; 31 | 32 | class DatatypeConverter extends BaseDatatypeConverter 33 | { 34 | } 35 | -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/Model/Formatter.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5\Model; 29 | 30 | use MwbExporter\Formatter\Formatter as BaseFormatter; 31 | use MwbExporter\Model\Base; 32 | 33 | class Formatter extends BaseFormatter 34 | { 35 | const CFG_PARENT_TABLE = 'parentTable'; 36 | const CFG_NAMESPACE = 'namespace'; 37 | const CFG_GENERATE_FILLABLE = 'generateFillable'; 38 | 39 | protected function init() 40 | { 41 | parent::init(); 42 | $this->addConfigurations(array( 43 | static::CFG_INDENTATION => 4, 44 | static::CFG_FILENAME => 'Models/%schema%/%entity%.%extension%', 45 | static::CFG_PARENT_TABLE => 'Model', 46 | static::CFG_NAMESPACE => 'App\Models', 47 | static::CFG_GENERATE_FILLABLE => false, 48 | )); 49 | } 50 | 51 | /** 52 | * (non-PHPdoc) 53 | * @see \MwbExporter\Formatter\Formatter::createDatatypeConverter() 54 | */ 55 | protected function createDatatypeConverter() 56 | { 57 | return new DatatypeConverter(); 58 | } 59 | 60 | /** 61 | * (non-PHPdoc) 62 | * @see \MwbExporter\Formatter\Formatter::createTable() 63 | */ 64 | public function createTable(Base $parent, $node) 65 | { 66 | return new Model\Table($parent, $node); 67 | } 68 | 69 | /** 70 | * (non-PHPdoc) 71 | * @see \MwbExporter\Formatter\Formatter::createForeignKey() 72 | */ 73 | public function createForeignKey(Base $parent, $node) 74 | { 75 | return new Model\ForeignKey($parent, $node); 76 | } 77 | 78 | public function getTitle() 79 | { 80 | return 'Laravel5 Model'; 81 | } 82 | 83 | public function getFileExtension() 84 | { 85 | return 'php'; 86 | } 87 | } -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/Model/Model/ForeignKey.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5\Model\Model; 29 | 30 | use MwbExporter\Model\ForeignKey as BaseForeignKey; 31 | use MwbExporter\Writer\WriterInterface; 32 | use Doctrine\Common\Inflector\Inflector; 33 | 34 | class ForeignKey extends BaseForeignKey 35 | { 36 | public function write(WriterInterface $writer) 37 | { 38 | $writer 39 | ->write('/**') 40 | ->write(' * Relationship with ' . $this->getReferencedTable()->getModelName() . '.') 41 | ->write(' */') 42 | ->write('public function ' . Inflector::pluralize($this->getReferencedTable()->getRawTableName()) . '()') 43 | ->write('{') 44 | ->indent() 45 | ->write('return $this->belongsTo(\'' . $this->getReferencedTable()->getNamespace() . '\\' . $this->getReferencedTable()->getModelName() . '\', \'' . $this->getLocal()->getColumnName() . '\');') 46 | ->outdent() 47 | ->write('}') 48 | ->write(''); 49 | ; 50 | 51 | return $this; 52 | } 53 | } -------------------------------------------------------------------------------- /lib/MwbExporter/Formatter/Laravel5/Model/Model/Table.php: -------------------------------------------------------------------------------- 1 | 7 | * Copyright (c) 2012-2014 Toha 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | namespace MwbExporter\Formatter\Laravel5\Model\Model; 29 | 30 | use MwbExporter\Model\Table as BaseTable; 31 | use MwbExporter\Formatter\Laravel5\Model\Formatter; 32 | use MwbExporter\Writer\WriterInterface; 33 | use MwbExporter\Helper\Comment; 34 | use Doctrine\Common\Inflector\Inflector; 35 | 36 | class Table extends BaseTable 37 | { 38 | public function getNamespace() 39 | { 40 | return $this->translateVars($this->getConfig()->get(Formatter::CFG_NAMESPACE)); 41 | } 42 | 43 | public function getParentTable() 44 | { 45 | return $this->translateVars($this->getConfig()->get(Formatter::CFG_PARENT_TABLE)); 46 | } 47 | 48 | public function writeTable(WriterInterface $writer) 49 | { 50 | if (!$this->isExternal() && !$this->isManyToMany()) { 51 | // $this->getModelName() return singular form with correct camel case 52 | // $this->getRawTableName() return original form with no camel case 53 | $writer 54 | ->open($this->getTableFileName()) 55 | ->write('getNamespace() . ';') 56 | ->write('') 57 | ->write('use Illuminate\Database\Eloquent\Model;') 58 | ->write('') 59 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 60 | if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { 61 | $writer 62 | ->write($_this->getFormatter()->getComment(Comment::FORMAT_PHP)) 63 | ->write('') 64 | ; 65 | } 66 | }) 67 | ->write('class ' . $this->getModelName() . ' extends '. $this->getParentTable()) 68 | ->write('{') 69 | ->indent() 70 | ->write('/**') 71 | ->write(' * The database table used by the model.') 72 | ->write(' * ') 73 | ->write(' * @var string') 74 | ->write(' */') 75 | ->write('protected $table = \''. $this->getRawTableName() .'\';') 76 | ->write('') 77 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 78 | if ($_this->getConfig()->get(Formatter::CFG_GENERATE_FILLABLE)) { 79 | $_this->writeFillable($writer); 80 | } 81 | }) 82 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 83 | $_this->writeRelationships($writer); 84 | }) 85 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 86 | $_this->writeReferences($writer); 87 | }) 88 | ->outdent() 89 | ->write('}') 90 | ->write('') 91 | ->close() 92 | ; 93 | 94 | return self::WRITE_OK; 95 | } 96 | 97 | return self::WRITE_EXTERNAL; 98 | } 99 | 100 | public function writeReferences(WriterInterface $writer) 101 | { 102 | $writer 103 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 104 | if (count($_this->getColumns())) { 105 | // Get current column from this table 106 | foreach ($_this->getColumns() as $column) { 107 | // Get tables from the same schema 108 | foreach ($this->getParent() as $table) { 109 | 110 | // If not a pivot table 111 | if(!$table->isManyToMany()) { 112 | // Get foreignKeys from table 113 | foreach ($table->getForeignKeys() as $foreignKey) { 114 | // If current column is referenced by foreignKey 115 | if(($_this->getRawTableName() == $foreignKey->getReferencedTable()->getRawTableName()) && 116 | ($column->getColumnName() == $foreignKey->getForeign()->getColumnName())) { 117 | // Comment 118 | $writer->write('/**'); 119 | $writer->write(' * Relationship with ' . $foreignKey->getOwningTable()->getModelName() . '.'); 120 | $writer->write(' */'); 121 | // Start Method 122 | $writer->write('public function ' . Inflector::pluralize($foreignKey->getOwningTable()->getRawTableName()) . '()'); 123 | $writer->write('{'); 124 | $writer->indent(); 125 | // One to Many 126 | if($foreignKey->isManyToOne()) { 127 | $writer->write('return $this->hasMany(\'' . $_this->getNamespace() . '\\' . $foreignKey->getOwningTable()->getModelName() . '\');'); 128 | } 129 | // One to One 130 | else { 131 | $writer->write('return $this->hasOne(\'' . $_this->getNamespace() . '\\' . $foreignKey->getOwningTable()->getModelName() . '\');'); 132 | } 133 | // End Method 134 | $writer->outdent(); 135 | $writer->write('}'); 136 | $writer->write(''); 137 | } 138 | } 139 | } else { 140 | if(count($table->getForeignKeys()) == 2) { 141 | // ForeignKey 1 142 | $foreignKey1 = $table->getForeignKeys()[0]; 143 | // ForeignKey 2 144 | $foreignKey2 = $table->getForeignKeys()[1]; 145 | 146 | // If current column is referenced by foreignKey 147 | if((($_this->getRawTableName() == $foreignKey1->getReferencedTable()->getRawTableName()) || 148 | ($_this->getRawTableName() == $foreignKey2->getReferencedTable()->getRawTableName())) && 149 | ($column->getColumnName() == $foreignKey1->getForeign()->getColumnName() || 150 | $column->getColumnName() == $foreignKey2->getForeign()->getColumnName())) { 151 | 152 | // Comment 153 | $writer->write('/**'); 154 | if($_this->getRawTableName() != $foreignKey1->getReferencedTable()->getRawTableName()) { 155 | $writer->write(' * Relationship with ' . $foreignKey1->getReferencedTable()->getModelName() . '.'); 156 | } else { 157 | $writer->write(' * Relationship with ' . $foreignKey2->getReferencedTable()->getModelName() . '.'); 158 | } 159 | $writer->write(' */'); 160 | 161 | // Method 162 | if($_this->getRawTableName() != $foreignKey1->getReferencedTable()->getRawTableName()) { 163 | $writer->write('public function ' . Inflector::pluralize($foreignKey1->getReferencedTable()->getRawTableName()) . '()'); 164 | } else { 165 | $writer->write('public function ' . Inflector::pluralize($foreignKey2->getReferencedTable()->getRawTableName()) . '()'); 166 | } 167 | 168 | $writer->write('{'); 169 | $writer->indent(); 170 | // Find out what foreignKey is this reference table and what the other table 171 | if($_this->getRawTableName() != $foreignKey1->getReferencedTable()->getRawTableName()) { 172 | $writer->write('return $this->belongsToMany(\'' . $_this->getNamespace() . '\\' . $foreignKey1->getReferencedTable()->getModelName() . '\', \'' . $foreignKey1->getOwningTable()->getRawTableName() . '\', \'' . $foreignKey2->getForeign()->getColumnName() . '\', \'' . $foreignKey1->getForeign()->getColumnName() . '\');'); 173 | } else { 174 | $writer->write('return $this->belongsToMany(\'' . $_this->getNamespace() . '\\' . $foreignKey2->getReferencedTable()->getModelName() . '\', \'' . $foreignKey2->getOwningTable()->getRawTableName() . '\', \'' . $foreignKey1->getForeign()->getColumnName() . '\', \'' . $foreignKey2->getForeign()->getColumnName() . '\');'); 175 | } 176 | $writer->outdent(); 177 | $writer->write('}'); 178 | $writer->write(''); 179 | } 180 | } 181 | } 182 | } 183 | } 184 | } 185 | }) 186 | ; 187 | 188 | return $this; 189 | } 190 | 191 | public function writeRelationships(WriterInterface $writer) 192 | { 193 | $writer 194 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 195 | if (count($_this->getForeignKeys())) { 196 | foreach ($_this->getForeignKeys() as $foreignKey) { 197 | $foreignKey->write($writer); 198 | } 199 | } 200 | }) 201 | ; 202 | 203 | return $this; 204 | } 205 | 206 | public function writeFillable(WriterInterface $writer) 207 | { 208 | /* 209 | * FIXME: identify which columns are FK and not add to the array fillable 210 | */ 211 | $writer 212 | ->write('/**') 213 | ->write(' * The attributes that are mass assignable.') 214 | ->write(' * ') 215 | ->write(' * @var array') 216 | ->write(' */') 217 | ->writeCallback(function(WriterInterface $writer, Table $_this = null) { 218 | if (count($_this->getColumns())) { 219 | $content = ''; 220 | $columns = $_this->getColumns(); 221 | foreach ($columns as $column) { 222 | $content .= '\'' . $column->getColumnName() . '\','; 223 | } 224 | $writer->write('protected $fillable = [' . substr($content, 0, -1) . '];'); 225 | } 226 | }) 227 | ; 228 | 229 | return $this; 230 | } 231 | } --------------------------------------------------------------------------------