Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 6851d1e

Browse files
committed
Support color output
Adds ConsoleHelper class, which provides a method for querying if the current console supports colors, and constants for common POSIX color escape sequences and resets. ConfigDumperCommand now escapes errors and some help output for purposes of colorization; if colorization is not supported, empty escape sequences are used.
1 parent 1e51f3d commit 6851d1e

File tree

4 files changed

+278
-34
lines changed

4 files changed

+278
-34
lines changed

bin/dump-config.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,6 @@
2020
exit(1);
2121
}
2222

23-
$configPath = isset($argv[1]) ? $argv[1] : '';
24-
$className = isset($argv[2]) ? $argv[2] : '';
25-
26-
// Retrieve configuration
27-
if (! file_exists($configPath)) {
28-
fwrite(STDERR, sprintf('Cannot find configuration file at path "%s"%s', $configPath, PHP_EOL));
29-
exit(1);
30-
}
31-
32-
$appConfig = require $configPath;
33-
if (! is_array($appConfig)) {
34-
fwrite(STDERR, sprintf('Configuration file at path "%s" does not return an array%s', $configPath, PHP_EOL));
35-
exit(1);
36-
}
37-
38-
$dumper = new Tool\ConfigDumper();
39-
40-
try {
41-
$config = $dumper->createDependencyConfig($appConfig, $className);
42-
} catch (Exception\InvalidArgumentException $e) {
43-
fwrite(STDERR, sprintf(
44-
'Unable to create config for "%s": %s%s',
45-
$className,
46-
$e->getMessage(),
47-
PHP_EOL
48-
));
49-
exit(1);
50-
}
51-
echo $dumper->dumpConfigFile($config);
52-
exit(0);
23+
$command = new Tool\ConfigDumperCommand($argv[0]);
24+
$status = $command(array_slice($argv, 1));
25+
exit($status);

src/Tool/ConfigDumper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22
/**
3-
* Zend Framework (http://framework.zend.com/)
4-
*
5-
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
3+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
75
* @license http://framework.zend.com/license/new-bsd New BSD License
86
*/
97

src/Tool/ConfigDumperCommand.php

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace Zend\ServiceManager\Tool;
9+
10+
use Zend\ServiceManager\Exception;
11+
12+
class ConfigDumperCommand
13+
{
14+
const COMMAND_DUMP = 'dump';
15+
const COMMAND_ERROR = 'error';
16+
const COMMAND_HELP = 'help';
17+
18+
const HELP_TEMPLATE = <<< EOH
19+
%sUsage:%s
20+
21+
%s [-h|--help|help] <configFile> <className>
22+
23+
%sArguments:%s
24+
25+
%s-h|--help|help%s This usage message
26+
%s<configFile>%s Path to an existing config file for which to generate
27+
additional configuration. Must return an array.
28+
%s<className>%s Name of the class to reflect and for which to generate
29+
dependency configuration.
30+
31+
Generates to STDOUT a replacement configuration file containing dependency
32+
configuration for the named class with which to configure the
33+
ConfigAbstractFactory.
34+
35+
EOH;
36+
37+
private $scriptName;
38+
39+
/**
40+
* @param string $scriptName
41+
*/
42+
public function __construct($scriptName = self::DEFAULT_SCRIPT_NAME)
43+
{
44+
$this->scriptName = $scriptName;
45+
}
46+
47+
/**
48+
* @param array $args Argument list, minus script name
49+
* @return int Exit status
50+
*/
51+
public function __invoke(array $args)
52+
{
53+
$arguments = $this->parseArgs($args);
54+
55+
switch ($arguments->command) {
56+
case self::COMMAND_HELP:
57+
$this->help();
58+
return 0;
59+
case self::COMMAND_ERROR:
60+
$this->emitErrorMessage($arguments->message);
61+
$this->help(STDERR);
62+
return 1;
63+
case self::COMMAND_DUMP:
64+
// fall-through
65+
default:
66+
break;
67+
}
68+
69+
$dumper = new ConfigDumper();
70+
try {
71+
$config = $dumper->createDependencyConfig($arguments->config, $arguments->class);
72+
} catch (Exception\InvalidArgumentException $e) {
73+
$this->emitErrorMessage(sprintf(
74+
'Unable to create config for "%s": %s',
75+
$arguments->class,
76+
$e->getMessage()
77+
));
78+
$this->help(STDERR);
79+
return 1;
80+
}
81+
82+
fwrite(STDOUT, $dumper->dumpConfigFile($config));
83+
return 0;
84+
}
85+
86+
/**
87+
* @param array $args
88+
* @return \stdClass
89+
*/
90+
private function parseArgs(array $args)
91+
{
92+
if (! count($args)) {
93+
return $this->createArguments(self::COMMAND_HELP);
94+
}
95+
96+
$arg1 = array_shift($args);
97+
98+
if (in_array($arg1, ['-h', '--help', 'help'], true)) {
99+
return $this->createArguments(self::COMMAND_HELP);
100+
}
101+
102+
if (! count($args)) {
103+
return $this->createArguments(self::COMMAND_ERROR, null, null, 'Missing class name');
104+
}
105+
106+
if (! file_exists($arg1)) {
107+
return $this->createArguments(self::COMMAND_ERROR, null, null, sprintf(
108+
'Cannot find configuration file at path "%s"',
109+
$arg1
110+
));
111+
}
112+
113+
$config = require $arg1;
114+
115+
if (! is_array($config)) {
116+
return $this->createArguments(self::COMMAND_ERROR, null, null, sprintf(
117+
'Configuration at path "%s" does not return an array.',
118+
$arg1
119+
));
120+
}
121+
122+
$class = array_shift($args);
123+
124+
if (! class_exists($class)) {
125+
return $this->createArguments(self::COMMAND_ERROR, null, null, sprintf(
126+
'Class "%s" does not exist or could not be autoloaded.',
127+
$class
128+
));
129+
}
130+
131+
return $this->createArguments(self::COMMAND_DUMP, $config, $class);
132+
}
133+
134+
/**
135+
* @param resource $resource Defaults to STDOUT
136+
* @return void
137+
*/
138+
private function help($resource = STDOUT)
139+
{
140+
$startInfoColor = $this->startInfoColor();
141+
$resetColor = $this->resetColor();
142+
fwrite($resource, sprintf(
143+
self::HELP_TEMPLATE,
144+
$startInfoColor,
145+
$resetColor,
146+
$this->scriptName,
147+
$startInfoColor,
148+
$resetColor,
149+
$startInfoColor,
150+
$resetColor,
151+
$startInfoColor,
152+
$resetColor,
153+
$startInfoColor,
154+
$resetColor
155+
));
156+
}
157+
158+
/**
159+
* @param string $command
160+
* @param array|null $config Parsed configuration.
161+
* @param string|null $class Name of class to reflect.
162+
* @param string|null $message Error message, if any.
163+
* @return \stdClass
164+
*/
165+
private function createArguments($command, $config = null, $class = null, $error = null)
166+
{
167+
return (object) [
168+
'command' => $command,
169+
'config' => $config,
170+
'class' => $class,
171+
'message' => $error,
172+
];
173+
}
174+
175+
/**
176+
* @param string $message
177+
* @return void
178+
*/
179+
private function emitErrorMessage($message)
180+
{
181+
fwrite(STDERR, sprintf(
182+
"%s%s%s%s%s",
183+
$this->startErrorColor(),
184+
$message,
185+
$this->resetColor(),
186+
PHP_EOL,
187+
PHP_EOL
188+
));
189+
}
190+
191+
/**
192+
* Ensure newlines are appropriate for the current terminal.
193+
*
194+
* @param string
195+
* @return string
196+
*/
197+
private function formatNewlines($string)
198+
{
199+
return str_replace("\n", PHP_EOL, $string);
200+
}
201+
202+
/**
203+
* @return string
204+
*/
205+
private function startInfoColor()
206+
{
207+
if (ConsoleHelper::supportsColorOutput()) {
208+
return ConsoleHelper::COLOR_GREEN;
209+
}
210+
return '';
211+
}
212+
213+
/**
214+
* @return string
215+
*/
216+
private function startErrorColor()
217+
{
218+
if (ConsoleHelper::supportsColorOutput()) {
219+
return ConsoleHelper::COLOR_RED;
220+
}
221+
return '';
222+
}
223+
224+
/**
225+
* @return string
226+
*/
227+
private function resetColor()
228+
{
229+
if (ConsoleHelper::supportsColorOutput()) {
230+
return ConsoleHelper::COLOR_RESET;
231+
}
232+
return '';
233+
}
234+
}

src/Tool/ConsoleHelper.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace Zend\ServiceManager\Tool;
9+
10+
/**
11+
* Utilities for console tooling.
12+
*
13+
* Provides posix escape sequences for some oft-used colors, as well as for
14+
* resetting color.
15+
*
16+
* Provides a method for determining if a given console supports color output.
17+
*/
18+
class ConsoleHelper
19+
{
20+
const COLOR_GREEN = "\033[32m";
21+
const COLOR_RED = "\033[31m";
22+
const COLOR_RESET = "\033[0m";
23+
24+
/**
25+
* @param resource $resource
26+
* @return bool
27+
*/
28+
public static function supportsColorOutput($resource = STDOUT)
29+
{
30+
if ('\\' === DIRECTORY_SEPARATOR) {
31+
// Windows
32+
return false !== getenv('ANSICON')
33+
|| 'ON' === getenv('ConEmuANSI')
34+
|| 'xterm' === getenv('TERM');
35+
}
36+
37+
return function_exists('posix_isatty') && posix_isatty($resource);
38+
}
39+
}

0 commit comments

Comments
 (0)