Skip to content

Commit 5b0e0fd

Browse files
committed
Merge pull request #37 from connectstudios/cleanup-autoloading_etc
Cleanup autoloading and comments
2 parents db979f4 + 69e4c73 commit 5b0e0fd

File tree

19 files changed

+145
-133
lines changed

19 files changed

+145
-133
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,38 @@ Requirements
1010
Function List
1111
-------------
1212

13-
* `\cli\out($msg, ...)`
14-
* `\cli\out_padded($msg, ...)`
15-
* `\cli\err($msg, ...)`
16-
* `\cli\line($msg = '', ...)`
17-
* `\cli\input()`
18-
* `\cli\prompt($question, $default = false, $marker = ':')`
19-
* `\cli\choose($question, $choices = 'yn', $default = 'n')`
20-
* `\cli\menu($items, $default = false, $title = 'Choose an Item')`
13+
* `cli\out($msg, ...)`
14+
* `cli\out_padded($msg, ...)`
15+
* `cli\err($msg, ...)`
16+
* `cli\line($msg = '', ...)`
17+
* `cli\input()`
18+
* `cli\prompt($question, $default = false, $marker = ':')`
19+
* `cli\choose($question, $choices = 'yn', $default = 'n')`
20+
* `cli\menu($items, $default = false, $title = 'Choose an Item')`
2121

2222
Progress Indicators
2323
-------------------
2424

25-
* `\cli\notifier\Dots($msg, $dots = 3, $interval = 100)`
26-
* `\cli\notifier\Spinner($msg, $interval = 100)`
27-
* `\cli\progress\Bar($msg, $total, $interval = 100)`
25+
* `cli\notifier\Dots($msg, $dots = 3, $interval = 100)`
26+
* `cli\notifier\Spinner($msg, $interval = 100)`
27+
* `cli\progress\Bar($msg, $total, $interval = 100)`
2828

2929
Tabular Display
3030
---------------
3131

32-
* `\cli\Table::__construct(array $headers = null, array $rows = null)`
33-
* `\cli\Table::setHeaders(array $headers)`
34-
* `\cli\Table::setRows(array $rows)`
35-
* `\cli\Table::setRenderer(\cli\table\Renderer $renderer)`
36-
* `\cli\Table::addRow(array $row)`
37-
* `\cli\Table::sort($column)`
38-
* `\cli\Table::display()`
32+
* `cli\Table::__construct(array $headers = null, array $rows = null)`
33+
* `cli\Table::setHeaders(array $headers)`
34+
* `cli\Table::setRows(array $rows)`
35+
* `cli\Table::setRenderer(cli\table\Renderer $renderer)`
36+
* `cli\Table::addRow(array $row)`
37+
* `cli\Table::sort($column)`
38+
* `cli\Table::display()`
3939

4040
The display function will detect if output is piped and, if it is, render a tab delimited table instead of the ASCII
4141
table rendered for visual display.
4242

43-
You can also explicitly set the renderer used by calling `\cli\Table::setRenderer()` and giving it an instance of one
44-
of the concrete `\cli\table\Renderer` classes.
43+
You can also explicitly set the renderer used by calling `cli\Table::setRenderer()` and giving it an instance of one
44+
of the concrete `cli\table\Renderer` classes.
4545

4646
Argument Parser
4747
---------------

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
"php": ">= 5.3.0"
1717
},
1818
"autoload": {
19-
"psr-0": {"cli": "lib/"},
20-
"files": ["lib/cli/cli.php"]
19+
"psr-0": {
20+
"cli": "lib/"
21+
},
22+
"files": [
23+
"lib/cli/cli.php"
24+
]
2125
}
2226
}

examples/common.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
ini_set('log_errors', 0);
1010
ini_set('html_errors', 0);
1111

12-
require 'lib/cli/cli.php';
13-
\cli\register_autoload();
12+
require_once __DIR__ . '/../vendor/autoload.php';
1413

15-
function test_notify(\cli\Notify $notify, $cycle = 1000000, $sleep = null) {
14+
function test_notify(cli\Notify $notify, $cycle = 1000000, $sleep = null) {
1615
for ($i = 0; $i <= $cycle; $i++) {
1716
$notify->tick();
1817
if ($sleep) usleep($sleep);

http-console.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
* An example application using php-cli-tools and Buzz
44
*/
55

6+
require_once __DIR__ . '/vendor/autoload.php';
7+
68
define('BUZZ_PATH', realpath('../Buzz'));
7-
define('TOOL_PATH', realpath('./'));
89
define('SCRIPT_NAME', array_shift($argv));
910

1011
require_once BUZZ_PATH . '/lib/Buzz/ClassLoader.php';
1112
Buzz\ClassLoader::register();
1213

13-
require_once TOOL_PATH . '/lib/cli/cli.php';
14-
\cli\register_autoload();
15-
1614
class HttpConsole {
1715
protected $_host;
1816
protected $_prompt;

lib/cli/Arguments.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212

1313
namespace cli;
1414

15-
require 'lib/cli/Memoize.php';
16-
require 'lib/cli/arguments/Argument.php';
17-
require 'lib/cli/arguments/HelpScreen.php';
18-
require 'lib/cli/arguments/InvalidArguments.php';
19-
require 'lib/cli/arguments/Lexer.php';
15+
use cli\arguments\Argument;
16+
use cli\arguments\HelpScreen;
17+
use cli\arguments\InvalidArguments;
18+
use cli\arguments\Lexer;
2019

2120
/**
2221
* Parses command line arguments.
@@ -69,7 +68,7 @@ public function getArguments() {
6968
}
7069

7170
public function getHelpScreen() {
72-
return new \cli\arguments\HelpScreen($this);
71+
return new HelpScreen($this);
7372
}
7473

7574
/**
@@ -88,7 +87,7 @@ public function asJSON() {
8887
* @return bool
8988
*/
9089
public function offsetExists($offset) {
91-
if ($offset instanceOf \cli\arguments\Argument) {
90+
if ($offset instanceOf Argument) {
9291
$offset = $offset->key;
9392
}
9493

@@ -102,7 +101,7 @@ public function offsetExists($offset) {
102101
* @return mixed
103102
*/
104103
public function offsetGet($offset) {
105-
if ($offset instanceOf \cli\arguments\Argument) {
104+
if ($offset instanceOf Argument) {
106105
$offset = $offset->key;
107106
}
108107

@@ -118,7 +117,7 @@ public function offsetGet($offset) {
118117
* @param mixed $value The value to set
119118
*/
120119
public function offsetSet($offset, $value) {
121-
if ($offset instanceOf \cli\arguments\Argument) {
120+
if ($offset instanceOf Argument) {
122121
$offset = $offset->key;
123122
}
124123

@@ -131,7 +130,7 @@ public function offsetSet($offset, $value) {
131130
* @param mixed $offset An Argument object or the name of the argument.
132131
*/
133132
public function offsetUnset($offset) {
134-
if ($offset instanceOf \cli\arguments\Argument) {
133+
if ($offset instanceOf Argument) {
135134
$offset = $offset->key;
136135
}
137136

@@ -197,7 +196,7 @@ public function addFlags($flags) {
197196
/**
198197
* Adds an option (string argument) to the argument list.
199198
*
200-
* @param mixed $flag A string representing the option, or an array of strings.
199+
* @param mixed $option A string representing the option, or an array of strings.
201200
* @param array $settings An array of settings for this option.
202201
* @setting string description A description to be shown in --help.
203202
* @setting bool default The default value for this option.
@@ -280,7 +279,7 @@ public function getInvalidArguments() {
280279
* @return array
281280
*/
282281
public function getFlag($flag) {
283-
if ($flag instanceOf \cli\arguments\Argument) {
282+
if ($flag instanceOf Argument) {
284283
$obj = $flag;
285284
$flag = $flag->value;
286285
}
@@ -341,7 +340,7 @@ public function isStackable($flag) {
341340
* @return array
342341
*/
343342
public function getOption($option) {
344-
if ($option instanceOf \cli\arguments\Argument) {
343+
if ($option instanceOf Argument) {
345344
$obj = $option;
346345
$option = $option->value;
347346
}
@@ -386,11 +385,12 @@ public function isOption($argument) {
386385
* if a long name is not given.
387386
*
388387
* @return array
388+
* @throws arguments\InvalidArguments
389389
*/
390390
public function parse() {
391391
$this->_invalid = array();
392392
$this->_parsed = array();
393-
$this->_lexer = new \cli\arguments\Lexer($this->_input);
393+
$this->_lexer = new Lexer($this->_input);
394394

395395
foreach ($this->_lexer as $argument) {
396396
if ($this->_parseFlag($argument)) {
@@ -404,7 +404,7 @@ public function parse() {
404404
}
405405

406406
if ($this->_strict && !empty($this->_invalid)) {
407-
throw new \cli\arguments\InvalidArguments($this->_invalid);
407+
throw new InvalidArguments($this->_invalid);
408408
}
409409
}
410410

lib/cli/Colors.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static public function shouldColorize($colored = null) {
7474
* Set the color.
7575
*
7676
* @param string $color The name of the color or style to set.
77+
* @return string
7778
*/
7879
static public function color($color) {
7980
if (!is_array($color)) {
@@ -105,7 +106,9 @@ static public function color($color) {
105106
* Colorize a string using helpful string formatters. If the `Streams::$out` points to a TTY coloring will be enabled,
106107
* otherwise disabled. You can control this check with the `$colored` parameter.
107108
*
109+
* @param string $string
108110
* @param boolean $colored Force enable or disable the colorized output. If left as `null` the TTY will control coloring.
111+
* @return string
109112
*/
110113
static public function colorize($string, $colored = null) {
111114
static $conversions = array(
@@ -161,6 +164,7 @@ static public function colorize($string, $colored = null) {
161164
* Return the length of the string without color codes.
162165
*
163166
* @param string $string the string to measure
167+
* @return string
164168
*/
165169
static public function length($string) {
166170
return strlen(self::colorize($string, false));
@@ -171,6 +175,7 @@ static public function length($string) {
171175
*
172176
* @param string $string the string to pad
173177
* @param integer $length the display length
178+
* @return string
174179
*/
175180
static public function pad($string, $length) {
176181
$real_length = strlen($string);

lib/cli/Notify.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace cli;
1414

15+
use cli\Streams;
16+
1517
/**
1618
* The `Notify` class is the basis of all feedback classes, such as Indicators
1719
* and Progress meters. The default behaviour is to refresh output after 100ms
@@ -117,8 +119,6 @@ public function speed() {
117119
* @return string The formatted time span.
118120
*/
119121
public function formatTime($time) {
120-
$minutes = $time / 60;
121-
$seconds = $time % 60;
122122
return floor($time / 60) . ':' . str_pad($time % 60, 2, 0, STR_PAD_LEFT);
123123
}
124124

@@ -129,9 +129,9 @@ public function formatTime($time) {
129129
* @see cli\Notify::display()
130130
*/
131131
public function finish() {
132-
\cli\out("\r");
132+
Streams::out("\r");
133133
$this->display(true);
134-
\cli\line();
134+
Streams::line();
135135
}
136136

137137
/**
@@ -178,7 +178,7 @@ public function tick($increment = 1) {
178178
$this->increment($increment);
179179

180180
if ($this->shouldUpdate()) {
181-
\cli\out("\r");
181+
Streams::out("\r");
182182
$this->display();
183183
}
184184
}

lib/cli/Shell.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class Shell {
2424
* @todo Test on more systems.
2525
*/
2626
static public function columns() {
27-
if( strpos( $_SERVER['OS'], 'indows' ) !== false )
27+
if (stripos(PHP_OS, 'indows') === false) {
2828
return exec('/usr/bin/env tput cols');
29-
else
30-
return 80; // default width of cmd window on Windows OS, maybe force using MODE CON COLS=XXX?
29+
}
30+
return 80; // default width of cmd window on Windows OS, maybe force using MODE CON COLS=XXX?
3131
}
3232

3333
/**

lib/cli/Streams.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static function input( $format = null ) {
144144
* @return string The users input.
145145
* @see cli\input()
146146
*/
147-
public static function prompt( $question, $default = false, $marker = ': ' ) {
147+
public static function prompt( $question, $default = null, $marker = ': ' ) {
148148
if( $default && strpos( $question, '[' ) === false ) {
149149
$question .= ' [' . $default . ']';
150150
}
@@ -165,8 +165,7 @@ public static function prompt( $question, $default = false, $marker = ': ' ) {
165165
* questions (which this public static function defaults too).
166166
*
167167
* @param string $question The question to ask the user.
168-
* @param string $valid A string of characters allowed as a response. Case
169-
* is ignored.
168+
* @param string $choice A string of characters allowed as a response. Case is ignored.
170169
* @param string $default The default choice. NULL if a default is not allowed.
171170
* @return string The users choice.
172171
* @see cli\prompt()
@@ -206,7 +205,7 @@ public static function choose( $question, $choice = 'yn', $default = 'n' ) {
206205
* @see cli\input()
207206
* @see cli\err()
208207
*/
209-
public static function menu( $items, $default = false, $title = 'Choose an item' ) {
208+
public static function menu( $items, $default = null, $title = 'Choose an item' ) {
210209
$map = array_values( $items );
211210

212211
if( $default && strpos( $title, '[' ) === false && isset( $items[$default] ) ) {

0 commit comments

Comments
 (0)