Issues (150)

migrations/Oauth2GenerateMigrationsAction.php (1 issue)

1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\console\migrations;
4
5
use rhertogh\Yii2Oauth2Server\controllers\console\migrations\base\Oauth2BaseGenerateMigrationsAction;
6
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2MigrationsController;
7
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\migrations\Oauth2GenerateMigrationsActionInterface;
8
use rhertogh\Yii2Oauth2Server\migrations\base\Oauth2BaseMigration;
9
use yii\base\Exception;
10
use yii\base\InvalidConfigException;
11
12
/**
13
 * @property Oauth2MigrationsController $controller
14
 */
15
class Oauth2GenerateMigrationsAction extends Oauth2BaseGenerateMigrationsAction implements
16
    Oauth2GenerateMigrationsActionInterface
17
{
18
    /**
19
     * Generate the migrations needed for the Oauth2 Server based on the current configuration.
20
     *
21
     * @return int
22
     * @throws InvalidConfigException
23
     * @throws Exception
24
     */
25 7
    public function run()
26
    {
27 7
        $sourcePath = $this->getMigrationsSourcePath();
28 7
        $sourceNamespace = $this->getMigrationsSourceNamespace();
29 7
        $sourceMigrationClasses = $this->getSourceMigrationClasses($sourcePath, $sourceNamespace);
30
31 7
        return $this->generateMigrations($sourceMigrationClasses);
32
    }
33
34
    /**
35
     * @param string $migrationPath
36
     * @param string $namespace
37
     * @return class-string<Oauth2BaseMigration>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Oauth2BaseMigration>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Oauth2BaseMigration>[].
Loading history...
38
     */
39 8
    protected function getSourceMigrationClasses($migrationPath, $namespace)
40
    {
41 8
        if (!file_exists($migrationPath)) {
42 1
            throw new InvalidConfigException('Source migration path "' . $migrationPath . '" does not exist.');
43
        }
44
45 7
        $migrations = [];
46 7
        $handle = opendir($migrationPath);
47 7
        while (($file = readdir($handle)) !== false) {
48 7
            if ($file === '.' || $file === '..') {
49 7
                continue;
50
            }
51 7
            $path = $migrationPath . DIRECTORY_SEPARATOR . $file;
52 7
            if (preg_match('/^(Oauth2_\d{5}_.+Migration)\.php$/is', $file, $matches) && is_file($path)) {
53 7
                $migrations[] = $namespace . '\\' . $matches[1];
54
            }
55
        }
56 7
        closedir($handle);
57
58 7
        sort($migrations);
59
60 7
        return $migrations;
61
    }
62
63 7
    protected function getMigrationsSourcePath()
64
    {
65 7
        $reflector = new \ReflectionClass($this->controller->module);
66 7
        $moduleFile = $reflector->getFileName();
67
68 7
        return dirname($moduleFile) . DIRECTORY_SEPARATOR . 'migrations';
69
    }
70
71 7
    protected function getMigrationsSourceNamespace()
72
    {
73 7
        $reflector = new \ReflectionClass($this->controller->module);
74 7
        return $reflector->getNamespaceName() . '\\migrations';
75
    }
76
}
77