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