1
|
|
|
<?php namespace Anomaly\Streams\Platform\Addon\Console\Command; |
2
|
|
|
|
3
|
|
|
use Illuminate\Filesystem\Filesystem; |
4
|
|
|
use Anomaly\Streams\Platform\Support\Parser; |
5
|
|
|
use Anomaly\Streams\Platform\Stream\StreamCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class WriteAddonSeeder |
9
|
|
|
* |
10
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
11
|
|
|
* @author Ryan Thompson <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @link http://pyrocms.com/ |
14
|
|
|
*/ |
15
|
|
|
class WriteAddonSeeder |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The addon path. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $path; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The addon type. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $type; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The addon slug. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $slug; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The vendor slug. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $vendor; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new WriteAddonSeeder instance. |
48
|
|
|
* |
49
|
|
|
* @param string $path The path |
50
|
|
|
* @param string $type The type |
51
|
|
|
* @param string $slug The slug |
52
|
|
|
* @param string $vendor The vendor |
53
|
|
|
* @param StreamCollection $streams The streams |
54
|
|
|
*/ |
55
|
|
|
public function __construct($path, $type, $slug, $vendor, StreamCollection $streams) |
56
|
|
|
{ |
57
|
|
|
$this->path = $path; |
58
|
|
|
$this->slug = $slug; |
59
|
|
|
$this->type = $type; |
60
|
|
|
$this->vendor = $vendor; |
61
|
|
|
$this->streams = $streams; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Handle the command. |
66
|
|
|
* |
67
|
|
|
* @param Parser $parser |
68
|
|
|
* @param Filesystem $filesystem |
69
|
|
|
*/ |
70
|
|
|
public function handle(Parser $parser, Filesystem $filesystem) |
71
|
|
|
{ |
72
|
|
|
$slug = ucfirst(camel_case($this->slug)); |
73
|
|
|
$type = ucfirst(camel_case($this->type)); |
74
|
|
|
$vendor = ucfirst(camel_case($this->vendor)); |
75
|
|
|
|
76
|
|
|
$addon = $slug.$type; |
77
|
|
|
$class = $addon.'Seeder'; |
78
|
|
|
$namespace = "{$vendor}\\{$addon}"; |
79
|
|
|
$uses = $this->getUses($namespace)->implode("\n"); |
80
|
|
|
$calls = $this->getCalls()->implode("\n "); |
81
|
|
|
|
82
|
|
|
$path = "{$this->path}/src/{$class}.php"; |
83
|
|
|
|
84
|
|
|
$template = $filesystem->get( |
85
|
|
|
base_path('vendor/anomaly/streams-platform/resources/stubs/addons/seeder.stub') |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$filesystem->makeDirectory(dirname($path), 0755, true, true); |
89
|
|
|
|
90
|
|
|
$filesystem->put($path, $parser->parse( |
91
|
|
|
$template, |
92
|
|
|
compact('namespace', 'class', 'uses', 'calls') |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets the uses. |
98
|
|
|
* |
99
|
|
|
* @param string $namespace The namespace |
100
|
|
|
* @return Collection The uses. |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function getUses($namespace) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
return $this->streams->map( |
105
|
|
|
function ($stream) use ($namespace) |
106
|
|
|
{ |
107
|
|
|
$name = ucfirst(str_singular($stream->getSlug())); |
108
|
|
|
|
109
|
|
|
return "use {$namespace}\\{$name}\\{$name}Seeder;"; |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Gets the calls. |
116
|
|
|
* |
117
|
|
|
* @return Collection The calls. |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function getCalls() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
return $this->streams->map( |
122
|
|
|
function ($stream) |
123
|
|
|
{ |
124
|
|
|
$name = ucfirst(str_singular($stream->getSlug())); |
125
|
|
|
|
126
|
|
|
return "\$this->call({$name}Seeder::class);"; |
127
|
|
|
} |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Gets the name. |
133
|
|
|
* |
134
|
|
|
* @param string $slug The slug |
135
|
|
|
* @return string The name. |
136
|
|
|
*/ |
137
|
|
|
public function getName($slug) |
138
|
|
|
{ |
139
|
|
|
return ucfirst(str_singular($slug)); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: