Skip to content

Commit

Permalink
some update, add a built-in controller for pack phar
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 27, 2017
1 parent d388ddf commit cdc9481
Show file tree
Hide file tree
Showing 16 changed files with 502 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ composer.lock
*.swp
*.swo
*.zip
*.phar
.DS_Store
.interactive_history
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/liteApp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

define('PROJECT_PATH', dirname(__DIR__));
define('BASE_PATH', dirname(__DIR__));

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

Expand Down
2 changes: 2 additions & 0 deletions examples/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @var Inhere\Console\Application $app
*/

use Inhere\Console\BuiltIn\PharController;
use Inhere\Console\Examples\HomeController;
use Inhere\Console\Examples\TestCommand;
use Inhere\Console\IO\Input;
Expand Down Expand Up @@ -39,3 +40,4 @@
}, 'a description message');

$app->controller('home', HomeController::class);
$app->controller(PharController::class);
28 changes: 28 additions & 0 deletions examples/tests/phar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-10-27
* Time: 10:02
*/

$srcDir = dirname(__DIR__, 2);
$pharFile = $srcDir . '/project.phar';
//var_dump($srcDir);die;

// create with alias "project.phar"
$phar = new Phar($pharFile, 0, basename($pharFile));
$phar->setSignatureAlgorithm(Phar::SHA1);

// 开始打包
$phar->startBuffering();

// add all files in the project, only include php files
$phar->buildFromDirectory($srcDir, '/[\.php|app]$/');
$phar->setStub($phar::createDefaultStub('examples/app'));
//$phar->setStub($phar::createDefaultStub('examples/app', 'www/index.php'));

$phar->stopBuffering();

// 打包完成
echo "Finished {$pharFile}\n";
43 changes: 42 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@
namespace Inhere\Console;

use Inhere\Console\Base\AbstractApplication;
use LightCms\Web\App;

/**
* Class App
* @package Inhere\Console
*/
class Application extends AbstractApplication
{
/**
* @var string|null
*/
protected $commandsNamespace;

/**
* @var string|null
*/
protected $controllersNamespace;

/**********************************************************
* register console controller/command
**********************************************************/
Expand Down Expand Up @@ -266,4 +275,36 @@ public function runAction($name, $action, $believable = false, $standAlone = fal
return $object->run($action);
}

/**
* @return null|string
*/
public function getCommandsNamespace()
{
return $this->commandsNamespace;
}

/**
* @param null|string $commandsNamespace
*/
public function setCommandsNamespace($commandsNamespace)
{
$this->commandsNamespace = $commandsNamespace;
}

/**
* @return null|string
*/
public function getControllersNamespace()
{
return $this->controllersNamespace;
}

/**
* @param null|string $controllersNamespace
*/
public function setControllersNamespace($controllersNamespace)
{
$this->controllersNamespace = $controllersNamespace;
}

}
1 change: 0 additions & 1 deletion src/Base/ApplicationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface ApplicationInterface

/**
* @param bool $exit
* @return int
*/
public function run($exit = true);

Expand Down
57 changes: 57 additions & 0 deletions src/BuiltIn/PharController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-10-27
* Time: 9:08
*/

namespace Inhere\Console\BuiltIn;

use Inhere\Console\Controller;
use Inhere\Console\Utils\PharCompiler;

/**
* Class PharController
* @package Inhere\Console\BuiltIn
*/
class PharController extends Controller
{
protected static $name = 'phar';
protected static $description = 'provide package code to phar/unpack phar tool.';

/**
* pack directory(s) to a phar file.
* @arguments
* src-dir The source directory for pack<red>*</red>
* phar The output phar file path
*
* @options
* -c,--compress Want compress php file.
* --file-include Append file include
*
*/
public function packCommand()
{
$pcr = new PharCompiler(BASE_PATH);
$pcr->setOptions([
'cliIndex' => 'examples/app',
'webIndex' => null,

'compress' => $this->getSameOpt(['c', 'compress'], false),

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

'fileInclude' => ['LICENSE', 'app', 'liteApp'],
'fileMatch' => '#\.php#',
]);

$pharFile = BASE_PATH . '/test.phar';
$count = $pcr->pack($pharFile);

$this->output->json([
'count' => $count,
'size' => round(filesize($pharFile) / 1000, 2) . ' kb',
]);
}
}
8 changes: 5 additions & 3 deletions src/LiteApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Inhere\Console;

use Inhere\Console\Style\LiteStyle;

/**
* Class LiteApplication
* @package Inhere\Console
Expand Down Expand Up @@ -227,19 +229,19 @@ public function commands(array $commands)
public function showCommands($err = '')
{
if ($err) {
echo "ERROR: $err\n\n";
echo LiteStyle::color("<red>ERROR</red>: $err\n\n");
}

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

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

echo $help . PHP_EOL;
echo LiteStyle::color($help) . PHP_EOL;
exit(0);
}

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/Style/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Inhere\Console\Style;

use Inhere\Console\Application;
use Inhere\Console\Utils\Helper;

/**
Expand Down Expand Up @@ -146,7 +147,7 @@ public function format($text)
}

// if don't support output color text, clear color tag.
if (!Helper::isSupportColor()) {
if (!Helper::isSupportColor() || Application::isNoColor()) {
return static::stripColor($text);
}

Expand Down
1 change: 1 addition & 0 deletions src/Traits/FormatOutputTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Inhere\Console\Traits;

use Inhere\Console\Application;
use Inhere\Console\Style\Style;
use Inhere\Console\Utils\Helper;
use Inhere\Console\Utils\Show;
Expand Down
8 changes: 4 additions & 4 deletions src/Traits/InputOutputTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function getRequiredArg($name)
* @see Input::getSameArg()
* {@inheritdoc}
*/
public function getSameArg($name, $default = null)
public function getSameArg(array $names, $default = null)
{
return $this->input->getSameArg($name, $default);
return $this->input->getSameArg($names, $default);
}

/**
Expand All @@ -75,9 +75,9 @@ public function getOpt($name, $default = null)
* @see Input::getSameOpt()
* {@inheritdoc}
*/
public function getSameOpt($name, $default = null)
public function getSameOpt(array $names, $default = null)
{
return $this->input->getSameOpt($name, $default);
return $this->input->getSameOpt($names, $default);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Utils/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ public static function init($object, array $options)
}
}

/**
* @param string $srcDir
* @param callable $filter
* @return \RecursiveIteratorIterator
*/
public static function recursiveDirectoryIterator(string $srcDir, callable $filter)
{
if (!$srcDir || !file_exists($srcDir)) {
throw new \LogicException('Please provide a exists source directory.');
}

$directory = new \RecursiveDirectoryIterator($srcDir);
$filterIterator = new \RecursiveCallbackFilterIterator($directory, $filter);

return new \RecursiveIteratorIterator($filterIterator);
}

/**
* wrap a style tag
* @param string $string
Expand Down
Loading

0 comments on commit cdc9481

Please sign in to comment.