1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Zero Framework. |
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 NunoMaduro\ZeroFramework\Commands; |
13
|
|
|
|
14
|
|
|
use Phar; |
15
|
|
|
use FilesystemIterator; |
16
|
|
|
use UnexpectedValueException; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The is the Zero Framework build command class. |
21
|
|
|
* |
22
|
|
|
* @author Nuno Maduro <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Builder extends AbstractCommand |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The directory that contains your application builds. |
28
|
|
|
*/ |
29
|
|
|
const BUILD_PATH = BASE_PATH.'/builds'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The default build name. |
33
|
|
|
*/ |
34
|
|
|
const BUILD_NAME = 'application'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Contains the application structure |
38
|
|
|
* needed for the build. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $structure = [ |
43
|
|
|
'app/', |
44
|
|
|
'bootstrap/', |
45
|
|
|
'vendor/', |
46
|
|
|
'config/', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The name and signature of the console command. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $signature = 'build'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The console command description. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $description = 'The build app command'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Execute the console command. |
65
|
|
|
*/ |
66
|
|
|
public function handle(): void |
67
|
|
|
{ |
68
|
|
|
if (Phar::canWrite()) { |
69
|
|
|
$this->build($this->input->getArgument('name') ?: self::BUILD_NAME); |
70
|
|
|
} else { |
71
|
|
|
$this->error('Unable to compile a phar because of php\'s security settings. ' |
72
|
|
|
.'phar.readonly must be disabled in php.ini. '.PHP_EOL.PHP_EOL |
73
|
|
|
.'You will need to edit '.php_ini_loaded_file().' and add or set' |
74
|
|
|
.PHP_EOL.PHP_EOL.' phar.readonly = Off'.PHP_EOL.PHP_EOL |
75
|
|
|
.'to continue. Details here: http://php.net/manual/en/phar.configuration.php' |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Configure the command options. |
82
|
|
|
* |
83
|
|
|
* Ask for the name of the build. |
84
|
|
|
*/ |
85
|
|
|
protected function configure(): void |
86
|
|
|
{ |
87
|
|
|
$this->addArgument('name', InputArgument::OPTIONAL); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Builds the application. |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
protected function build(string $name): Build |
98
|
|
|
{ |
99
|
|
|
$this->comment("Building: $name"); |
100
|
|
|
$this->compile($name) |
101
|
|
|
->cleanUp($name); |
102
|
|
|
|
103
|
|
|
$this->info("Standalone application compiled into: builds/$name"); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Compiles the standalone application. |
110
|
|
|
* |
111
|
|
|
* @param string $name |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
protected function compile(string $name): Build |
116
|
|
|
{ |
117
|
|
|
$compiler = $this->makeFolder() |
118
|
|
|
->getCompiler($name); |
119
|
|
|
|
120
|
|
|
$compiler->buildFromDirectory(BASE_PATH, '#'.implode('|', $this->structure).'#'); |
121
|
|
|
$compiler->setStub($compiler->createDefaultStub('bootstrap/init.php')); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Gets a new instance of the compiler. |
128
|
|
|
* |
129
|
|
|
* @param string $name |
130
|
|
|
* |
131
|
|
|
* @return \Phar |
132
|
|
|
*/ |
133
|
|
|
protected function getCompiler(string $name): \Phar |
134
|
|
|
{ |
135
|
|
|
try { |
136
|
|
|
return new Phar( |
137
|
|
|
self::BUILD_PATH.'/'.$name.'.phar', |
138
|
|
|
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, |
139
|
|
|
$name |
140
|
|
|
); |
141
|
|
|
} catch (UnexpectedValueException $e) { |
142
|
|
|
$this->error("You can't perform a build."); |
143
|
|
|
exit(0); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Creates the folder for the builds. |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
protected function makeFolder(): Build |
153
|
|
|
{ |
154
|
|
|
if (! file_exists(self::BUILD_PATH)) { |
155
|
|
|
mkdir(self::BUILD_PATH); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Moves the compiled files to the builds folder. |
163
|
|
|
* |
164
|
|
|
* @param string $name |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
protected function cleanUp(string $name): Build |
169
|
|
|
{ |
170
|
|
|
$file = self::BUILD_PATH."/$name"; |
171
|
|
|
rename("$file.phar", $file); |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|