1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Laravel Zero. |
5
|
|
|
* |
6
|
|
|
* (c) Nuno Maduro <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LaravelZero\Framework\Commands\App; |
13
|
|
|
|
14
|
|
|
use Phar; |
15
|
|
|
use FilesystemIterator; |
16
|
|
|
use UnexpectedValueException; |
17
|
|
|
use Illuminate\Support\Facades\File; |
18
|
|
|
use LaravelZero\Framework\Commands\Command; |
19
|
|
|
use LaravelZero\Framework\Contracts\Providers\Composer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This is the Laravel Zero Framework Builder Command implementation. |
23
|
|
|
*/ |
24
|
|
|
class Builder extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
protected $signature = 'app:build {name=application : The build name} {--with-dev : Whether the dev dependencies should be included}'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Perform an application build'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Contains the default app structure. |
38
|
|
|
* |
39
|
|
|
* @var string[] |
40
|
|
|
*/ |
41
|
|
|
protected $structure = [ |
42
|
|
|
'app'.DIRECTORY_SEPARATOR, |
43
|
|
|
'bootstrap'.DIRECTORY_SEPARATOR, |
44
|
|
|
'vendor'.DIRECTORY_SEPARATOR, |
45
|
|
|
'config'.DIRECTORY_SEPARATOR, |
46
|
|
|
'composer.json', |
47
|
|
|
'builder-stub', |
48
|
|
|
'.env', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Holds the stub name. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $stub = 'builder-stub'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Holds the configuration on is original state. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected static $config; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function handle(): void |
69
|
|
|
{ |
70
|
1 |
|
$this->info('Building the application...'); |
71
|
|
|
|
72
|
1 |
|
if (Phar::canWrite()) { |
73
|
1 |
|
$this->build($this->input->getArgument('name') ?: static::BUILD_NAME); |
74
|
|
|
} else { |
75
|
|
|
$this->error( |
76
|
|
|
'Unable to compile a phar because of php\'s security settings. '.'phar.readonly must be disabled in php.ini. '.PHP_EOL.PHP_EOL.'You will need to edit '.php_ini_loaded_file( |
77
|
|
|
).' and add or set'.PHP_EOL.PHP_EOL.' phar.readonly = Off'.PHP_EOL.PHP_EOL.'to continue. Details here: http://php.net/manual/en/phar.configuration.php' |
78
|
|
|
); |
79
|
|
|
} |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Builds the application into a single file. |
84
|
|
|
* |
85
|
|
|
* @param string $name The file name. |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
1 |
|
protected function build(string $name): Builder |
90
|
|
|
{ |
91
|
|
|
/* |
92
|
|
|
* We prepare the application for a build, moving it to production. Then, |
93
|
|
|
* after compile all the code to a single file, we move the built file |
94
|
|
|
* to the builds folder with the correct permissions. |
95
|
|
|
*/ |
96
|
1 |
|
$this->prepare() |
97
|
1 |
|
->compile($name) |
98
|
1 |
|
->setPermissions($name) |
99
|
1 |
|
->clear(); |
100
|
|
|
|
101
|
1 |
|
$this->info( |
102
|
1 |
|
sprintf('Application built into a single file: %s', $this->app->buildsPath($name)) |
|
|
|
|
103
|
|
|
); |
104
|
|
|
|
105
|
1 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $name |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
1 |
|
protected function compile(string $name): Builder |
114
|
|
|
{ |
115
|
1 |
|
$compiler = $this->makeBuildsFolder() |
116
|
1 |
|
->getCompiler($name); |
117
|
|
|
|
118
|
1 |
|
$structure = config('app.structure') ?: $this->structure; |
119
|
|
|
|
120
|
1 |
|
$regex = '#'.implode('|', $structure).'#'; |
121
|
|
|
|
122
|
1 |
|
if (stristr(PHP_OS, 'WINNT') !== false) { // For windows: |
123
|
|
|
$compiler->buildFromDirectory($this->app->basePath(), str_replace('\\', '/', $regex)); |
124
|
|
|
} else { // Linux, OS X: |
125
|
1 |
|
$compiler->buildFromDirectory($this->app->basePath(), $regex); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
$this->task('Compiling code', function () use ($compiler) { |
|
|
|
|
129
|
1 |
|
$compiler->setStub( |
130
|
1 |
|
"#!/usr/bin/env php \n".$compiler->createDefaultStub( |
131
|
1 |
|
$this->stub |
132
|
|
|
) |
133
|
|
|
); |
134
|
1 |
|
}); |
135
|
|
|
|
136
|
1 |
|
$file = $this->app->buildsPath($name); |
|
|
|
|
137
|
|
|
|
138
|
1 |
|
File::move("$file.phar", $file); |
139
|
|
|
|
140
|
1 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Gets a new instance of the compiler. |
145
|
|
|
* |
146
|
|
|
* @param string $name |
147
|
|
|
* |
148
|
|
|
* @return \Phar |
149
|
|
|
*/ |
150
|
1 |
|
protected function getCompiler(string $name): \Phar |
151
|
|
|
{ |
152
|
|
|
try { |
153
|
1 |
|
return new Phar( |
154
|
1 |
|
$this->app->buildsPath($name.'.phar'), |
|
|
|
|
155
|
1 |
|
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, |
156
|
1 |
|
$name |
157
|
|
|
); |
158
|
|
|
} catch (UnexpectedValueException $e) { |
159
|
|
|
$this->app->abort(401, 'Unauthorized.'); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
1 |
|
protected function makeBuildsFolder(): Builder |
167
|
|
|
{ |
168
|
1 |
|
if (! File::exists($this->app->buildsPath())) { |
|
|
|
|
169
|
1 |
|
File::makeDirectory($this->app->buildsPath()); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sets the executable mode on the single application file. |
177
|
|
|
* |
178
|
|
|
* @param string $name |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
1 |
|
protected function setPermissions($name): Builder |
183
|
|
|
{ |
184
|
1 |
|
chmod($this->app->buildsPath($name), 0755); |
|
|
|
|
185
|
|
|
|
186
|
1 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
1 |
|
protected function prepare(): Builder |
193
|
|
|
{ |
194
|
1 |
|
$file = $this->app->configPath('app.php'); |
195
|
1 |
|
static::$config = File::get($file); |
196
|
1 |
|
$config = include $file; |
197
|
|
|
|
198
|
1 |
|
$config['production'] = true; |
199
|
|
|
|
200
|
1 |
|
$this->task('Moving application to production mode', function () use ($file, $config) { |
|
|
|
|
201
|
1 |
|
File::put($file, '<?php return '.var_export($config, true).';'.PHP_EOL); |
202
|
1 |
|
}); |
203
|
|
|
|
204
|
1 |
View Code Duplication |
if ($this->option('with-dev') === false) { |
|
|
|
|
205
|
1 |
|
$this->task('Temporarily removing dev dependencies', function () { |
|
|
|
|
206
|
1 |
|
$this->app[Composer::class]->install(['--no-dev']); |
207
|
1 |
|
}); |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
$stub = str_replace('#!/usr/bin/env php', '', File::get($this->app->basePath(ARTISAN_BINARY))); |
|
|
|
|
211
|
|
|
|
212
|
|
|
// Remove first line. |
213
|
1 |
|
$stubAsArray = explode("\n", $stub); |
214
|
1 |
|
array_shift($stubAsArray); |
215
|
1 |
|
$stub = implode("\n", $stubAsArray); |
216
|
|
|
|
217
|
1 |
|
File::put($this->app->basePath($this->stub), $stub); |
|
|
|
|
218
|
|
|
|
219
|
1 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return $this |
224
|
|
|
*/ |
225
|
1 |
|
protected function clear(): Builder |
226
|
|
|
{ |
227
|
1 |
|
File::put($this->app->configPath('app.php'), static::$config); |
228
|
|
|
|
229
|
1 |
|
File::delete($this->app->basePath($this->stub)); |
|
|
|
|
230
|
|
|
|
231
|
1 |
View Code Duplication |
if ($this->option('with-dev') === false) { |
|
|
|
|
232
|
1 |
|
$this->task('Reinstalling dev dependencies', function () { |
|
|
|
|
233
|
1 |
|
$this->app[Composer::class]->install(); |
234
|
1 |
|
}); |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
static::$config = null; |
238
|
|
|
|
239
|
1 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Makes sure that the `clear` is performed even |
244
|
|
|
* if the command fails. |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
18 |
|
public function __destruct() |
249
|
|
|
{ |
250
|
18 |
|
if (static::$config !== null) { |
251
|
|
|
$this->clear(); |
252
|
|
|
$this->error('Something went wrong.'); |
253
|
|
|
} |
254
|
18 |
|
} |
255
|
|
|
} |
256
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: