Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit d8e0c52

Browse files
committed
update, some logic modify
1 parent a624ac8 commit d8e0c52

File tree

4 files changed

+120
-32
lines changed

4 files changed

+120
-32
lines changed

src/collections/Config.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ class Config extends DataCollector
4545
/**
4646
* __construct
4747
* @param mixed $data If mode is 'folder', $data is config folder path
48-
* @param string $format
48+
* @param array $options
4949
* @param string $name
5050
*/
51-
public function __construct($data = [], array $options = [], $name = 'config')
51+
public function __construct($data = [], $options = [], $name = 'config')
5252
{
53+
if (is_string($options)) {
54+
$options = [ 'format' => $options ];
55+
}
56+
5357
$options = array_merge([
5458
'format' => 'php',
5559
'readonly' => true,

src/console/Input.php

Lines changed: 112 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,47 @@
88

99
namespace inhere\librarys\console;
1010

11-
use inhere\librarys\io\Input as BaseInput;
12-
1311
/**
1412
* Class Input
1513
* @package inhere\librarys\console
14+
* e.g:
15+
* ./bin/app image/packTask test -d -s=df --debug=true
16+
* php bin/cli.php start test -d -s=df --debug=true
1617
*/
17-
class Input extends BaseInput
18+
class Input
1819
{
20+
/**
21+
* @var @resource
22+
*/
1923
protected $inputStream = STDIN;
2024

25+
/**
26+
* Input data
27+
* @var array
28+
*/
29+
protected $data = [];
30+
31+
/**
32+
* the script name
33+
* e.g `./bin/app` OR `bin/cli.php`
34+
* @var string
35+
*/
2136
public static $scriptName;
2237

38+
/**
39+
* the script name
40+
* e.g `image/packTask` OR `start`
41+
* @var string
42+
*/
43+
public static $command;
44+
45+
public function __construct($parseArgv = true, $fixServer = false, $fillToGlobal = false)
46+
{
47+
if ($parseArgv) {
48+
$this->data = self::parseGlobalArgv($fixServer, $fillToGlobal);
49+
}
50+
}
51+
2352
/**
2453
* @return string
2554
*/
@@ -28,6 +57,20 @@ public function read()
2857
return trim(fgets($this->inputStream));
2958
}
3059

60+
/**
61+
* @param null|string $name
62+
* @param mixed $default
63+
* @return mixed
64+
*/
65+
public function get($name=null, $default = null)
66+
{
67+
if (null === $name) {
68+
return $this->data;
69+
}
70+
71+
return isset($this->data[$name]) ? $this->data[$name] : $default;
72+
}
73+
3174
/**
3275
* @param $key
3376
* @param bool $default
@@ -41,7 +84,31 @@ public function getBool($key, $default = false)
4184
return $default;
4285
}
4386

44-
return in_array($value, ['0', 0, 'false'], true) ? false : true;
87+
return in_array($value, ['0', 0, 'false', false], true) ? false : true;
88+
}
89+
90+
/**
91+
* @return string
92+
*/
93+
public function getScriptName()
94+
{
95+
return self::$scriptName;
96+
}
97+
98+
/**
99+
* @return string
100+
*/
101+
public function getCommand()
102+
{
103+
return self::$command;
104+
}
105+
106+
/**
107+
* @return string
108+
*/
109+
public function getData()
110+
{
111+
return $this->data;
45112
}
46113

47114
/**
@@ -52,44 +119,64 @@ public function getInputStream()
52119
return $this->inputStream;
53120
}
54121

55-
56122
/**
57-
* @param array $argv
58123
*/
59-
public static function parseConsoleArgs($argv)
124+
public static function parseGlobalArgv($fixServer = false, $fillToGlobal = false)
60125
{
61-
// fixed: '/home' is not equals to '/home/'
62-
if (isset($_SERVER['REQUEST_URI'])) {
63-
$_SERVER['REQUEST_URI'] = rtrim($_SERVER['REQUEST_URI'],'/ ');
64-
}
126+
// ./bin/app image/packTask start test -d -s=df --debug=true
127+
// php bin/cli.php image/packTask start test -d -s=df --debug=true
128+
global $argv;
129+
$args = $argv;
65130

66-
// fixed: PHP_SELF = 'index.php', it is should be '/index.php'
67-
if (isset($_SERVER['PHP_SELF'])) {
68-
$_SERVER['PHP_SELF'] = '/' . ltrim($_SERVER['PHP_SELF'],'/ ');
131+
if ($args[0] === 'php') {
132+
array_shift($args);
69133
}
70134

71-
self::$scriptName = array_shift($argv);
135+
self::$scriptName = array_shift($args);
136+
137+
if ($fixServer) {
138+
// fixed: '/home' is not equals to '/home/'
139+
if (isset($_SERVER['REQUEST_URI'])) {
140+
$_SERVER['REQUEST_URI'] = rtrim($_SERVER['REQUEST_URI'],'/ ');
141+
}
72142

73-
// $_SERVER['PHP_SELF'] = self::$scriptName;
74-
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
75-
$_SERVER['REQUEST_METHOD'] = 'GET';
76-
$_SERVER['REQUEST_URI'] = '/';
143+
// fixed: PHP_SELF = 'index.php', it is should be '/index.php'
144+
if (isset($_SERVER['PHP_SELF'])) {
145+
$_SERVER['PHP_SELF'] = '/' . ltrim($_SERVER['PHP_SELF'],'/ ');
146+
}
77147

78-
if ( isset($argv[0]) && strpos($argv[0], '=') === false ) {
79-
$path = trim(array_shift($argv), '/');
80148

81-
$_SERVER['REQUEST_URI'] .= $path;
149+
// $_SERVER['PHP_SELF'] = self::$scriptName;
150+
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
151+
$_SERVER['REQUEST_METHOD'] = 'GET';
152+
$_SERVER['REQUEST_URI'] = '/';
82153
}
83154

155+
// collect command
156+
if ( isset($args[0]) && strpos($args[0], '=') === false ) {
157+
self::$command = trim(array_shift($args), '/');
158+
159+
if ($fixServer) {
160+
$_SERVER['REQUEST_URI'] .= self::$command;
161+
}
162+
}
163+
164+
$data = [];
165+
84166
// parse query params
85167
// ./bin/app image/packTask start test -d -s=df --debug=true
86168
// parse to
87169
// ./bin/app image/packTask?start&test&d&s=df&debug=true
88-
if ($argv) {
89-
$url = preg_replace('/&[-]+/', '&', implode('&',$argv));
170+
if ($args) {
171+
$url = preg_replace('/&[-]+/', '&', implode('&',$args));
90172

91173
parse_str($url, $data);
92-
$_REQUEST = $_GET = $data;
174+
175+
if ($fillToGlobal) {
176+
$_REQUEST = $_GET = $data;
177+
}
93178
}
179+
180+
return $data;
94181
}
95182
}

src/console/Interact.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ public static function block($messages, $type = null, $style='default')
260260
$text = "<{$style}>$text</{$style}>";
261261
}
262262

263-
$this->write($text);
264-
263+
// $this->write($text);
265264
static::write($text);
266265
}
267266

src/console/Output.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
namespace inhere\librarys\console;
1010

11-
use inhere\librarys\StdBase;
12-
1311
/**
1412
* Class Output
1513
* @package inhere\librarys\console
1614
*/
17-
class Output extends StdBase
15+
class Output
1816
{
1917
/**
2018
* 正常输出流

0 commit comments

Comments
 (0)