Skip to content

Commit

Permalink
complete phar package tool. add a example controller for pack phar
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 22, 2018
1 parent bfde581 commit 189051b
Show file tree
Hide file tree
Showing 10 changed files with 746 additions and 154 deletions.
160 changes: 160 additions & 0 deletions examples/Controllers/PharController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2018-01-22
* Time: 11:55
*/

namespace Inhere\Console\Examples\Controllers;

use Inhere\Console\Components\PharCompiler;
use Inhere\Console\Controller;
use Inhere\Console\Utils\Helper;
use Inhere\Console\Utils\Show;

/**
* Class PharController
* @package Inhere\Console\Examples\Controllers
*/
class PharController extends Controller
{
protected static $name = 'phar';
protected static $description = 'Pack a project directory to phar or unpack phar to directory';

/**
* pack project to a phar package
* @usage {fullCommand} [--dir DIR] [--output FILE]
* @options
* --dir STRING Setting the directory for packing.
* - default is current work-dir.(<comment>{workDir}</comment>)
* --output STRING Setting the output file name(<comment>app.phar</comment>)
* --refresh BOOL Whether build vendor folder files on phar file exists(<comment>False</comment>)
* @param \Inhere\Console\IO\Input $in
* @param \Inhere\Console\IO\Output $out
* @return int
*/
public function packCommand($in, $out): int
{
$time = microtime(1);
$workDir = $in->getPwd();
$dir = $in->getOpt('dir') ?: $workDir;
$pharFile = $workDir . '/' . $in->getOpt('output', 'app.phar');

$cpr = new PharCompiler($dir);

// config
$cpr
// ->stripComments(false)
->setShebang(true)
->addExclude([
'demo',
'tests',
'tmp',
])
->addFile([
'LICENSE',
'composer.json',
'README.md',
'tests/boot.php',
])
->setCliIndex('examples/app')
// ->setWebIndex('web/index.php')
// ->setVersionFile('config/config.php')
->in($dir)
;

$cpr->setStripFilter(function ($file) {
/** @var \SplFileInfo $file */
$name = $file->getFilename();

return false === strpos($name, 'Command.php') && false === strpos($name, 'Controller.php');
});

$counter = null;
$refresh = $in->boolOpt('refresh');

$out->liteInfo(
"Now, will begin building phar package.\n from path: <comment>$workDir</comment>\n" .
" phar file: <info>$pharFile</info>"
);

$out->info('Pack file to Phar: ');

$cpr->onError(function ($error) {
$this->output->warning($error);
});

if ($in->getOpt('debug')) {
$cpr->onAdd(function ($path) {
$this->output->write(" <comment>+</comment> $path");
});
} else {
$counter = Show::counterTxt('Handling ...', 'Done.');

$cpr->onAdd(function () use($counter) {
$counter->send(1);
});
}

// packing ...
$cpr->pack($pharFile, $refresh);

if ($counter) {
$counter->send(-1);
}

$out->write([
PHP_EOL . '<success>Phar build completed!</success>',
" - Phar file: $pharFile",
' - Phar size: ' . round(filesize($pharFile) / 1024 / 1024, 2) . ' Mb',
' - Pack Time: ' . round(microtime(1) - $time, 3) . ' s',
' - Pack File: ' . $cpr->getCounter(),
' - Commit ID: ' . $cpr->getVersion(),
]);

return 0;
}

/**
* unpack a phar package to a directory
* @usage {fullCommand} -f FILE [-d DIR]
* @options
* -f, --file STRING The packed phar file path
* -d, --dir STRING The output dir on extract phar package.
* -y, --yes BOOL Whether display goon tips message.
* --overwrite BOOL Whether overwrite exists files on extract phar
* @example {fullCommand} -f myapp.phar -d var/www/app
* @param \Inhere\Console\IO\Input $in
* @param \Inhere\Console\IO\Output $out
* @return int
*/
public function unpackCommand($in, $out): int
{
if (!$path = $in->getSameOpt(['f', 'file'])) {
return $out->error("Please input the phar file path by option '-f|--file'");
}

$basePath = $in->getPwd();
$file = realpath($basePath . '/' . $path);

if (!file_exists($file)) {
return $out->error("The phar file not exists. File: $file");
}

$dir = $in->getSameOpt(['d', 'dir']) ?: $basePath;
$overwrite = $in->getBoolOpt('overwrite');

if (!is_dir($dir)) {
Helper::mkdir($dir);
}

$out->write("Now, begin extract phar file:\n $file \nto dir:\n $dir");

PharCompiler::unpack($file, $dir, null, $overwrite);

$out->success("OK, phar package have been extract to the dir: $dir");

return 0;
}
}
2 changes: 2 additions & 0 deletions examples/commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Inhere\Console\Examples\Commands\DemoCommand;
use Inhere\Console\Examples\Commands\TestCommand;
use Inhere\Console\Examples\Controllers\HomeController;
use Inhere\Console\Examples\Controllers\PharController;
use Inhere\Console\Examples\Controllers\ProcessController;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
Expand All @@ -35,6 +36,7 @@
$app->controller(ProcessController::class, null, [
'aliases' => 'prc'
]);
$app->controller(PharController::class);

// add alias for a group command.
$app->addCommandAliases('home:test', 'h-test');
4 changes: 2 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function addGroup(string $name, string $controller = null)
public function registerCommands(string $namespace, string $basePath)
{
$length = \strlen($basePath) + 1;
$iterator = Helper::recursiveDirectoryIterator($basePath, $this->getFileFilter());
$iterator = Helper::directoryIterator($basePath, $this->getFileFilter());

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

foreach ($iterator as $file) {
$class = $namespace . '\\' . \substr($file, $length, -4);
Expand Down
18 changes: 8 additions & 10 deletions src/Base/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function annotationVars()
return [
'name' => self::getName(),
'group' => self::getName(),
'workDir' => $this->input->getPwd(),
'script' => $this->input->getScript(), // bin/app
'command' => $this->input->getCommand(), // demo OR home:test
'fullCommand' => $this->input->getScript() . ' ' . $this->input->getCommand(),
Expand All @@ -143,7 +144,8 @@ public function run($command = '')
$this->configure();

if ($this->input->sameOpt(['h', 'help'])) {
return $this->showHelp();
$this->showHelp();
return 0;
}

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

$status = $this->execute($this->input, $this->output);
$status = (int)$this->execute($this->input, $this->output);
$this->afterExecute();

return $status;
Expand All @@ -165,7 +167,7 @@ public function run($command = '')
* before command execute
* @return boolean It MUST return TRUE to continue execute.
*/
protected function beforeExecute()
protected function beforeExecute(): bool
{
return true;
}
Expand All @@ -189,14 +191,13 @@ protected function afterExecute()
* display help information
* @return bool
*/
protected function showHelp()
protected function showHelp(): bool
{
if (!$def = $this->getDefinition()) {
return false;
}

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

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

/**
* validate input arguments and options
* @return bool
*/
public function validateInput()
public function validateInput(): bool
{
if (!$def = $this->definition) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Base/ControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ControllerInterface
/**
* @return int
*/
public function helpCommand();
public function helpCommand(): int;

/**
* show command list of the controller class
Expand Down
6 changes: 3 additions & 3 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ abstract class Command extends AbstractCommand implements CommandInterface
// }

/**
* @return int
* @return bool
*/
protected function showHelp()
protected function showHelp(): bool
{
if (true === parent::showHelp()) {
return 0;
return true;
}

return $this->showHelpByMethodAnnotations('execute');
Expand Down
Loading

0 comments on commit 189051b

Please sign in to comment.