MakeTeamwork::compileControllerStub()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Mpociot\Teamwork\Commands;
4
5
use Illuminate\Console\Command;
6
7
class MakeTeamwork extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'make:teamwork {--views : Only scaffold the teamwork views}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Create Teamwork scaffolding files.';
22
23
    protected $views = [
24
        'emails/invite.blade.php' => 'teamwork/emails/invite.blade.php',
25
        'members/list.blade.php' => 'teamwork/members/list.blade.php',
26
        'create.blade.php' => 'teamwork/create.blade.php',
27
        'edit.blade.php' => 'teamwork/edit.blade.php',
28
        'index.blade.php' => 'teamwork/index.blade.php',
29
    ];
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     * @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...
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return mixed
45
     */
46
    public function handle()
47
    {
48
        $this->createDirectories();
49
50
        $this->exportViews();
51
52
        if (! $this->option('views')) {
53
            $this->info('Installed TeamController.');
54
            file_put_contents(
55
                app_path('Http/Controllers/Teamwork/TeamController.php'),
56
                $this->compileControllerStub('TeamController')
57
            );
58
59
            $this->info('Installed TeamMemberController.');
60
            file_put_contents(
61
                app_path('Http/Controllers/Teamwork/TeamMemberController.php'),
62
                $this->compileControllerStub('TeamMemberController')
63
            );
64
65
            $this->info('Installed AuthController.');
66
            file_put_contents(
67
                app_path('Http/Controllers/Teamwork/AuthController.php'),
68
                $this->compileControllerStub('AuthController')
69
            );
70
71
            $this->info('Installed JoinTeamListener');
72
            file_put_contents(
73
                app_path('Listeners/Teamwork/JoinTeamListener.php'),
74
                str_replace(
75
                    '{{namespace}}',
76
                    $this->getNamespace(),
77
                    file_get_contents(__DIR__.'/../../stubs/listeners/JoinTeamListener.stub')
78
                )
79
            );
80
81
            $this->info('Updated Routes File.');
82
            file_put_contents(
83
               // app_path('Http/routes.php'),
84
               base_path('routes/web.php'),
85
                file_get_contents(__DIR__.'/../../stubs/routes.stub'),
86
                FILE_APPEND
87
            );
88
        }
89
        $this->comment('Teamwork scaffolding generated successfully!');
90
    }
91
92
    /**
93
     * Create the directories for the files.
94
     *
95
     * @return void
96
     */
97
    protected function createDirectories()
98
    {
99
        if (! is_dir(app_path('Http/Controllers/Teamwork'))) {
100
            mkdir(app_path('Http/Controllers/Teamwork'), 0755, true);
101
        }
102
        if (! is_dir(app_path('Listeners/Teamwork'))) {
103
            mkdir(app_path('Listeners/Teamwork'), 0755, true);
104
        }
105
        if (! is_dir(base_path('resources/views/teamwork'))) {
106
            mkdir(base_path('resources/views/teamwork'), 0755, true);
107
        }
108
        if (! is_dir(base_path('resources/views/teamwork/emails'))) {
109
            mkdir(base_path('resources/views/teamwork/emails'), 0755, true);
110
        }
111
        if (! is_dir(base_path('resources/views/teamwork/members'))) {
112
            mkdir(base_path('resources/views/teamwork/members'), 0755, true);
113
        }
114
    }
115
116
    /**
117
     * Export the authentication views.
118
     *
119
     * @return void
120
     */
121
    protected function exportViews()
122
    {
123
        foreach ($this->views as $key => $value) {
124
            $path = base_path('resources/views/'.$value);
125
            $this->line('<info>Created View:</info> '.$path);
126
            copy(__DIR__.'/../../stubs/views/'.$key, $path);
127
        }
128
    }
129
130
    /**
131
     * Compiles the HTTP controller stubs.
132
     *
133
     * @param $stubName
134
     * @return string
135
     */
136
    protected function compileControllerStub($stubName)
137
    {
138
        return str_replace(
139
            '{{namespace}}',
140
            $this->getNamespace(),
141
            file_get_contents(__DIR__.'/../../stubs/controllers/'.$stubName.'.stub')
142
        );
143
    }
144
145
    protected function getNamespace()
146
    {
147
        return  app()->getNamespace();
148
    }
149
}
150