Skip to content

Commit

Permalink
0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Feb 26, 2020
1 parent 13a43e7 commit b9c848f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -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"}
],
Expand Down
40 changes: 38 additions & 2 deletions src/Repat/Laravel/Migration.php
Expand Up @@ -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
*
Expand All @@ -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));
}

/**
Expand All @@ -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;
}
}

0 comments on commit b9c848f

Please sign in to comment.