Skip to content

Commit 189051b

Browse files
committed
complete phar package tool. add a example controller for pack phar
1 parent bfde581 commit 189051b

File tree

10 files changed

+746
-154
lines changed

10 files changed

+746
-154
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018-01-22
6+
* Time: 11:55
7+
*/
8+
9+
namespace Inhere\Console\Examples\Controllers;
10+
11+
use Inhere\Console\Components\PharCompiler;
12+
use Inhere\Console\Controller;
13+
use Inhere\Console\Utils\Helper;
14+
use Inhere\Console\Utils\Show;
15+
16+
/**
17+
* Class PharController
18+
* @package Inhere\Console\Examples\Controllers
19+
*/
20+
class PharController extends Controller
21+
{
22+
protected static $name = 'phar';
23+
protected static $description = 'Pack a project directory to phar or unpack phar to directory';
24+
25+
/**
26+
* pack project to a phar package
27+
* @usage {fullCommand} [--dir DIR] [--output FILE]
28+
* @options
29+
* --dir STRING Setting the directory for packing.
30+
* - default is current work-dir.(<comment>{workDir}</comment>)
31+
* --output STRING Setting the output file name(<comment>app.phar</comment>)
32+
* --refresh BOOL Whether build vendor folder files on phar file exists(<comment>False</comment>)
33+
* @param \Inhere\Console\IO\Input $in
34+
* @param \Inhere\Console\IO\Output $out
35+
* @return int
36+
*/
37+
public function packCommand($in, $out): int
38+
{
39+
$time = microtime(1);
40+
$workDir = $in->getPwd();
41+
$dir = $in->getOpt('dir') ?: $workDir;
42+
$pharFile = $workDir . '/' . $in->getOpt('output', 'app.phar');
43+
44+
$cpr = new PharCompiler($dir);
45+
46+
// config
47+
$cpr
48+
// ->stripComments(false)
49+
->setShebang(true)
50+
->addExclude([
51+
'demo',
52+
'tests',
53+
'tmp',
54+
])
55+
->addFile([
56+
'LICENSE',
57+
'composer.json',
58+
'README.md',
59+
'tests/boot.php',
60+
])
61+
->setCliIndex('examples/app')
62+
// ->setWebIndex('web/index.php')
63+
// ->setVersionFile('config/config.php')
64+
->in($dir)
65+
;
66+
67+
$cpr->setStripFilter(function ($file) {
68+
/** @var \SplFileInfo $file */
69+
$name = $file->getFilename();
70+
71+
return false === strpos($name, 'Command.php') && false === strpos($name, 'Controller.php');
72+
});
73+
74+
$counter = null;
75+
$refresh = $in->boolOpt('refresh');
76+
77+
$out->liteInfo(
78+
"Now, will begin building phar package.\n from path: <comment>$workDir</comment>\n" .
79+
" phar file: <info>$pharFile</info>"
80+
);
81+
82+
$out->info('Pack file to Phar: ');
83+
84+
$cpr->onError(function ($error) {
85+
$this->output->warning($error);
86+
});
87+
88+
if ($in->getOpt('debug')) {
89+
$cpr->onAdd(function ($path) {
90+
$this->output->write(" <comment>+</comment> $path");
91+
});
92+
} else {
93+
$counter = Show::counterTxt('Handling ...', 'Done.');
94+
95+
$cpr->onAdd(function () use($counter) {
96+
$counter->send(1);
97+
});
98+
}
99+
100+
// packing ...
101+
$cpr->pack($pharFile, $refresh);
102+
103+
if ($counter) {
104+
$counter->send(-1);
105+
}
106+
107+
$out->write([
108+
PHP_EOL . '<success>Phar build completed!</success>',
109+
" - Phar file: $pharFile",
110+
' - Phar size: ' . round(filesize($pharFile) / 1024 / 1024, 2) . ' Mb',
111+
' - Pack Time: ' . round(microtime(1) - $time, 3) . ' s',
112+
' - Pack File: ' . $cpr->getCounter(),
113+
' - Commit ID: ' . $cpr->getVersion(),
114+
]);
115+
116+
return 0;
117+
}
118+
119+
/**
120+
* unpack a phar package to a directory
121+
* @usage {fullCommand} -f FILE [-d DIR]
122+
* @options
123+
* -f, --file STRING The packed phar file path
124+
* -d, --dir STRING The output dir on extract phar package.
125+
* -y, --yes BOOL Whether display goon tips message.
126+
* --overwrite BOOL Whether overwrite exists files on extract phar
127+
* @example {fullCommand} -f myapp.phar -d var/www/app
128+
* @param \Inhere\Console\IO\Input $in
129+
* @param \Inhere\Console\IO\Output $out
130+
* @return int
131+
*/
132+
public function unpackCommand($in, $out): int
133+
{
134+
if (!$path = $in->getSameOpt(['f', 'file'])) {
135+
return $out->error("Please input the phar file path by option '-f|--file'");
136+
}
137+
138+
$basePath = $in->getPwd();
139+
$file = realpath($basePath . '/' . $path);
140+
141+
if (!file_exists($file)) {
142+
return $out->error("The phar file not exists. File: $file");
143+
}
144+
145+
$dir = $in->getSameOpt(['d', 'dir']) ?: $basePath;
146+
$overwrite = $in->getBoolOpt('overwrite');
147+
148+
if (!is_dir($dir)) {
149+
Helper::mkdir($dir);
150+
}
151+
152+
$out->write("Now, begin extract phar file:\n $file \nto dir:\n $dir");
153+
154+
PharCompiler::unpack($file, $dir, null, $overwrite);
155+
156+
$out->success("OK, phar package have been extract to the dir: $dir");
157+
158+
return 0;
159+
}
160+
}

examples/commands.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Inhere\Console\Examples\Commands\DemoCommand;
1212
use Inhere\Console\Examples\Commands\TestCommand;
1313
use Inhere\Console\Examples\Controllers\HomeController;
14+
use Inhere\Console\Examples\Controllers\PharController;
1415
use Inhere\Console\Examples\Controllers\ProcessController;
1516
use Inhere\Console\IO\Input;
1617
use Inhere\Console\IO\Output;
@@ -35,6 +36,7 @@
3536
$app->controller(ProcessController::class, null, [
3637
'aliases' => 'prc'
3738
]);
39+
$app->controller(PharController::class);
3840

3941
// add alias for a group command.
4042
$app->addCommandAliases('home:test', 'h-test');

src/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public function addGroup(string $name, string $controller = null)
191191
public function registerCommands(string $namespace, string $basePath)
192192
{
193193
$length = \strlen($basePath) + 1;
194-
$iterator = Helper::recursiveDirectoryIterator($basePath, $this->getFileFilter());
194+
$iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
195195

196196
foreach ($iterator as $file) {
197197
$class = $namespace . '\\' . \substr($file, $length, -4);
@@ -211,7 +211,7 @@ public function registerCommands(string $namespace, string $basePath)
211211
public function registerGroups(string $namespace, string $basePath)
212212
{
213213
$length = \strlen($basePath) + 1;
214-
$iterator = Helper::recursiveDirectoryIterator($basePath, $this->getFileFilter());
214+
$iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
215215

216216
foreach ($iterator as $file) {
217217
$class = $namespace . '\\' . \substr($file, $length, -4);

src/Base/AbstractCommand.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function annotationVars()
122122
return [
123123
'name' => self::getName(),
124124
'group' => self::getName(),
125+
'workDir' => $this->input->getPwd(),
125126
'script' => $this->input->getScript(), // bin/app
126127
'command' => $this->input->getCommand(), // demo OR home:test
127128
'fullCommand' => $this->input->getScript() . ' ' . $this->input->getCommand(),
@@ -143,7 +144,8 @@ public function run($command = '')
143144
$this->configure();
144145

145146
if ($this->input->sameOpt(['h', 'help'])) {
146-
return $this->showHelp();
147+
$this->showHelp();
148+
return 0;
147149
}
148150

149151
// some prepare check
@@ -155,7 +157,7 @@ public function run($command = '')
155157
return -1;
156158
}
157159

158-
$status = $this->execute($this->input, $this->output);
160+
$status = (int)$this->execute($this->input, $this->output);
159161
$this->afterExecute();
160162

161163
return $status;
@@ -165,7 +167,7 @@ public function run($command = '')
165167
* before command execute
166168
* @return boolean It MUST return TRUE to continue execute.
167169
*/
168-
protected function beforeExecute()
170+
protected function beforeExecute(): bool
169171
{
170172
return true;
171173
}
@@ -189,14 +191,13 @@ protected function afterExecute()
189191
* display help information
190192
* @return bool
191193
*/
192-
protected function showHelp()
194+
protected function showHelp(): bool
193195
{
194196
if (!$def = $this->getDefinition()) {
195197
return false;
196198
}
197199

198-
// 创建了 InputDefinition , 则使用它的信息。
199-
// 此时不会再解析和使用命令的注释。
200+
// 创建了 InputDefinition , 则使用它的信息(此时不会再解析和使用命令的注释)
200201
$info = $def->getSynopsis();
201202
$info['usage'] = sprintf('%s %s %s',
202203
$this->input->getScript(),
@@ -227,20 +228,17 @@ protected function prepare()
227228
}
228229
} elseif (\function_exists('setproctitle')) {
229230
setproctitle($this->processTitle);
230-
// } elseif (isDebug) {
231-
// $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>');
232231
}
233232
}
234233

235-
// do validate input arg and opt
236234
return $this->validateInput();
237235
}
238236

239237
/**
240238
* validate input arguments and options
241239
* @return bool
242240
*/
243-
public function validateInput()
241+
public function validateInput(): bool
244242
{
245243
if (!$def = $this->definition) {
246244
return true;

src/Base/ControllerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface ControllerInterface
1717
/**
1818
* @return int
1919
*/
20-
public function helpCommand();
20+
public function helpCommand(): int;
2121

2222
/**
2323
* show command list of the controller class

src/Command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ abstract class Command extends AbstractCommand implements CommandInterface
5050
// }
5151

5252
/**
53-
* @return int
53+
* @return bool
5454
*/
55-
protected function showHelp()
55+
protected function showHelp(): bool
5656
{
5757
if (true === parent::showHelp()) {
58-
return 0;
58+
return true;
5959
}
6060

6161
return $this->showHelpByMethodAnnotations('execute');

0 commit comments

Comments
 (0)