|
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
|
|
|
|
|
11
|
|
|
class SetupCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
use DetectsApplicationNamespace; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The name and signature of the console command. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $signature = 'blog:setup {--data : Specifies that demo data should be seeded}'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Scaffold basic blog views and routes'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Execute the console command. |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function handle() |
|
35
|
|
|
{ |
|
36
|
|
|
// TODO: |
|
37
|
|
|
// Controllers |
|
38
|
|
|
// Routes |
|
39
|
|
|
// Views? |
|
40
|
|
|
|
|
41
|
|
|
// Optionally seed the database with demo data |
|
42
|
|
|
if ($this->option('data')) { |
|
43
|
|
|
$this->seed(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->info('Setup complete'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Run the demo data seeder. |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
private function seed() |
|
55
|
|
|
{ |
|
56
|
|
|
// TODO: Check that blog:install has been run before this blog:setup |
|
57
|
|
|
|
|
58
|
|
|
// Publish factories |
|
59
|
|
|
$files = collect( |
|
|
|
|
|
|
60
|
|
|
array_diff(scandir(__DIR__.'/../../../database/factories/'), array('.', '..')) |
|
61
|
|
|
)->each(function ($file) { |
|
62
|
|
|
copy(__DIR__."/../../../database/factories/{$file}", database_path('factories')."/{$file}"); |
|
63
|
|
|
}); |
|
64
|
|
|
|
|
65
|
|
|
// Create data |
|
66
|
|
|
factory(Post::class, 20) |
|
67
|
|
|
->create() |
|
68
|
|
View Code Duplication |
->each(function ($post) { |
|
|
|
|
|
|
69
|
|
|
$post->tags()->sync( |
|
70
|
|
|
factory(Tag::class, 4)->create()->pluck('id')->toArray() |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$post->comments()->save(factory(Comment::class)->make()); |
|
74
|
|
|
$post->comments()->save(factory(Comment::class)->make()); |
|
75
|
|
|
$post->comments()->save(factory(Comment::class)->make()); |
|
76
|
|
|
}); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.