Completed
Push — master ( 284085...99e334 )
by Fumio
02:01
created

sources/Console/AddonMakeCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Functions;
20
    use MakeCommandTrait;
21
22
    /**
23
     * The console command signature.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'make:addon
28
        {name : The name of the addon.}
29
        {skeleton? : Skeleton of addon.}
30
        {--space= : Addons space.}
31
        {--namespace= : PHP namespace of addon. Slash OK.}
32
        {--no-namespace : No PHP namespace.}
33
        {--language= : Languages, comma separated.}
34
        {--yes : No confirm.}
35
    ';
36
37
    /**
38
     * The console command description.
39
     *
40
     * @var string
41
     */
42
    protected $description = 'Create a new addon directory';
43
44
    /**
45
     * @var array
46
     */
47
    protected $skeletons = [
48
        1 => 'minimum',
49
        2 => 'simple',
50
        3 => 'asset',
51
        4 => 'library',
52
        5 => 'api',
53
        6 => 'ui',
54
        11 => 'ui-sample',
55
        12 => 'debug',
56
        13 => 'generator',
57
        14 => 'laravel5',
58
        15 => 'laravel5-auth',
59
    ];
60
61
    /**
62
     * @var string
63
     */
64
    protected $default_skeleton = 'ui-sample';
65
66
    /**
67
     * Execute the console command.
68
     *
69
     * @param \Jumilla\Addomnipot\Laravel\Addons\AddonGenerator $generator
70
     *
71
     * @return mixed
72
     */
73 3
    public function handle(Filesystem $filesystem, AddonEnvironment $env, AddonGenerator $generator)
74
    {
75 3
        $addon_name = preg_replace('#(/+)#', '-', $this->argument('name'));
76
77
        // Check addon-directory
78 3
        if ($env->exists($addon_name)) {
79
            throw new UnexpectedValueException("addon directory '{$addon_name}' is already exists.");
80
        }
81
82 3
        $output_path = $env->spacePath($this->option('space'), $addon_name);
83
84
        // Adjust addon_name
85 3
        $addon_name = preg_replace('/[^\w_\-]/', '', $addon_name);
86
87 3
        $addon_class = preg_replace(
88 3
            ['/[^\w_]/', '/^(\d)/'],
89 3
            ['', '_$1'],
90 3
            studly_case($addon_name)
91 3
        );
92
93
        // namespace
94 3
        if ($this->option('no-namespace')) {
95 1
            $namespace = '';
96 1
        } else {
97 2
            if ($this->option('namespace')) {
98 1
                $namespace = str_replace('/', '\\', $this->option('namespace'));
99 1
            } else {
100 1
                $namespace = 'App\\'.$addon_class;
101
            }
102
103 2
            if (! $this->validPhpNamespace($namespace)) {
104
                throw new UnexpectedValueException("PHP namespace '{$namespace}' is invalid.");
105
            }
106
        }
107
108
        // languages
109 3
        $languages = $this->option('language') ? explode($this->option('language')) : [];
110
111
        // Show select prompt if not specified
112 3
        $skeleton = $this->chooseSkeleton($this->argument('skeleton'));
0 ignored issues
show
It seems like $this->argument('skeleton') targeting Illuminate\Console\Command::argument() can also be of type array; however, Jumilla\Addomnipot\Larav...Trait::chooseSkeleton() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
113
114
        $properties = [
115 3
            'addon_name' => $addon_name,
116 3
            'addon_class' => $addon_class,
117 3
            'namespace' => $namespace,
118 3
            'languages' => array_unique(array_merge(['en', $this->laravel['config']->get('app.locale')], $languages)),
119 3
        ];
120
121
        // confirm
122 3
        $this->line('Addon name: '.$properties['addon_name']);
123 3
        $this->line('PHP namespace: '.$properties['namespace']);
124 3
        $this->line('Skeleton: '.$skeleton);
125 3
        $this->line('Languages: '.implode(', ', $properties['languages']));
126
127 3 View Code Duplication
        if (!$this->option('yes') && !$this->confirm('generate ready? [Y/n]', true)) {
128
            $this->comment('canceled');
129
            return;
130
        }
131
132
        try {
133 3
            $generator->generateAddon($output_path, str_replace(':', '-', $skeleton), $properties);
134 3
            $this->info('Addon Generated.');
135 3
        } catch (Exception $ex) {
136
            $filesystem->deleteDirectory($output_path);
137
138
            throw $ex;
139
        }
140 3
    }
141
}
142