Skip to content

Commit cdc9481

Browse files
committed
some update, add a built-in controller for pack phar
1 parent d388ddf commit cdc9481

16 files changed

+502
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ composer.lock
1111
*.swp
1212
*.swo
1313
*.zip
14+
*.phar
1415
.DS_Store
1516
.interactive_history

LICENSE.md LICENSE

File renamed without changes.

examples/liteApp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env php
22
<?php
33

4-
define('PROJECT_PATH', dirname(__DIR__));
4+
define('BASE_PATH', dirname(__DIR__));
55

66
require __DIR__ . '/s-autoload.php';
77

examples/routes.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @var Inhere\Console\Application $app
99
*/
1010

11+
use Inhere\Console\BuiltIn\PharController;
1112
use Inhere\Console\Examples\HomeController;
1213
use Inhere\Console\Examples\TestCommand;
1314
use Inhere\Console\IO\Input;
@@ -39,3 +40,4 @@
3940
}, 'a description message');
4041

4142
$app->controller('home', HomeController::class);
43+
$app->controller(PharController::class);

examples/tests/phar.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-27
6+
* Time: 10:02
7+
*/
8+
9+
$srcDir = dirname(__DIR__, 2);
10+
$pharFile = $srcDir . '/project.phar';
11+
//var_dump($srcDir);die;
12+
13+
// create with alias "project.phar"
14+
$phar = new Phar($pharFile, 0, basename($pharFile));
15+
$phar->setSignatureAlgorithm(Phar::SHA1);
16+
17+
// 开始打包
18+
$phar->startBuffering();
19+
20+
// add all files in the project, only include php files
21+
$phar->buildFromDirectory($srcDir, '/[\.php|app]$/');
22+
$phar->setStub($phar::createDefaultStub('examples/app'));
23+
//$phar->setStub($phar::createDefaultStub('examples/app', 'www/index.php'));
24+
25+
$phar->stopBuffering();
26+
27+
// 打包完成
28+
echo "Finished {$pharFile}\n";

src/Application.php

+42-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@
99
namespace Inhere\Console;
1010

1111
use Inhere\Console\Base\AbstractApplication;
12-
use LightCms\Web\App;
1312

1413
/**
1514
* Class App
1615
* @package Inhere\Console
1716
*/
1817
class Application extends AbstractApplication
1918
{
19+
/**
20+
* @var string|null
21+
*/
22+
protected $commandsNamespace;
23+
24+
/**
25+
* @var string|null
26+
*/
27+
protected $controllersNamespace;
28+
2029
/**********************************************************
2130
* register console controller/command
2231
**********************************************************/
@@ -266,4 +275,36 @@ public function runAction($name, $action, $believable = false, $standAlone = fal
266275
return $object->run($action);
267276
}
268277

278+
/**
279+
* @return null|string
280+
*/
281+
public function getCommandsNamespace()
282+
{
283+
return $this->commandsNamespace;
284+
}
285+
286+
/**
287+
* @param null|string $commandsNamespace
288+
*/
289+
public function setCommandsNamespace($commandsNamespace)
290+
{
291+
$this->commandsNamespace = $commandsNamespace;
292+
}
293+
294+
/**
295+
* @return null|string
296+
*/
297+
public function getControllersNamespace()
298+
{
299+
return $this->controllersNamespace;
300+
}
301+
302+
/**
303+
* @param null|string $controllersNamespace
304+
*/
305+
public function setControllersNamespace($controllersNamespace)
306+
{
307+
$this->controllersNamespace = $controllersNamespace;
308+
}
309+
269310
}

src/Base/ApplicationInterface.php

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ interface ApplicationInterface
2323

2424
/**
2525
* @param bool $exit
26-
* @return int
2726
*/
2827
public function run($exit = true);
2928

src/BuiltIn/PharController.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-27
6+
* Time: 9:08
7+
*/
8+
9+
namespace Inhere\Console\BuiltIn;
10+
11+
use Inhere\Console\Controller;
12+
use Inhere\Console\Utils\PharCompiler;
13+
14+
/**
15+
* Class PharController
16+
* @package Inhere\Console\BuiltIn
17+
*/
18+
class PharController extends Controller
19+
{
20+
protected static $name = 'phar';
21+
protected static $description = 'provide package code to phar/unpack phar tool.';
22+
23+
/**
24+
* pack directory(s) to a phar file.
25+
* @arguments
26+
* src-dir The source directory for pack<red>*</red>
27+
* phar The output phar file path
28+
*
29+
* @options
30+
* -c,--compress Want compress php file.
31+
* --file-include Append file include
32+
*
33+
*/
34+
public function packCommand()
35+
{
36+
$pcr = new PharCompiler(BASE_PATH);
37+
$pcr->setOptions([
38+
'cliIndex' => 'examples/app',
39+
'webIndex' => null,
40+
41+
'compress' => $this->getSameOpt(['c', 'compress'], false),
42+
43+
'dirExclude' => '#[\.git|tests]#',
44+
45+
'fileInclude' => ['LICENSE', 'app', 'liteApp'],
46+
'fileMatch' => '#\.php#',
47+
]);
48+
49+
$pharFile = BASE_PATH . '/test.phar';
50+
$count = $pcr->pack($pharFile);
51+
52+
$this->output->json([
53+
'count' => $count,
54+
'size' => round(filesize($pharFile) / 1000, 2) . ' kb',
55+
]);
56+
}
57+
}

src/LiteApplication.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Inhere\Console;
1010

11+
use Inhere\Console\Style\LiteStyle;
12+
1113
/**
1214
* Class LiteApplication
1315
* @package Inhere\Console
@@ -227,19 +229,19 @@ public function commands(array $commands)
227229
public function showCommands($err = '')
228230
{
229231
if ($err) {
230-
echo "ERROR: $err\n\n";
232+
echo LiteStyle::color("<red>ERROR</red>: $err\n\n");
231233
}
232234

233235
$commandWidth = 12;
234-
$help = "Welcome to the Lite Console Application.\n\nAvailable Commands:\n";
236+
$help = "Welcome to the Lite Console Application.\n\n<comment>Available Commands:</comment>\n";
235237

236238
foreach ($this->messages as $command => $desc) {
237239
$command = str_pad($command, $commandWidth, ' ');
238240
$desc = $desc ?: 'No description for the command';
239241
$help .= " $command $desc\n";
240242
}
241243

242-
echo $help . PHP_EOL;
244+
echo LiteStyle::color($help) . PHP_EOL;
243245
exit(0);
244246
}
245247

File renamed without changes.

src/Style/Style.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Inhere\Console\Style;
1212

13+
use Inhere\Console\Application;
1314
use Inhere\Console\Utils\Helper;
1415

1516
/**
@@ -146,7 +147,7 @@ public function format($text)
146147
}
147148

148149
// if don't support output color text, clear color tag.
149-
if (!Helper::isSupportColor()) {
150+
if (!Helper::isSupportColor() || Application::isNoColor()) {
150151
return static::stripColor($text);
151152
}
152153

src/Traits/FormatOutputTrait.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Inhere\Console\Traits;
1010

11+
use Inhere\Console\Application;
1112
use Inhere\Console\Style\Style;
1213
use Inhere\Console\Utils\Helper;
1314
use Inhere\Console\Utils\Show;

src/Traits/InputOutputTrait.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function getRequiredArg($name)
5757
* @see Input::getSameArg()
5858
* {@inheritdoc}
5959
*/
60-
public function getSameArg($name, $default = null)
60+
public function getSameArg(array $names, $default = null)
6161
{
62-
return $this->input->getSameArg($name, $default);
62+
return $this->input->getSameArg($names, $default);
6363
}
6464

6565
/**
@@ -75,9 +75,9 @@ public function getOpt($name, $default = null)
7575
* @see Input::getSameOpt()
7676
* {@inheritdoc}
7777
*/
78-
public function getSameOpt($name, $default = null)
78+
public function getSameOpt(array $names, $default = null)
7979
{
80-
return $this->input->getSameOpt($name, $default);
80+
return $this->input->getSameOpt($names, $default);
8181
}
8282

8383
/**

src/Utils/Helper.php

+17
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ public static function init($object, array $options)
8888
}
8989
}
9090

91+
/**
92+
* @param string $srcDir
93+
* @param callable $filter
94+
* @return \RecursiveIteratorIterator
95+
*/
96+
public static function recursiveDirectoryIterator(string $srcDir, callable $filter)
97+
{
98+
if (!$srcDir || !file_exists($srcDir)) {
99+
throw new \LogicException('Please provide a exists source directory.');
100+
}
101+
102+
$directory = new \RecursiveDirectoryIterator($srcDir);
103+
$filterIterator = new \RecursiveCallbackFilterIterator($directory, $filter);
104+
105+
return new \RecursiveIteratorIterator($filterIterator);
106+
}
107+
91108
/**
92109
* wrap a style tag
93110
* @param string $string

0 commit comments

Comments
 (0)