/
MigrationLocator.php
88 lines (76 loc) · 2.61 KB
/
MigrationLocator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/UserFrosting
* @copyright Copyright (c) 2019 Alexander Weissman
* @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Sprinkle\Core\Database\Migrator;
use Illuminate\Support\Str;
use UserFrosting\UniformResourceLocator\Resource as ResourceInstance;
use UserFrosting\UniformResourceLocator\ResourceLocator;
/**
* MigrationLocator Class.
*
* Finds all migrations class in a given sprinkle
*
* @author Louis Charette
*/
class MigrationLocator implements MigrationLocatorInterface
{
/**
* @var ResourceLocator The locator service
*/
protected $locator;
/**
* @var string The resource locator migration scheme
*/
protected $scheme = 'migrations://';
/**
* Class Constructor.
*
* @param ResourceLocator $locator The locator services
*/
public function __construct(ResourceLocator $locator)
{
$this->locator = $locator;
}
/**
* Loop all the available sprinkles and return a list of their migrations.
*
* @return array A list of all the migration files found for every sprinkle
*/
public function getMigrations()
{
$migrationFiles = $this->locator->listResources($this->scheme, false, false);
$migrations = [];
foreach ($migrationFiles as $migrationFile) {
// Note that PSR4 insists that all php files must end in PHP, so ignore all
// files that don't end in PHP.
if ($migrationFile->getExtension() == 'php') {
$migrations[] = $this->getMigrationDetails($migrationFile);
}
}
return $migrations;
}
/**
* Return an array of migration details inclusing the classname and the sprinkle name.
*
* @param ResourceInstance $file The migration file
*
* @return string The migration full class path
*/
protected function getMigrationDetails(ResourceInstance $file)
{
// Format the sprinkle name for the namespace
$sprinkleName = $file->getLocation()->getName();
$sprinkleName = Str::studly($sprinkleName);
// Getting base path, name and classname
$basePath = str_replace($file->getBasename(), '', $file->getBasePath());
$name = $basePath . $file->getFilename();
$className = str_replace('/', '\\', $basePath) . $file->getFilename();
// Build the class name and namespace
return "\\UserFrosting\\Sprinkle\\$sprinkleName\\Database\\Migrations\\$className";
}
}