1: <?php
2:
3: namespace Aurora\System\Console\Commands;
4:
5: use Symfony\Component\Console\Command\Command;
6: use Illuminate\Support\Facades\File;
7: use Symfony\Component\Finder\Finder;
8: use Symfony\Component\Finder\SplFileInfo;
9:
10: class BaseCommand extends Command
11: {
12: /**
13: * Get all of the migration paths.
14: *
15: * @return array
16: */
17: protected function getMigrationPaths($input)
18: {
19: // Here, we will check to see if a path option has been defined. If it has we will
20: // use the path relative to the root of the installation folder so our database
21: // migrations may be run for any customized path from within the application.
22: if ($input->getOption('path')) {
23: return collect($input->getOption('path'))->map(function ($path) use ($input) {
24: return !$this->usingRealPath($input)
25: ? \Aurora\Api::RootPath() . $path
26: : $path;
27: })->all();
28: }
29:
30: return array_merge(
31: [$this->getSystemMigrationsPath()],
32: $this->migrator->paths(), // @phpstan-ignore-line
33: $this->getMigrationPath()
34: );
35: }
36:
37: /**
38: * Determine if the given path(s) are pre-resolved "real" paths.
39: *
40: * @return bool
41: */
42: protected function usingRealPath($input)
43: {
44: return $input->hasOption('realpath') && $input->getOption('realpath');
45: }
46:
47: /**
48: * Get the path to the migration directory.
49: *
50: * @param null $sRequiredModule
51: * @return string[]|string
52: */
53: protected function getMigrationPath($sRequiredModule = null)
54: {
55: if ($sRequiredModule) {
56: if ($sRequiredModule === 'system') {
57: $sPath = $this->getSystemMigrationsPath();
58: } else {
59: $sPath = \Aurora\Api::GetModuleManager()->GetModulePath($sRequiredModule) . $sRequiredModule . DIRECTORY_SEPARATOR . 'Migrations';
60: }
61:
62: if (!file_exists($sPath)) {
63: mkdir($sPath, 0755, true);
64: }
65:
66: return $sPath;
67: } else {
68: $aModules = \Aurora\Api::GetModuleManager()->GetModulesPaths();
69: return array_map(function ($sModule, $sPath) {
70: return $sPath . $sModule . DIRECTORY_SEPARATOR . 'Migrations';
71: }, array_keys($aModules), $aModules);
72: }
73: }
74:
75: public function getAllModels()
76: {
77: $finder = Finder::create();
78: $aModules = \Aurora\Api::GetModuleManager()->GetModulesPaths();
79: $aModulesModelsPaths = array_map(function ($sModule, $sPath) {
80: return $sPath . $sModule . DIRECTORY_SEPARATOR . 'Models';
81: }, array_keys($aModules), $aModules);
82:
83: $aDirModels = [];
84: foreach ($aModulesModelsPaths as $sModuleModel) {
85: if (is_dir($sModuleModel)) {
86: $finder
87: ->in($sModuleModel)
88: ->depth(0);
89: $aDirModels = array_keys(\iterator_to_array($finder));
90: }
91: }
92:
93: $aFormatedDirModels = [];
94: foreach ($aDirModels as $sDirModel) {
95: $fileWithoutExtenstion = explode('.', $sDirModel);
96: array_pop($fileWithoutExtenstion);
97: $fileWithoutExtenstion = implode('.', $fileWithoutExtenstion);
98: $sModelName = explode(DIRECTORY_SEPARATOR, $fileWithoutExtenstion);
99: $sModelName = array_pop($sModelName);
100: $aFormatedDirModels[$sModelName] = $fileWithoutExtenstion;
101: }
102: return $aFormatedDirModels;
103: }
104:
105: /**
106: * @return string
107: */
108: private function getSystemMigrationsPath()
109: {
110: return \Aurora\Api::RootPath() . DIRECTORY_SEPARATOR . 'Migrations';
111: }
112: }
113: