-
Notifications
You must be signed in to change notification settings - Fork 41
/
PackageCreator.php
executable file
·376 lines (327 loc) · 9.4 KB
/
PackageCreator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php namespace Illuminate\Workbench;
use Illuminate\Filesystem\Filesystem;
class PackageCreator {
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The basic building blocks of the package.
*
* @param array
*/
protected $basicBlocks = array(
'SupportFiles',
'TestDirectory',
'ServiceProvider',
);
/**
* The building blocks of the package.
*
* @param array
*/
protected $blocks = array(
'SupportFiles',
'SupportDirectories',
'PublicDirectory',
'TestDirectory',
'ServiceProvider',
);
/**
* Create a new package creator instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @return void
*/
public function __construct(Filesystem $files)
{
$this->files = $files;
}
/**
* Create a new package stub.
*
* @param \Illuminate\Workbench\Package $package
* @param string $path
* @param bool $plain
* @return string
*/
public function create(Package $package, $path, $plain = true)
{
$directory = $this->createDirectory($package, $path);
// To create the package, we will spin through a list of building blocks that
// make up each package. We'll then call the method to build that block on
// the class, which keeps the actual building of stuff nice and cleaned.
foreach ($this->getBlocks($plain) as $block)
{
$this->{"write{$block}"}($package, $directory, $plain);
}
return $directory;
}
/**
* Create a package with all resource directories.
*
* @param \Illuminate\Workbench\Package $package
* @param string $path
* @return void
*/
public function createWithResources(Package $package, $path)
{
return $this->create($package, $path, false);
}
/**
* Get the blocks for a given package.
*
* @param bool $plain
* @return array
*/
protected function getBlocks($plain)
{
return $plain ? $this->basicBlocks : $this->blocks;
}
/**
* Write the support files to the package root.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @param bool $plain
* @return void
*/
public function writeSupportFiles(Package $package, $directory, $plain)
{
foreach (array('PhpUnit', 'Travis', 'Composer', 'Ignore') as $file)
{
$this->{"write{$file}File"}($package, $directory, $plain);
}
}
/**
* Write the PHPUnit stub file.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @return void
*/
protected function writePhpUnitFile(Package $package, $directory)
{
$stub = __DIR__.'/stubs/phpunit.xml';
$this->files->copy($stub, $directory.'/phpunit.xml');
}
/**
* Write the Travis stub file.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @return void
*/
protected function writeTravisFile(Package $package, $directory)
{
$stub = __DIR__.'/stubs/.travis.yml';
$this->files->copy($stub, $directory.'/.travis.yml');
}
/**
* Write the Composer.json stub file.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @param bool $plain
* @return void
*/
protected function writeComposerFile(Package $package, $directory, $plain)
{
$stub = $this->getComposerStub($plain);
$stub = $this->formatPackageStub($package, $stub);
$this->files->put($directory.'/composer.json', $stub);
}
/**
* Get the Composer.json stub file contents.
*
* @param bool $plain
* @return string
*/
protected function getComposerStub($plain)
{
if ($plain) return $this->files->get(__DIR__.'/stubs/plain.composer.json');
return $this->files->get(__DIR__.'/stubs/composer.json');
}
/**
* Write the stub .gitignore file for the package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @param bool $plain
* @return void
*/
public function writeIgnoreFile(Package $package, $directory, $plain)
{
$this->files->copy(__DIR__.'/stubs/gitignore.txt', $directory.'/.gitignore');
}
/**
* Create the support directories for a package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @return void
*/
public function writeSupportDirectories(Package $package, $directory)
{
foreach (array('config', 'controllers', 'lang', 'migrations', 'views') as $support)
{
$this->writeSupportDirectory($package, $support, $directory);
}
}
/**
* Write a specific support directory for the package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $support
* @param string $directory
* @return void
*/
protected function writeSupportDirectory(Package $package, $support, $directory)
{
// Once we create the source directory, we will write an empty file to the
// directory so that it will be kept in source control allowing the dev
// to go ahead and push these components to GitHub right on creation.
$path = $directory.'/src/'.$support;
$this->files->makeDirectory($path, 0777, true);
$this->files->put($path.'/.gitkeep', '');
}
/**
* Create the public directory for the package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @param bool $plain
* @return void
*/
public function writePublicDirectory(Package $package, $directory, $plain)
{
if ($plain) return;
$this->files->makeDirectory($directory.'/public');
$this->files->put($directory.'/public/.gitkeep', '');
}
/**
* Create the test directory for the package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @return void
*/
public function writeTestDirectory(Package $package, $directory)
{
$this->files->makeDirectory($directory.'/tests');
$this->files->put($directory.'/tests/.gitkeep', '');
}
/**
* Write the stub ServiceProvider for the package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @param bool $plain
* @return void
*/
public function writeServiceProvider(Package $package, $directory, $plain)
{
// Once we have the service provider stub, we will need to format it and make
// the necessary replacements to the class, namespaces, etc. Then we'll be
// able to write it out into the package's workbench directory for them.
$stub = $this->getProviderStub($package, $plain);
$this->writeProviderStub($package, $directory, $stub);
}
/**
* Write the service provider stub for the package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @param string $stub
* @return void
*/
protected function writeProviderStub(Package $package, $directory, $stub)
{
$path = $this->createClassDirectory($package, $directory);
// The primary source directory where the package's classes will live may not
// exist yet, so we will need to create it before we write these providers
// out to that location. We'll go ahead and create now here before then.
$file = $path.'/'.$package->name.'ServiceProvider.php';
$this->files->put($file, $stub);
}
/**
* Get the stub for a ServiceProvider.
*
* @param \Illuminate\Workbench\Package $package
* @param bool $plain
* @return string
*/
protected function getProviderStub(Package $package, $plain)
{
return $this->formatPackageStub($package, $this->getProviderFile($plain));
}
/**
* Load the raw service provider file.
*
* @param bool $plain
* @return string
*/
protected function getProviderFile($plain)
{
if ($plain)
{
return $this->files->get(__DIR__.'/stubs/plain.provider.stub');
}
return $this->files->get(__DIR__.'/stubs/provider.stub');
}
/**
* Create the main source directory for the package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $directory
* @return string
*/
protected function createClassDirectory(Package $package, $directory)
{
$path = $directory.'/src/'.$package->vendor.'/'.$package->name;
if ( ! $this->files->isDirectory($path))
{
$this->files->makeDirectory($path, 0777, true);
}
return $path;
}
/**
* Format a generic package stub file.
*
* @param \Illuminate\Workbench\Package $package
* @param string $stub
* @return string
*/
protected function formatPackageStub(Package $package, $stub)
{
foreach (get_object_vars($package) as $key => $value)
{
$stub = str_replace('{{'.snake_case($key).'}}', $value, $stub);
}
return $stub;
}
/**
* Create a workbench directory for the package.
*
* @param \Illuminate\Workbench\Package $package
* @param string $path
* @return string
*
* @throws \InvalidArgumentException
*/
protected function createDirectory(Package $package, $path)
{
$fullPath = $path.'/'.$package->getFullName();
// If the directory doesn't exist, we will go ahead and create the package
// directory in the workbench location. We will use this entire package
// name when creating the directory to avoid any potential conflicts.
if ( ! $this->files->isDirectory($fullPath))
{
$this->files->makeDirectory($fullPath, 0777, true);
return $fullPath;
}
throw new \InvalidArgumentException("Package exists.");
}
}