diff --git a/.push b/.push new file mode 100644 index 00000000..32969afb --- /dev/null +++ b/.push @@ -0,0 +1,25 @@ +1. Confirma y sube tus cambios a tu repositorio de GitHub: + +bash +git add . +git commit -m "Descripción de los cambios" +git push origin master +(Asegúrate de estar en la rama principal/master o la rama que uses como estable). + +2. Actualiza la versión en tu archivo composer.json: + +Cambia el valor de "version" (si lo tienes) o agrega una nueva etiqueta/tag de versión en GitHub (recomendado). +3. Crea un nuevo tag de versión: + +bash +git tag v1.0.1 +git push origin v1.0.1 +(Reemplaza v1.0.1 por la versión correspondiente según tu ciclo de versiones.) + +4. Verifica que tu repositorio es público y tiene un composer.json válido. + +5. Si aún no lo has hecho, publica tu paquete en Packagist: + +Ve a https://packagist.org/packages/submit +Ingresa la URL de tu repositorio de GitHub y sigue los pasos. +Para actualizaciones futuras, Packagist detecta automáticamente los nuevos tags y actualiza tu paquete. Si no lo hace, puedes forzar la actualización desde la página de tu paquete en Packagist. diff --git a/README.md b/README.md index 9bb50525..053d89b7 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,4 @@ -# Reliese Laravel - -[![StyleCI](https://styleci.io/repos/71080508/shield?style=flat)](https://styleci.io/repos/71080508) -[![Build Status](https://travis-ci.org/reliese/laravel.svg?branch=master)](https://travis-ci.org/reliese/laravel) -[![Latest Stable Version](https://poser.pugx.org/reliese/laravel/v/stable)](https://packagist.org/packages/reliese/laravel) -[![Total Downloads](https://poser.pugx.org/reliese/laravel/downloads)](https://packagist.org/packages/reliese/laravel) -[![Latest Unstable Version](https://poser.pugx.org/reliese/laravel/v/unstable)](https://packagist.org/packages/reliese/laravel) -[![License](https://poser.pugx.org/reliese/laravel/license)](https://packagist.org/packages/reliese/laravel) +# AJULCAB Reliese Laravel Reliese Laravel is a collection of Laravel Components which aim is to help the development process of Laravel applications by @@ -14,10 +7,10 @@ providing some convenient code-generation capabilities. ## How does it work? This package expects that you are using Laravel 5.1 or above. -You will need to import the `reliese/laravel` package via composer: +You will need to import the `ajulcab/laravel-generate-model` package via composer: ```shell -composer require reliese/laravel +composer require ajulcab/laravel-generate-model ``` ### Configuration @@ -40,7 +33,7 @@ Add the service provider to your `config/app.php` file within the `providers` ke If you wish to enable generators only for your local environment, you should install it via composer using the --dev option like this: ```shell -composer require reliese/laravel --dev +composer require ajulcab/laravel-generate-model --dev ``` Then you'll need to register the provider in `app/Providers/AppServiceProvider.php` file. @@ -115,4 +108,4 @@ to decide whether this approach gives value to your project :-) #### Support -For the time being, this package only supports MySQL databases. Support for other databases will be added soon. +For the time being, this package supports MySQL, SqlSrv, Postgress and SQLite databases. diff --git a/_config.yml b/_config.yml new file mode 100644 index 00000000..b8497135 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-leap-day \ No newline at end of file diff --git a/composer.json b/composer.json index 3d3678be..89f81a1c 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,17 @@ { - "name": "reliese/laravel", + "name": "ajulcab/laravel-generate-model", "description": "Reliese Components for Laravel Framework code generation.", - "keywords": ["reliese", "laravel"], - "homepage": "http://cristianllanos.com", + "keywords": ["reliese", "laravel","generate","model","ajulcab"], + "homepage": "http://infotiks.com", "support": { - "issues": "https://github.com/reliese/laravel/issues", - "source": "https://github.com/reliese/laravel" + "issues": "https://github.com/AJULCAB/generate-model/issues", + "source": "https://github.com/AJULCAB/generate-model" }, "license": "MIT", "authors": [ { - "name": "Cristian Llanos", - "email": "cristianllanos@outlook.com" + "name": "Ahiezer Julca Blas", + "email": "julca.blas@gmail.com" } ], "require": { diff --git a/config/models.php b/config/models.php index 5f97d5bf..1060aded 100644 --- a/config/models.php +++ b/config/models.php @@ -280,7 +280,14 @@ */ 'except' => [ + 'cache', + 'cache_locks', + 'failed_jobs', + 'jobs', + 'job_batches', 'migrations', + 'password_reset_tokens', + 'sessions', ], /* @@ -374,7 +381,7 @@ | You can disable pluralization tables and relations | */ - 'pluralize' => true, + 'pluralize' => false, /* |-------------------------------------------------------------------------- diff --git a/src/Meta/MySql/Schema.php b/src/Meta/MySql/Schema.php index 186010f7..e857bbc1 100644 --- a/src/Meta/MySql/Schema.php +++ b/src/Meta/MySql/Schema.php @@ -270,8 +270,11 @@ protected function resolveForeignTable($table, Blueprint $blueprint) */ public static function schemas(Connection $connection) { - $schemas = $connection->getDoctrineSchemaManager()->listDatabases(); + //$schemas = $connection->getDoctrineSchemaManager()->listDatabases(); + $pdo = $connection->getPdo(); + $query = $pdo->query("SHOW DATABASES"); + $schemas = $query->fetchAll(\PDO::FETCH_COLUMN); return array_diff($schemas, [ 'information_schema', 'sys', diff --git a/src/Meta/SchemaManager.php b/src/Meta/SchemaManager.php index ba98da05..f37398fe 100644 --- a/src/Meta/SchemaManager.php +++ b/src/Meta/SchemaManager.php @@ -13,11 +13,12 @@ use Illuminate\Database\MySqlConnection; use Illuminate\Database\SQLiteConnection; use Illuminate\Database\PostgresConnection; +use Illuminate\Database\SqlServerConnection; use Illuminate\Database\ConnectionInterface; use Reliese\Meta\MySql\Schema as MySqlSchema; use Reliese\Meta\Sqlite\Schema as SqliteSchema; use Reliese\Meta\Postgres\Schema as PostgresSchema; - +use Reliese\Meta\SqlSrv\Schema as SqlSrvSchema; class SchemaManager implements IteratorAggregate { /** @@ -28,6 +29,7 @@ class SchemaManager implements IteratorAggregate SQLiteConnection::class => SqliteSchema::class, PostgresConnection::class => PostgresSchema::class, \Larapack\DoctrineSupport\Connections\MySqlConnection::class => MySqlSchema::class, + SqlServerConnection::class=>SqlSrvSchema::class, ]; /** diff --git a/src/Meta/SqlSrv/Column.php b/src/Meta/SqlSrv/Column.php new file mode 100644 index 00000000..cd3c656f --- /dev/null +++ b/src/Meta/SqlSrv/Column.php @@ -0,0 +1,122 @@ + ['varchar','char','nvarchar','nchar','string','varchar(MAX)'], + 'date' => ['datetime', 'datetime2', 'date'], + 'int' => ['bigint', 'int', 'tinyint', 'smallint','integer'], + 'float' => ['float', 'decimal','money','smallmoney'], + 'real' =>['real'], + 'boolean' => ['bit','bool','boolean'], + ]; + + /** + * MysqlColumn constructor. + * + * @param array $metadata + */ + public function __construct($metadata = []) + { + $this->metadata = $metadata; + } + + /** + * @return \Illuminate\Support\Fluent + */ + public function normalize() + { + $attributes = new Fluent(); + + foreach ($this->metas as $meta) { + $this->{'parse'.ucfirst($meta)}($attributes); + } + + return $attributes; + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseType(Fluent $attributes) + { + + + $dataType = $this->metadata->getType()->getName(); + + foreach (static::$mappings as $phpType => $database) { + if (in_array($dataType, $database)) { + $attributes['type'] = $phpType; + } + } + + /* + if ($attributes['type'] == 'int') { + $attributes['unsigned'] = $this->metadata->getUnsigned(); + }*/ + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseName(Fluent $attributes) + { + $attributes['name'] = $this->metadata->getName(); + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseAutoincrement(Fluent $attributes) + { + $attributes['autoincrement'] = $this->metadata->getAutoincrement(); + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseNullable(Fluent $attributes) + { + $attributes['nullable'] = $this->metadata->getNotnull(); + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseDefault(Fluent $attributes) + { + $attributes['default'] = $this->metadata->getDefault(); + } + + /** + * @param \Illuminate\Support\Fluent $attributes + */ + protected function parseComment(Fluent $attributes) + { + $attributes['comment'] = $this->metadata->getComment(); + } +} diff --git a/src/Meta/SqlSrv/Schema.php b/src/Meta/SqlSrv/Schema.php new file mode 100644 index 00000000..8d04ba5d --- /dev/null +++ b/src/Meta/SqlSrv/Schema.php @@ -0,0 +1,279 @@ +schema = $schema; + $this->connection = $connection; + /* Sqlite has a bool type that doctrine isn't registering */ + $this->connection->getDoctrineConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('bool', 'boolean'); + $this->connection->getDoctrineConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('xml', 'string'); + $this->load(); + } + + /** + * @return \Doctrine\DBAL\Schema\AbstractSchemaManager + * @todo: Use Doctrine instead of raw database queries + */ + public function manager() + { + return $this->connection->getDoctrineSchemaManager(); + } + + /** + * Loads schema's tables' information from the database. + */ + protected function load() + { + $tables = $this->fetchTables(); + + foreach ($tables as $table) { + $blueprint = new Blueprint($this->connection->getName(), $this->schema, $table); + $this->fillColumns($blueprint); + $this->fillConstraints($blueprint); + if ($blueprint->primaryKey()){ + $this->tables[$table] = $blueprint; + } + } + } + + /** + * @return array + * @internal param string $schema + */ + protected function fetchTables() + { + $names = $this->manager()->listTableNames(); + + return array_diff($names, [ + 'sqlite_master', + 'sqlite_sequence', + 'sqlite_stat1', + ]); + } + + /** + * @param \Reliese\Meta\Blueprint $blueprint + */ + protected function fillColumns(Blueprint $blueprint) + { + $columns = $this->manager()->listTableColumns($blueprint->table()); + + foreach ($columns as $column) { + $blueprint->withColumn( + $this->parseColumn($column) + ); + } + } + + /** + * @param \Doctrine\DBAL\Schema\Column $metadata + * + * @return \Illuminate\Support\Fluent + */ + protected function parseColumn($metadata) + { + return (new Column($metadata))->normalize(); + } + + /** + * @param \Reliese\Meta\Blueprint $blueprint + */ + protected function fillConstraints(Blueprint $blueprint) + { + $this->fillPrimaryKey($blueprint); + $this->fillIndexes($blueprint); + + $this->fillRelations($blueprint); + } + + /** + * Quick little hack since it is no longer possible to set PDO's fetch mode + * to PDO::FETCH_ASSOC. + * + * @param $data + * @return mixed + */ + protected function arraify($data) + { + return json_decode(json_encode($data), true); + } + + /** + * @param \Reliese\Meta\Blueprint $blueprint + * @todo: Support named primary keys + */ + protected function fillPrimaryKey(Blueprint $blueprint) + { + $indexes = $this->manager()->listTableIndexes($blueprint->table()); + + if (count($indexes)>0){ + $key = [ + 'name' => 'primary', + 'index' => '', + 'columns' => $indexes['primary']->getColumns(), + ]; + + $blueprint->withPrimaryKey(new Fluent($key)); + } + + } + + /** + * @param \Reliese\Meta\Blueprint $blueprint + * @internal param string $sql + */ + protected function fillIndexes(Blueprint $blueprint) + { + $indexes = $this->manager()->listTableIndexes($blueprint->table()); + unset($indexes['primary']); + + foreach ($indexes as $setup) { + $index = [ + 'name' => $setup->isUnique() ? 'unique' : 'index', + 'columns' => $setup->getColumns(), + 'index' => $setup->getName(), + ]; + $blueprint->withIndex(new Fluent($index)); + } + } + + /** + * @param \Reliese\Meta\Blueprint $blueprint + * @todo: Support named foreign keys + */ + protected function fillRelations(Blueprint $blueprint) + { + $relations = $this->manager()->listTableForeignKeys($blueprint->table()); + + foreach ($relations as $setup) { + $table = ['database' => '', 'table'=>$setup->getForeignTableName()]; + + $relation = [ + 'name' => 'foreign', + 'index' => '', + 'columns' => $setup->getColumns(), + 'references' => $setup->getForeignColumns(), + 'on' => $table, + ]; + + $blueprint->withRelation(new Fluent($relation)); + } + } + + /** + * @param \Illuminate\Database\Connection $connection + * + * @return array + */ + public static function schemas(Connection $connection) + { + return ['database']; + } + + /** + * @return string + */ + public function schema() + { + return $this->schema; + } + + /** + * @param string $table + * + * @return bool + */ + public function has($table) + { + return array_key_exists($table, $this->tables); + } + + /** + * @return \Reliese\Meta\Blueprint[] + */ + public function tables() + { + return $this->tables; + } + + /** + * @param string $table + * + * @return \Reliese\Meta\Blueprint + */ + public function table($table) + { + if (! $this->has($table)) { + throw new \InvalidArgumentException("Table [$table] does not belong to schema [{$this->schema}]"); + } + + return $this->tables[$table]; + } + + /** + * @return \Illuminate\Database\MySqlConnection + */ + public function connection() + { + return $this->connection; + } + + /** + * @param \Reliese\Meta\Blueprint $table + * + * @return array + */ + public function referencing(Blueprint $table) + { + $references = []; + + foreach ($this->tables as $blueprint) { + foreach ($blueprint->references($table) as $reference) { + $references[] = [ + 'blueprint' => $blueprint, + 'reference' => $reference, + ]; + } + } + + return $references; + } +}