From b9c848f883de7e99380b7a81819173984a381d9f Mon Sep 17 00:00:00 2001 From: repat Date: Thu, 27 Feb 2020 03:45:11 +0700 Subject: [PATCH] 0.2 --- README.md | 8 ++++++- composer.json | 2 +- src/Repat/Laravel/Migration.php | 40 +++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7509872..10bc65e 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,19 @@ $migration->filename; // Methods $migration->fileExists(); + +// list all files from migrations folder +$migrations = Migration::listFiles(); + +// list all files from migrations folder as migration name (without '.php') +$migrations = Migration::listFiles(true); ``` ## License * MIT, see [LICENSE](https://github.com/repat/laravel-migration-model/blob/master/LICENSE) ## Version -* Version 0.1 +* Version 0.2 ## Contact #### repat diff --git a/composer.json b/composer.json index 2571d21..1c611b2 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "keywords": ["laravel", "eloquent", "migrations", "model", "migration", "artisan"], "homepage": "https://repat.de", "license": "MIT", - "version" : "0.1", + "version" : "0.2", "authors": [ {"name": "repat", "email": "repat@repat.de"} ], diff --git a/src/Repat/Laravel/Migration.php b/src/Repat/Laravel/Migration.php index e071376..1f344fa 100644 --- a/src/Repat/Laravel/Migration.php +++ b/src/Repat/Laravel/Migration.php @@ -4,6 +4,20 @@ class Migration extends \Illuminate\Database\Eloquent\Model { + /** + * Migrations folder inside of laravel app + * + * @var string + */ + const FOLDER = 'database/migrations/'; + + /** + * File extension + * + * @var string + */ + const EXT = '.php'; + /** * Table is not timestamped * @@ -25,7 +39,7 @@ class Migration extends \Illuminate\Database\Eloquent\Model */ public function fileExists() : bool { - return file_exists(base_path('database/migrations/' . $this->filename)); + return file_exists(base_path(self::FOLDER . $this->filename)); } /** @@ -35,6 +49,28 @@ public function fileExists() : bool */ public function getFilenameAttribute() : string { - return $this->migration . '.php'; + return $this->migration . self::EXT; + } + + /** + * Lists all files in the migrations folder + * + * @param bool $asMigration + * @return array + */ + public static function listFiles(bool $asMigration = false) : array + { + $files = array_values( + array_filter( + scandir(base_path(self::FOLDER)), + function ($file) { + return $file !== '.' && $file !== '..'; + } + ) + ); + return $asMigration ? + array_map(function (string $file) { + return str_replace(self::EXT, '', $file); + }, $files) : $files; } }