1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jumilla\Addomnipot\Laravel\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Jumilla\Addomnipot\Laravel\Environment as AddonEnvironment; |
8
|
|
|
use Jumilla\Addomnipot\Laravel\Generator as AddonGenerator; |
9
|
|
|
use UnexpectedValueException; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Modules console commands. |
14
|
|
|
* |
15
|
|
|
* @author Fumio Furukawa <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class AddonMakeCommand extends Command |
18
|
|
|
{ |
19
|
|
|
use MakeCommandTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command signature. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $signature = 'make:addon |
27
|
|
|
{name : The name of the addon.} |
28
|
|
|
{skeleton? : Skeleton of addon.} |
29
|
|
|
{--namespace= : PHP namespace of addon. Slash OK.} |
30
|
|
|
{--no-namespace : No PHP namespace.} |
31
|
|
|
{--language= : Languages, comma separated.} |
32
|
|
|
{--yes : No confirm.} |
33
|
|
|
'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The console command description. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $description = 'Create a new addon directory'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $skeletons = [ |
46
|
|
|
1 => 'minimum', |
47
|
|
|
2 => 'simple', |
48
|
|
|
3 => 'library', |
49
|
|
|
4 => 'api', |
50
|
|
|
5 => 'ui', |
51
|
|
|
6 => 'debug', |
52
|
|
|
7 => 'generator', |
53
|
|
|
8 => 'laravel5', |
54
|
|
|
9 => 'sample:ui', |
55
|
|
|
10 => 'sample:auth', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $default_skeleton = 'sample:ui'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Execute the console command. |
65
|
|
|
* |
66
|
|
|
* @param \Jumilla\Addomnipot\Laravel\Addons\AddonGenerator $generator |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
3 |
|
*/ |
70
|
|
|
public function handle(Filesystem $filesystem, AddonEnvironment $env, AddonGenerator $generator) |
71
|
3 |
|
{ |
72
|
|
|
$addon_name = preg_replace('#(/+)#', '-', $this->argument('name')); |
73
|
3 |
|
|
74
|
|
|
$output_path = $env->path($addon_name); |
|
|
|
|
75
|
3 |
|
|
76
|
|
|
if ($filesystem->exists($output_path)) { |
77
|
|
|
throw new UnexpectedValueException("addon directory '{$addon_name}' is already exists."); |
78
|
|
|
} |
79
|
3 |
|
|
80
|
3 |
|
$skeleton = $this->chooseSkeleton($this->argument('skeleton')); |
|
|
|
|
81
|
1 |
|
|
82
|
1 |
|
if ($this->option('no-namespace')) { |
83
|
2 |
|
$namespace = ''; |
84
|
1 |
|
} else { |
85
|
1 |
|
if ($this->option('namespace')) { |
86
|
1 |
|
$namespace = str_replace('/', '\\', $this->option('namespace')); |
87
|
1 |
|
$namespace = studly_case(preg_replace('/[^\w_\\\\]/', '', $namespace)); |
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
$namespace = 'App\\'.studly_case(preg_replace('/[^\w_]/', '', $addon_name)); |
|
|
|
|
90
|
2 |
|
} |
91
|
|
|
|
92
|
3 |
|
$namespace = preg_replace('/^(\d)/', '_$1', $namespace); |
93
|
|
|
} |
94
|
|
|
$languages = $this->option('language') ? explode($this->option('language')) : []; |
95
|
3 |
|
|
96
|
3 |
|
$properties = [ |
97
|
3 |
|
'addon_name' => preg_replace('/[^\w_]/', '', $addon_name), |
98
|
3 |
|
'addon_class' => preg_replace( |
99
|
3 |
|
['/[^\w_]/', '/^(\d)/'], |
100
|
3 |
|
['', '_$1'], |
101
|
3 |
|
studly_case($addon_name) |
|
|
|
|
102
|
3 |
|
), |
103
|
3 |
|
'namespace' => $namespace, |
104
|
|
|
'languages' => array_unique(array_merge(['en', $this->laravel['config']->get('app.locale')], $languages)), |
105
|
|
|
]; |
106
|
3 |
|
|
107
|
3 |
|
// confirm |
108
|
3 |
|
$this->line('Addon name: '.$properties['addon_name']); |
109
|
|
|
$this->line('PHP namespace: '.$properties['namespace']); |
110
|
|
|
$this->line('Skeleton: '.$skeleton); |
111
|
|
|
$this->line('Languages: '.implode(', ', $properties['languages'])); |
112
|
|
|
|
113
|
3 |
View Code Duplication |
if (!$this->option('yes') && !$this->confirm('Are you sure? [y/N]', false)) { |
|
|
|
|
114
|
|
|
$this->comment('canceled'); |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
try { |
119
|
|
|
$generator->generateAddon($output_path, str_replace(':', '-', $skeleton), $properties); |
120
|
|
|
$this->info('Addon Generated.'); |
121
|
|
|
} catch (Exception $ex) { |
122
|
|
|
$filesystem->deleteDirectory($output_path); |
123
|
|
|
|
124
|
|
|
throw $ex; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.