Completed
Push — master ( dc40d0...a009a1 )
by Marcel
05:50
created

MakeTeamwork::compileControllerStub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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