|
| 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 | +} |
0 commit comments