58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Lean\Console\Lib;
|
|
|
|
class DatabaseMigrations
|
|
{
|
|
public array $folderList;
|
|
|
|
public array $entities;
|
|
|
|
public function __construct(array $folders = [])
|
|
{
|
|
$this->folderList = $folders;
|
|
}
|
|
|
|
public function getEntities() : void
|
|
{
|
|
$this->entities = [];
|
|
|
|
foreach($this->folderList as $namespace => $folder) {
|
|
foreach(static::files($folder) as $file) {
|
|
$name = $file->getBasename("." . $file->getExtension());
|
|
$subNs = substr($file->getPath(), strlen($folder));
|
|
$entity = rtrim($namespace, "\\") . ( $subNs ? "\\$subNs" : "" ) . "\\{$name}";
|
|
|
|
if ( ! method_exists($entity, 'resolveEntity') ) {
|
|
continue;
|
|
}
|
|
|
|
$this->entities[$entity] = $entity::resolveEntity();
|
|
}
|
|
}
|
|
|
|
ksort($this->entities);
|
|
}
|
|
|
|
protected static function files(string $path, string $fileExtension = "") : \Generator
|
|
{
|
|
if ( \file_exists($path) ) {
|
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST, \RecursiveIteratorIterator::CATCH_GET_CHILD);
|
|
|
|
foreach ($iterator as $file) {
|
|
if ( $file->isFile() || $file->isDir() ) {
|
|
if ($fileExtension) {
|
|
if ( $file->getExtension() === $fileExtension ) {
|
|
yield $file;
|
|
}
|
|
}
|
|
else {
|
|
if ( $file->isFile() ) {
|
|
yield $file;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |