Completed
Push — master ( b877df...b3f08b )
by Christopher
01:17
created

SetupCommand::publishProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Chriscreates\Blog\Console\Commands;
4
5
use Chriscreates\Blog\Comment;
6
use Chriscreates\Blog\Post;
7
use Chriscreates\Blog\Tag;
8
use Illuminate\Console\Command;
9
use Illuminate\Console\DetectsApplicationNamespace;
10
use Illuminate\Support\Str;
11
12
class SetupCommand extends Command
13
{
14
    use DetectsApplicationNamespace;
15
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'blog:setup
22
                            {--data : Setup the database with demo data}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Scaffold basic blog views and routes';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @return void
35
     */
36
    public function handle()
37
    {
38
        if ( ! file_exists(config_path('blog.php'))) {
39
            $this->error("You haven't published the config file.");
40
41
            if ($this->confirm('Publish the config file?')) {
42
                $this->call('blog:install');
43
            } else {
44
                return;
45
            }
46
        }
47
48
        if ( ! file_exists(base_path('routes/blog.php'))) {
49
            $this->publishRoutes();
50
        }
51
52
        if ( ! file_exists(app_path('Providers/BlogServiceProvider.php'))) {
53
            $this->publishProvider();
54
        }
55
56
        // TODO:
57
        // Views?
58
59
        if ($this->option('data')) {
60
            $this->seed();
61
        } elseif ($this->confirm(
62
            'Would you like to setup the database with demo data?'
63
        )) {
64
            $this->seed();
65
        }
66
67
        $this->info('Setup complete.');
68
    }
69
70
    /**
71
     * Run the demo data seeder.
72
     *
73
     * @return void
74
     */
75
    private function seed()
76
    {
77
        $this->callSilent('vendor:publish', ['--tag' => 'blog-factories']);
78
79
        // $files = collect(
80
        //     array_diff(scandir(__DIR__.'/../../../database/factories/'), array('.', '..'))
81
        // )->each(function ($file) {
82
        //     copy(__DIR__."/../../../database/factories/{$file}", database_path('factories')."/{$file}");
83
        // });
84
85
        // Create data
86
        factory(Post::class, 20)
87
         ->create()
88
         ->each(function ($post) {
89
             $post->tags()->sync(
90
                 factory(Tag::class, 4)->create()->pluck('id')->toArray()
91
             );
92
93
             $post->comments()->save(factory(Comment::class)->make());
94
             $post->comments()->save(factory(Comment::class)->make());
95
             $post->comments()->save(factory(Comment::class)->make());
96
         });
97
    }
98
99
    /**
100
     * Register the provider.
101
     *
102
     * @return void
103
     */
104
    private function publishProvider()
105
    {
106
        copy(
107
            __DIR__.'/../../Providers/BlogServiceProvider.php',
108
            app_path('Providers/BlogServiceProvider.php')
109
        );
110
111
        $namespace = Str::replaceLast('\\', '', $this->getAppNamespace());
112
        $appConfig = file_get_contents(config_path('app.php'));
113
114
        if (Str::contains($appConfig, $namespace.'\\Providers\\BlogServiceProvider::class')) {
115
            return;
116
        }
117
118
        $lineEndingCount = [
119
            "\r\n" => substr_count($appConfig, "\r\n"),
120
            "\r" => substr_count($appConfig, "\r"),
121
            "\n" => substr_count($appConfig, "\n"),
122
        ];
123
124
        $eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
125
126
        file_put_contents(config_path('app.php'), str_replace(
127
            "{$namespace}\\Providers\EventServiceProvider::class,".$eol,
128
            "{$namespace}\\Providers\EventServiceProvider::class,".$eol."        {$namespace}\Providers\BlogServiceProvider::class,".$eol,
129
            $appConfig
130
        ));
131
    }
132
133
    /**
134
     * Register the routes.
135
     *
136
     * @return void
137
     */
138
    private function publishRoutes()
139
    {
140
        copy(
141
            __DIR__.'/../../../routes/web.php',
142
            base_path('routes/blog.php')
143
        );
144
    }
145
}
146