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(app_path('Providers/BlogServiceProvider.php'))) { |
49
|
|
|
$this->publishProvider(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// TODO: |
53
|
|
|
// Routes |
54
|
|
|
// Views? |
55
|
|
|
|
56
|
|
|
if ($this->option('data')) { |
57
|
|
|
$this->seed(); |
58
|
|
|
} elseif ($this->confirm( |
59
|
|
|
'Would you like to setup the database with demo data?' |
60
|
|
|
)) { |
61
|
|
|
$this->seed(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->info('Setup complete.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Run the demo data seeder. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
private function seed() |
73
|
|
|
{ |
74
|
|
|
$this->callSilent('vendor:publish', ['--tag' => 'blog-factories']); |
75
|
|
|
|
76
|
|
|
// $files = collect( |
77
|
|
|
// array_diff(scandir(__DIR__.'/../../../database/factories/'), array('.', '..')) |
78
|
|
|
// )->each(function ($file) { |
79
|
|
|
// copy(__DIR__."/../../../database/factories/{$file}", database_path('factories')."/{$file}"); |
80
|
|
|
// }); |
81
|
|
|
|
82
|
|
|
// Create data |
83
|
|
|
factory(Post::class, 20) |
84
|
|
|
->create() |
85
|
|
|
->each(function ($post) { |
86
|
|
|
$post->tags()->sync( |
87
|
|
|
factory(Tag::class, 4)->create()->pluck('id')->toArray() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$post->comments()->save(factory(Comment::class)->make()); |
91
|
|
|
$post->comments()->save(factory(Comment::class)->make()); |
92
|
|
|
$post->comments()->save(factory(Comment::class)->make()); |
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Register the provider. |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
private function publishProvider() |
102
|
|
|
{ |
103
|
|
|
copy( |
104
|
|
|
__DIR__.'/../../Providers/BlogServiceProvider.php', |
105
|
|
|
app_path('Providers/BlogServiceProvider.php') |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$namespace = Str::replaceLast('\\', '', $this->getAppNamespace()); |
109
|
|
|
$appConfig = file_get_contents(config_path('app.php')); |
110
|
|
|
|
111
|
|
|
if (Str::contains($appConfig, $namespace.'\\Providers\\BlogServiceProvider::class')) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$lineEndingCount = [ |
116
|
|
|
"\r\n" => substr_count($appConfig, "\r\n"), |
117
|
|
|
"\r" => substr_count($appConfig, "\r"), |
118
|
|
|
"\n" => substr_count($appConfig, "\n"), |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
$eol = array_keys($lineEndingCount, max($lineEndingCount))[0]; |
122
|
|
|
|
123
|
|
|
file_put_contents(config_path('app.php'), str_replace( |
124
|
|
|
"{$namespace}\\Providers\EventServiceProvider::class,".$eol, |
125
|
|
|
"{$namespace}\\Providers\EventServiceProvider::class,".$eol." {$namespace}\Providers\BlogServiceProvider::class,".$eol, |
126
|
|
|
$appConfig |
127
|
|
|
)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|