Skip to content

Commit cdc9481

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

File tree

16 files changed

+502
-11
lines changed

16 files changed

+502
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
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
File renamed without changes.

examples/liteApp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 28 additions & 0 deletions
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

Lines changed: 42 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 57 additions & 0 deletions
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

Lines changed: 5 additions & 3 deletions
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.

0 commit comments

Comments
 (0)