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
|
|
|
'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The console command description. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $description = 'Create a new addon directory'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $skeletons = [ |
45
|
|
|
1 => 'minimum', |
46
|
|
|
2 => 'simple', |
47
|
|
|
3 => 'library', |
48
|
|
|
4 => 'api', |
49
|
|
|
5 => 'ui', |
50
|
|
|
6 => 'debug', |
51
|
|
|
7 => 'generator', |
52
|
|
|
8 => 'laravel5', |
53
|
|
|
9 => 'sample:ui', |
54
|
|
|
10 => 'sample:auth', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $default_skeleton = 'sample:ui'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Execute the console command. |
64
|
|
|
* |
65
|
|
|
* @param \Jumilla\Addomnipot\Laravel\Addons\AddonGenerator $generator |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
3 |
|
public function handle(Filesystem $filesystem, AddonEnvironment $env, AddonGenerator $generator) |
70
|
|
|
{ |
71
|
3 |
|
$addon_name = preg_replace('#(/+)#', '-', $this->argument('name')); |
72
|
|
|
|
73
|
3 |
|
$output_path = $env->path($addon_name); |
|
|
|
|
74
|
|
|
|
75
|
3 |
|
if ($filesystem->exists($output_path)) { |
76
|
|
|
throw new UnexpectedValueException("addon directory '{$addon_name}' is already exists."); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
$skeleton = $this->chooseSkeleton($this->argument('skeleton')); |
|
|
|
|
80
|
3 |
|
if ($this->option('no-namespace')) { |
81
|
1 |
|
$namespace = ''; |
82
|
1 |
|
} else { |
83
|
2 |
|
if ($this->option('namespace')) { |
84
|
1 |
|
$namespace = str_replace('/', '\\', $this->option('namespace')); |
85
|
1 |
|
$namespace = studly_case(preg_replace('/[^\w_\\\\]/', '', $namespace)); |
|
|
|
|
86
|
1 |
|
} else { |
87
|
1 |
|
$namespace = studly_case(preg_replace('/[^\w_]/', '', $addon_name)); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
$namespace = preg_replace('/^(\d)/', '_$1', $namespace); |
91
|
|
|
} |
92
|
3 |
|
$languages = $this->option('language') ? explode($this->option('language')) : []; |
93
|
|
|
|
94
|
|
|
$properties = [ |
95
|
3 |
|
'addon_name' => preg_replace('/[^\w_]/', '', $addon_name), |
96
|
3 |
|
'addon_class' => preg_replace( |
97
|
3 |
|
['/[^\w_]/', '/^(\d)/'], |
98
|
3 |
|
['', '_$1'], |
99
|
3 |
|
studly_case($addon_name) |
|
|
|
|
100
|
3 |
|
), |
101
|
3 |
|
'namespace' => $this->option('no-namespace') ? '' : $namespace, |
102
|
3 |
|
'languages' => array_unique(array_merge(['en', $this->laravel['config']->get('app.locale')], $languages)), |
103
|
3 |
|
]; |
104
|
|
|
|
105
|
|
|
try { |
106
|
3 |
|
$generator->generateAddon($output_path, str_replace(':', '-', $skeleton), $properties); |
107
|
3 |
|
$this->info('Addon Generated.'); |
108
|
3 |
|
} catch (Exception $ex) { |
109
|
|
|
$filesystem->deleteDirectory($output_path); |
110
|
|
|
|
111
|
|
|
throw $ex; |
112
|
|
|
} |
113
|
3 |
|
} |
114
|
|
|
} |
115
|
|
|
|
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.