|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Larafolio\database\seeds; |
|
4
|
|
|
|
|
5
|
|
|
use App\User; |
|
6
|
|
|
use Larafolio\Models\Page; |
|
7
|
|
|
use Larafolio\Models\Project; |
|
8
|
|
|
use Illuminate\Database\Seeder; |
|
9
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
class ImagesTableSeeder extends Seeder |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Lookup table to map images to projects. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $projectLookup = [ |
|
19
|
|
|
'a' => 1, |
|
20
|
|
|
'b' => 2, |
|
21
|
|
|
'c' => 3, |
|
22
|
|
|
'd' => 4, |
|
23
|
|
|
'e' => 5, |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* User instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @var User |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $user; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Run the database seeds. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function run() |
|
37
|
|
|
{ |
|
38
|
|
|
$filesystem = new Filesystem(); |
|
39
|
|
|
|
|
40
|
|
|
$this->user = $user = User::find(1); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$this->makeProjectImages($filesystem); |
|
43
|
|
|
|
|
44
|
|
|
$this->makePageImages($filesystem); |
|
45
|
|
|
|
|
46
|
|
|
$old = umask(0); |
|
47
|
|
|
|
|
48
|
|
|
chmod(storage_path('app/public/images'), 0775); |
|
49
|
|
|
|
|
50
|
|
|
umask($old); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Make images for project. |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function makeProjectImages(Filesystem $filesystem) |
|
59
|
|
|
{ |
|
60
|
|
|
$images = $filesystem->allFiles(__DIR__.'/../../../tests/_data/images'); |
|
61
|
|
|
|
|
62
|
|
|
foreach ($images as $image) { |
|
63
|
|
|
$name = $image->getFilename(); |
|
64
|
|
|
|
|
65
|
|
|
$path = 'public/images/'.$name; |
|
66
|
|
|
|
|
67
|
|
|
$this->moveImage($image, $path, $filesystem); |
|
68
|
|
|
|
|
69
|
|
|
$this->addToProject($name, $path); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Make images for project. |
|
75
|
|
|
* |
|
76
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function makePageImages(Filesystem $filesystem) |
|
79
|
|
|
{ |
|
80
|
|
|
$image = $filesystem->get(__DIR__.'/../../../tests/_data/new.jpg'); |
|
81
|
|
|
|
|
82
|
|
|
$path = 'public/images/pageImage.jpg'; |
|
83
|
|
|
|
|
84
|
|
|
\Storage::put($path, $image); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
$page = Page::first(); |
|
87
|
|
|
|
|
88
|
|
|
$this->user->addImageToModel($page, ['path' => $path]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Move image file to storage. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \Symfony\Component\Finder\SplFileInfo $image File info. |
|
95
|
|
|
* @param string $path Path to move to. |
|
96
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function moveImage($image, $path, Filesystem $filesystem) |
|
99
|
|
|
{ |
|
100
|
|
|
$imageFile = $filesystem->get($image->getPathname()); |
|
101
|
|
|
|
|
102
|
|
|
\Storage::put($path, $imageFile); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Add image to a project. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $name Original filename. |
|
109
|
|
|
* @param string $path Path to image. |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function addToProject($name, $path) |
|
112
|
|
|
{ |
|
113
|
|
|
$key = $this->projectLookup[substr($name, 0, 1)]; |
|
114
|
|
|
|
|
115
|
|
|
$project = Project::find($key); |
|
116
|
|
|
|
|
117
|
|
|
$this->user->addImageToModel($project, ['path' => $path]); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
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.