Skip to content

Commit 891d84f

Browse files
committed
some update
1 parent 0d00a70 commit 891d84f

File tree

6 files changed

+86
-19
lines changed

6 files changed

+86
-19
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# php 命令行应用库
22

3+
[![License](https://img.shields.io/packagist/l/inhere/console.svg?style=flat-square)](LICENSE-2.0.txt)
4+
[![Php Version](https://img.shields.io/badge/php-%3E=7.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/console)
5+
[![Latest Stable Version](http://img.shields.io/packagist/v/inhere/console.svg)](https://packagist.org/packages/inhere/console)
6+
37
简洁、功能全面的php命令行应用库。提供控制台参数解析, 颜色风格输出, 用户信息交互, 特殊格式信息显示。
48

59
> 无其他库依赖,可以方便的整合到任何已有项目中。

README_en.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# console tool
1+
# console application lib
2+
3+
[![License](https://img.shields.io/packagist/l/inhere/console.svg?style=flat-square)](LICENSE-2.0.txt)
4+
[![Php Version](https://img.shields.io/badge/php-%3E=7.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/console)
5+
[![Latest Stable Version](http://img.shields.io/packagist/v/inhere/console.svg)](https://packagist.org/packages/inhere/console)
26

37
a php console application library.
48

examples/home

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
use Inhere\Console\Examples\HomeController;
55

6-
define('PROJECT_PATH', dirname(__DIR__));
6+
define('BASE_PATH', dirname(__DIR__));
77

88
require __DIR__ . '/s-autoload.php';
99

1010
$app = new \Inhere\Console\Application([
1111
'debug' => true,
12-
'rootPath' => PROJECT_PATH,
12+
'rootPath' => BASE_PATH,
1313
]);
1414

1515
$app->controller('home', HomeController::class);

src/BuiltIn/PharController.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Inhere\Console\BuiltIn;
1010

11+
use Inhere\Console\Components\PharBuilder;
1112
use Inhere\Console\Controller;
1213
use Inhere\Console\Components\PharCompiler;
1314

@@ -60,4 +61,30 @@ public function packCommand()
6061
'size' => round(filesize($pharFile) / 1000, 2) . ' kb',
6162
]);
6263
}
64+
65+
/**
66+
* pack directory(s) to a phar file.
67+
*/
68+
public function buildCommand()
69+
{
70+
$packer = new PharBuilder(BASE_PATH);
71+
72+
$packer->addDirectory(BASE_PATH);
73+
$packer->setOptions([
74+
'cliIndex' => 'examples/app',
75+
'webIndex' => null,
76+
77+
// 'compress' => $this->getSameOpt(['c', 'compress'], false),
78+
79+
'dirExclude' => '#[\.git|tests]#',
80+
81+
'fileInclude' => ['LICENSE', 'app', 'liteApp'],
82+
'fileMatch' => '#\.php$#',
83+
]);
84+
$packer->build($pharFile = BASE_PATH . '/example.phar');
85+
86+
$this->output->json([
87+
'size' => round(filesize($pharFile) / 1000, 2) . ' kb',
88+
]);
89+
}
6390
}

src/Components/PharBuilder.php

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
class PharBuilder
1818
{
19-
private $scanner;
20-
2119
/** @var int @see \Phar::GZ, \Phar::BZ2 */
2220
private $compressMode;
2321

@@ -52,17 +50,17 @@ class PharBuilder
5250
'webIndex' => null,
5351

5452
// compress php code
55-
'compress' => false,
53+
// 'compress' => false,
5654

5755
'dirExclude' => '#[\.git|tests]#',
5856

5957
'fileInclude' => [],
58+
'fileExclude' => [],
6059
'fileMatch' => '#\.php#',
6160
];
6261

63-
public function __construct(DirectoryScanner $scanner, $basedir)
62+
public function __construct($basedir)
6463
{
65-
$this->scanner = $scanner;
6664
$this->basedir = $basedir;
6765
}
6866

@@ -89,25 +87,35 @@ public function setSignatureType($type)
8987
$this->signatureType = $type;
9088
}
9189

90+
/**
91+
* @param $key
92+
*/
9293
public function setSignatureKey($key)
9394
{
9495
$this->key = $key;
9596
}
9697

98+
/**
99+
* @param string $directory
100+
*/
97101
public function addDirectory($directory)
98102
{
99103
$this->directories[] = $directory;
100104
}
101105

106+
/**
107+
* @param $name
108+
*/
102109
public function setAliasName($name)
103110
{
104111
$this->aliasName = $name;
105112
}
106113

107114
/**
108-
* @param $filename
109-
* @param $stub
110-
* @throws \LogicException
115+
* @param string $filename
116+
* @param string $stub
117+
* @throws \RuntimeException
118+
* @throws \InvalidArgumentException
111119
* @throws \UnexpectedValueException
112120
* @throws \BadMethodCallException
113121
*/
@@ -117,6 +125,14 @@ public function build($filename, $stub = null)
117125
unlink($filename);
118126
}
119127

128+
if (ini_get('phar.readonly')) {
129+
throw new \RuntimeException("The 'phar.readonly' is 'On', build phar must setting it 'Off'");
130+
}
131+
132+
if (!$this->directories) {
133+
throw new \RuntimeException("Please setting the 'directories' want building directories");
134+
}
135+
120136
$aliasName = $this->aliasName ?: basename($filename);
121137
$phar = new \Phar($filename, 0, $aliasName);
122138
$phar->startBuffering();
@@ -140,7 +156,7 @@ public function build($filename, $stub = null)
140156
$phar->buildFromIterator($iterator, $basedir);
141157
}
142158

143-
if ($this->compressMode !== \Phar::NONE) {
159+
if ($this->compressMode !== null) {
144160
$phar->compressFiles($this->compressMode);
145161
}
146162

@@ -151,14 +167,14 @@ private function createStub($pharName)
151167
{
152168
// Stubs
153169
// $phar->setStub($this->getStub());
154-
$stub = \Phar::createDefaultStub($this->options['cliIndex'], $this->options['webIndex']);
170+
return \Phar::createDefaultStub($this->options['cliIndex'], $this->options['webIndex']);
155171

156172
// 设置入口
157-
return"<?php
158-
Phar::mapPhar('{$pharName}');
159-
require 'phar://{$pharName}/examples/app';
160-
__HALT_COMPILER();
161-
?>";
173+
// return "<?php
174+
//Phar::mapPhar('{$pharName}');
175+
//require 'phar://{$pharName}/examples/app';
176+
//__HALT_COMPILER();
177+
/*?>";*/
162178
}
163179

164180
/**
@@ -231,4 +247,20 @@ public function setKey($key)
231247

232248
return $this;
233249
}
250+
251+
/**
252+
* @return array
253+
*/
254+
public function getOptions(): array
255+
{
256+
return $this->options;
257+
}
258+
259+
/**
260+
* @param array $options
261+
*/
262+
public function setOptions(array $options)
263+
{
264+
$this->options = $options;
265+
}
234266
}

src/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ final public function showCommandList()
207207
]);
208208

209209
$this->write(sprintf(
210-
"To see more information about a command, please use: <cyan>$script {command} -h</cyan>",
210+
"More information about a command, please use: <cyan>$script $name{command} -h</cyan>",
211211
$this->standAlone ? ' ' . $name : ''
212212
));
213213
}

0 commit comments

Comments
 (0)