1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelModulize\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Console\GeneratorCommand; |
8
|
|
|
use LaravelModulize\Contracts\ModulizerRepositoryInterface; |
9
|
|
|
|
10
|
|
|
abstract class BaseGeneratorCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Instance of the repository |
14
|
|
|
* |
15
|
|
|
* @var \LaravelModulize\Contracts\ModulizerRepositoryInterface |
16
|
|
|
*/ |
17
|
|
|
protected $repository; |
18
|
|
|
|
19
|
|
|
protected $module; |
20
|
|
|
protected $modulePath; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new command instance. |
24
|
|
|
* |
25
|
|
|
* @param \LaravelModulize\Contracts\ModulizerRepositoryInterface $repository |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Filesystem $files, ModulizerRepositoryInterface $repository) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($files); |
31
|
|
|
|
32
|
|
|
$this->repository = $repository; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function handle() |
41
|
|
|
{ |
42
|
|
|
$this->module = $this->argument('module'); |
43
|
|
|
$this->modulePath = $this->repository->getModulePath($this->module); |
|
|
|
|
44
|
|
|
|
45
|
|
|
if (!$this->repository->filesExist($this->modulePath)) { |
46
|
|
|
$this->call('modulize:make:module', [ |
47
|
|
|
'module' => $this->module, |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
parent::handle(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getClassName($name) |
55
|
|
|
{ |
56
|
|
|
return Str::contains($name, '/') |
57
|
|
|
? array_reverse(explode('/', $name, 2))[0] |
58
|
|
|
: $name; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function getClassNamespace($name) |
62
|
|
|
{ |
63
|
|
|
return Str::contains($name, '/') |
64
|
|
|
? $this->convertPathToNamespace($name) |
65
|
|
|
: ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function convertPathToNamespace($name) |
69
|
|
|
{ |
70
|
|
|
return Str::start(str_replace('/', '', $this->getPathBeforeClassName($name)), '\\'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getPathBeforeClassName($name) |
74
|
|
|
{ |
75
|
|
|
return Str::before($name, $this->getClassName($name)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|