Skip to content

Commit 73a5cd6

Browse files
authored
Merge pull request #1860 from MGatner/migrationrunner-use-autoloader
Migrationrunner use autoloader
2 parents 7f2b675 + 6a8a104 commit 73a5cd6

3 files changed

Lines changed: 58 additions & 21 deletions

File tree

system/Autoloader/FileLocator.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,48 @@ public function listFiles(string $path): array
376376

377377
//--------------------------------------------------------------------
378378

379+
/**
380+
* Scans the provided namespace, returning a list of all files
381+
* that are contained within the subpath specified by $path.
382+
*
383+
* @param string $prefix
384+
* @param string $path
385+
*
386+
* @return array
387+
*/
388+
public function listNamespaceFiles(string $prefix, string $path): array
389+
{
390+
if (empty($path) || empty($prefix))
391+
{
392+
return [];
393+
}
394+
395+
$files = [];
396+
helper('filesystem');
397+
398+
// autoloader->getNamespace($prefix) returns an array of paths for that namespace
399+
foreach ($this->autoloader->getNamespace($prefix) as $namespacePath)
400+
{
401+
$fullPath = realpath($namespacePath . $path);
402+
403+
if (! is_dir($fullPath))
404+
{
405+
continue;
406+
}
407+
408+
$tempFiles = get_filenames($fullPath, true);
409+
410+
if (! empty($tempFiles))
411+
{
412+
$files = array_merge($files, $tempFiles);
413+
}
414+
}
415+
416+
return $files;
417+
}
418+
419+
//--------------------------------------------------------------------
420+
379421
/**
380422
* Checks the application folder to see if the file can be found.
381423
* Only for use with filenames that DO NOT include namespacing.

system/Database/MigrationRunner.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @filesource
3737
*/
3838

39+
use Config\Services;
3940
use CodeIgniter\CLI\CLI;
4041
use CodeIgniter\Config\BaseConfig;
4142
use CodeIgniter\Exceptions\ConfigException;
@@ -346,11 +347,10 @@ public function latestAll(string $group = null)
346347
$this->setGroup($group);
347348
}
348349

349-
// Get all namespaces form PSR4 paths.
350-
$config = config('Autoload');
351-
$namespaces = $config->psr4;
350+
// Get all namespaces from the autoloader
351+
$namespaces = Services::autoloader()->getNamespace();
352352

353-
foreach ($namespaces as $namespace => $path)
353+
foreach ($namespaces as $namespace => $paths)
354354
{
355355
$this->setNamespace($namespace);
356356
$migrations = $this->findMigrations();
@@ -400,36 +400,30 @@ public function current(string $group = null)
400400
//--------------------------------------------------------------------
401401

402402
/**
403-
* Retrieves list of available migration scripts
403+
* Retrieves list of available migration scripts for one namespace
404404
*
405405
* @return array list of migrations as $version for one namespace
406406
*/
407407
public function findMigrations()
408408
{
409409
$migrations = [];
410-
helper('filesystem');
411410

412411
// If $this->path contains a valid directory use it.
413412
if (! empty($this->path))
414413
{
414+
helper('filesystem');
415415
$dir = rtrim($this->path, DIRECTORY_SEPARATOR) . '/';
416+
$files = get_filenames($dir, true);
416417
}
417-
// Otherwise, get namespace location form PSR4 paths
418-
// and add Database/Migrations for a standard location.
418+
// Otherwise use FileLocator to search files in the subdirectory of the namespace
419419
else
420420
{
421-
$config = config('Autoload');
422-
423-
$location = $config->psr4[$this->namespace];
424-
425-
// Setting migration directories.
426-
$dir = rtrim($location, DIRECTORY_SEPARATOR) . '/Database/Migrations/';
421+
$locator = Services::locator(true);
422+
$files = $locator->listNamespaceFiles($this->namespace, '/Database/Migrations/');
427423
}
428424

429425
// Load all *_*.php files in the migrations path
430426
// We can't use glob if we want it to be testable....
431-
$files = get_filenames($dir, true);
432-
433427
foreach ($files as $file)
434428
{
435429
if (substr($file, -4) !== '.php')
@@ -447,9 +441,9 @@ public function findMigrations()
447441
$migration = new \stdClass();
448442

449443
// Get migration version number
450-
$migration->version = $this->getMigrationNumber($name);
451-
$migration->name = $this->getMigrationName($name);
452-
$migration->path = ! empty($this->path) && strpos($file, $this->path) !== 0
444+
$migration->version = $this->getMigrationNumber($name);
445+
$migration->name = $this->getMigrationName($name);
446+
$migration->path = ! empty($this->path) && strpos($file, $this->path) !== 0
453447
? $this->path . $file
454448
: $file;
455449

user_guide_src/source/dbmgmt/migration.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ Namespaces
130130
==========
131131

132132
The migration library can automatically scan all namespaces you have defined within
133-
**app/Config/Autoload.php** and its ``$psr4`` property for matching directory
134-
names. It will include all migrations it finds in Database/Migrations.
133+
**app/Config/Autoload.php** or loaded from an external source like Composer, using
134+
the ``$psr4`` property for matching directory names. It will include all migrations
135+
it finds in Database/Migrations.
135136

136137
Each namespace has it's own version sequence, this will help you upgrade and downgrade each module (namespace) without affecting other namespaces.
137138

0 commit comments

Comments
 (0)