Completed
Push — master ( 86ed53...02bbd5 )
by Oliver
14s queued 12s
created

MakeTeamwork::getNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Mpociot\Teamwork\Commands;
4
5
use Illuminate\Console\Command;
6
7
class MakeTeamwork extends Command
8
{
9
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'make:teamwork {--views : Only scaffold the teamwork views}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Create Teamwork scaffolding files.';
23
24
    protected $views = [
25
        'emails/invite.blade.php' => 'teamwork/emails/invite.blade.php',
26
        'members/list.blade.php' => 'teamwork/members/list.blade.php',
27
        'create.blade.php' => 'teamwork/create.blade.php',
28
        'edit.blade.php' => 'teamwork/edit.blade.php',
29
        'index.blade.php' => 'teamwork/index.blade.php',
30
    ];
31
32
    /**
33
     * Create a new command instance.
34
     *
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return mixed
46
     */
47
    public function handle()
48
    {
49
        $this->createDirectories();
50
51
        $this->exportViews();
52
53
        if (! $this->option('views')) {
54
            $this->info('Installed TeamController.');
55
            file_put_contents(
56
                app_path('Http/Controllers/Teamwork/TeamController.php'),
57
                $this->compileControllerStub('TeamController')
58
            );
59
60
            $this->info('Installed TeamMemberController.');
61
            file_put_contents(
62
                app_path('Http/Controllers/Teamwork/TeamMemberController.php'),
63
                $this->compileControllerStub('TeamMemberController')
64
            );
65
66
            $this->info('Installed AuthController.');
67
            file_put_contents(
68
                app_path('Http/Controllers/Teamwork/AuthController.php'),
69
                $this->compileControllerStub('AuthController')
70
            );
71
72
            $this->info('Installed JoinTeamListener');
73
            file_put_contents(
74
                app_path('Listeners/Teamwork/JoinTeamListener.php'),
75
                str_replace(
76
                    '{{namespace}}',
77
                    $this->getNamespace(),
78
                    file_get_contents(__DIR__ . '/../../stubs/listeners/JoinTeamListener.stub')
79
                )
80
            );
81
82
            $this->info('Updated Routes File.');
83
            file_put_contents(
84
               // app_path('Http/routes.php'),
85
               base_path('routes/web.php'),
86
                file_get_contents(__DIR__.'/../../stubs/routes.stub'),
87
                FILE_APPEND
88
            );
89
        }
90
        $this->comment('Teamwork scaffolding generated successfully!');
91
    }
92
93
    /**
94
     * Create the directories for the files.
95
     *
96
     * @return void
97
     */
98
    protected function createDirectories()
99
    {
100
        if (! is_dir(app_path('Http/Controllers/Teamwork'))) {
101
            mkdir(app_path('Http/Controllers/Teamwork'), 0755, true);
102
        }
103
        if (! is_dir(app_path('Listeners/Teamwork'))) {
104
            mkdir(app_path('Listeners/Teamwork'), 0755, true);
105
        }
106
        if (! is_dir(base_path('resources/views/teamwork'))) {
107
            mkdir(base_path('resources/views/teamwork'), 0755, true);
108
        }
109
        if (! is_dir(base_path('resources/views/teamwork/emails'))) {
110
            mkdir(base_path('resources/views/teamwork/emails'), 0755, true);
111
        }
112
        if (! is_dir(base_path('resources/views/teamwork/members'))) {
113
            mkdir(base_path('resources/views/teamwork/members'), 0755, true);
114
        }
115
    }
116
117
    /**
118
     * Export the authentication views.
119
     *
120
     * @return void
121
     */
122
    protected function exportViews()
123
    {
124
        foreach ($this->views as $key => $value) {
125
            $path = base_path('resources/views/'.$value);
126
            $this->line('<info>Created View:</info> '.$path);
127
            copy(__DIR__.'/../../stubs/views/'.$key, $path);
128
        }
129
    }
130
131
    /**
132
     * Compiles the HTTP controller stubs.
133
     *
134
     * @param $stubName
135
     * @return string
136
     */
137
    protected function compileControllerStub($stubName)
138
    {
139
        return str_replace(
140
            '{{namespace}}',
141
            $this->getNamespace(),
142
            file_get_contents(__DIR__.'/../../stubs/controllers/'.$stubName.'.stub')
143
        );
144
    }
145
146
    protected function getNamespace()
147
    {
148
        return  app()->getNamespace();
149
    }
150
}
151