Permalink
Browse files

add translation capabilities and exception factory for them

  • Loading branch information...
1 parent 75a1a4d commit d60df70181b70bb44b1b872ab0c1bcf196d0e159 @pmjones pmjones committed Nov 3, 2012
View
@@ -0,0 +1,16 @@
+<?php
+return array(
+ 'fallback' => null,
+ 'formatter' => 'basic',
+ 'messages' => [
+ 'en_US' => [
+ 'ERR_GETOPT_INITIALIZED' => "Getopt is already initialized.",
+ 'ERR_OPTION_ARRAY' => "The definition for option '{:option}' is not an array.",
+ 'ERR_OPTION_PARAM' => "The value for 'param' in option '{:option}' is not recognized.",
+ 'ERR_OPTION_NOT_DEFINED' => "The option '{:option}' is not recognized.",
+ 'ERR_OPTION_NOT_MULTI' => "The option '{:option}' does not take multiple values.",
+ 'ERR_OPTION_PARAM_REJECTED' => "The option '{:option}' is not allowed to have a value.",
+ 'ERR_OPTION_PARAM_REQUIRED' => "The option '{:option}' is required to have a value.",
+ ],
+ ],
+);
View
@@ -2,6 +2,9 @@
require_once __DIR__ . '/src/Aura/Cli/AbstractCommand.php';
require_once __DIR__ . '/src/Aura/Cli/Context.php';
require_once __DIR__ . '/src/Aura/Cli/Exception.php';
+require_once __DIR__ . '/src/Aura/Cli/ExceptionFactory.php';
+require_once __DIR__ . '/src/Aura/Cli/Exception/GetoptInitialized.php';
+require_once __DIR__ . '/src/Aura/Cli/Exception/OptionArray.php';
require_once __DIR__ . '/src/Aura/Cli/Exception/OptionName.php';
require_once __DIR__ . '/src/Aura/Cli/Exception/OptionNotDefined.php';
require_once __DIR__ . '/src/Aura/Cli/Exception/OptionNotMulti.php';
@@ -13,6 +16,8 @@
require_once __DIR__ . '/src/Aura/Cli/OptionFactory.php';
require_once __DIR__ . '/src/Aura/Cli/SignalInterface.php';
require_once __DIR__ . '/src/Aura/Cli/Signal.php';
+require_once __DIR__ . '/src/Aura/Cli/TranslatorInterface.php';
+require_once __DIR__ . '/src/Aura/Cli/Translator.php';
require_once __DIR__ . '/src/Aura/Cli/Stdio.php';
require_once __DIR__ . '/src/Aura/Cli/StdioResource.php';
require_once __DIR__ . '/src/Aura/Cli/Vt100.php';
@@ -0,0 +1,24 @@
+<?php
+/**
+ *
+ * This file is part of the Aura project for PHP.
+ *
+ * @package Aura.Cli
+ *
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ *
+ */
+namespace Aura\Cli\Exception;
+
+use Aura\Cli\Exception as Exception;
+
+/**
+ *
+ * Getopt has already been initialized.
+ *
+ * @package Aura.Cli
+ *
+ */
+class GetoptInitialized extends Exception
+{
+}
@@ -0,0 +1,24 @@
+<?php
+/**
+ *
+ * This file is part of the Aura project for PHP.
+ *
+ * @package Aura.Cli
+ *
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ *
+ */
+namespace Aura\Cli\Exception;
+
+use Aura\Cli\Exception as Exception;
+
+/**
+ *
+ * The option definition needs to be an array.
+ *
+ * @package Aura.Cli
+ *
+ */
+class OptionArray extends Exception
+{
+}
@@ -14,7 +14,7 @@
/**
*
- * The option 'param' value needs a boolean or a null.
+ * The option 'param' value needs a proper constant.
*
* @package Aura.Cli
*
@@ -0,0 +1,32 @@
+<?php
+namespace Aura\Cli;
+
+class ExceptionFactory
+{
+ protected $translator;
+
+ public function __construct(TranslatorInterface $translator)
+ {
+ $this->translator = $translator;
+ }
+
+ public function newInstance($key, $tokens_values = [])
+ {
+ // get a message translation
+ $message = $this->translator->translate($key, $tokens_values);
+
+ // strip 'ERR_' prefix.
+ // ERR_OPTION_NOT_DEFINED -> OPTINO_NOT_DEFINED
+ $class = substr($key, 4);
+
+ // underscores to spaces, lowercase all, uppercase words.
+ // OPTION_NOT_DEFINED -> OptionNotDefined
+ $class = ucwords(strtolower(str_replace('_', ' ', $class)));
+
+ // remove spaces and add prefix
+ $class = 'Aura\Cli\Exception\\' . str_replace(' ', '', $class);
+
+ // return the new exception
+ return new $class($message);
+ }
+}
@@ -80,9 +80,12 @@ class Getopt
* @param OptionFactory $option_factory A factory for Option objects.
*
*/
- public function __construct(OptionFactory $option_factory)
- {
+ public function __construct(
+ OptionFactory $option_factory,
+ ExceptionFactory $exception_factory
+ ) {
$this->option_factory = $option_factory;
+ $this->exception_factory = $exception_factory;
}
/**
@@ -118,15 +121,24 @@ public function __get($key)
public function init(array $opts, $strict = self::STRICT)
{
if ($this->options) {
- throw new Exception('Already initialized.');
+ throw $this->exception_factory->newInstance(
+ 'ERR_GETOPT_INITIALIZED'
+ );
}
foreach ($opts as $name => $spec) {
if (! is_array($spec)) {
throw new \UnexpectedValueException;
}
$spec['name'] = $name;
- $this->options[$name] = $this->option_factory->newInstance($spec);
+ try {
+ $this->options[$name] = $this->option_factory->newInstance($spec);
+ } catch (Exception\OptionParam $e) {
+ throw $this->exception_factory->newInstance(
+ 'ERR_OPTION_PARAM',
+ ['option' => $name]
+ );
+ }
}
$this->strict = $strict;
@@ -160,7 +172,10 @@ public function getOption($prop)
}
if ($this->strict) {
- throw new Exception\OptionNotDefined($prop);
+ throw $this->exception_factory->newInstance(
+ 'ERR_OPTION_NOT_DEFINED',
+ ['option' => $prop]
+ );
}
}
@@ -227,7 +242,10 @@ public function getLongOption($long)
}
if ($this->strict) {
- throw new Exception\OptionNotDefined("--$long");
+ throw $this->exception_factory->newInstance(
+ 'ERR_OPTION_NOT_DEFINED',
+ ['option' => "--$long"]
+ );
}
}
@@ -249,7 +267,10 @@ public function getShortOption($char)
}
if ($this->strict) {
- throw new Exception\OptionNotDefined("-$char");
+ throw $this->exception_factory->newInstance(
+ 'ERR_OPTION_NOT_DEFINED',
+ ['option' => "-$char"]
+ );
}
}
@@ -335,17 +356,23 @@ protected function loadLong($spec)
}
// if param is required but not present, blow up
- if ($option->isParamRequired() && $value === null) {
- throw new Exception\OptionParamRequired;
+ if ($option->isParamRequired() && trim($value) === '') {
+ throw $this->exception_factory->newInstance(
+ 'ERR_OPTION_PARAM_REQUIRED',
+ ['option' => "--$spec"]
+ );
}
// if params are rejected and one is present, blow up
- if ($option->isParamRejected() && $value !== null) {
- throw new Exception\OptionParamRejected;
+ if ($option->isParamRejected() && trim($value) !== '') {
+ throw $this->exception_factory->newInstance(
+ 'ERR_OPTION_PARAM_REJECTED',
+ ['option' => "--$spec"]
+ );
}
// if param is optional but not present, set to true
- if ($option->isParamOptional() && $value === null) {
+ if ($option->isParamOptional() && trim($value) === '') {
$option->setValue(true);
} else {
$option->setValue($value);
@@ -401,7 +428,10 @@ protected function loadShort($spec)
if (! $is_param && $option->isParamRequired()) {
// the next value is not a param, but a param is required,
// so blow up.
- throw new Exception\OptionParamRequired;
+ throw $this->exception_factory->newInstance(
+ 'ERR_OPTION_PARAM_REQUIRED',
+ ['option' => "-$char"]
+ );
}
// at this point, the value is a param, and it's optional or required.
@@ -441,7 +471,10 @@ protected function loadShortCluster($spec)
// can't process params in a cluster
if ($option->isParamRequired()) {
- throw new Exception\OptionParamRequired;
+ throw $this->exception_factory->newInstance(
+ 'ERR_OPTION_PARAM_REQUIRED',
+ ['option' => "-$char"]
+ );
}
// otherwise, set the value as a flag
@@ -141,30 +141,10 @@ public function __construct(
$this->name = (string) $name;
$this->long = (string) $long;
$this->short = (string) $short;
- $this->param = $param ?: static::PARAM_OPTIONAL;
+ $this->param = (string) $param;
$this->multi = (bool) $multi;
$this->default = $default;
- // always need a name
- if (! $this->name) {
- throw new Exception\OptionName;
- }
-
- // always need a long format or a short format.
- if (! $this->long && ! $this->short) {
- // auto-add a long format
- $this->long = str_replace('_', '-', $this->name);
- }
-
- // param has to be boolean or null
- $ok = $this->param === static::PARAM_REQUIRED
- || $this->param === static::PARAM_REJECTED
- || $this->param === static::PARAM_OPTIONAL;
-
- if (! $ok) {
- throw new Exception\OptionParam;
- }
-
// preset the value to an array if multiple values are allowed
if ($this->multi) {
$this->value = [];
@@ -184,20 +164,11 @@ public function __construct(
*/
public function setValue($value)
{
- if ($this->isParamRequired() && trim($value) === '') {
- throw new Exception\OptionParamRequired;
- }
-
if ($this->isMulti()) {
$this->value[] = $value;
- return;
- }
-
- if ($this->value !== null) {
- throw new Exception\OptionNotMulti;
+ } else {
+ $this->value = $value;
}
-
- $this->value = $value;
}
/**
@@ -10,6 +10,8 @@
*/
namespace Aura\Cli;
+use Aura\Cli\Option;
+
/**
*
* A factory to create Option objects.
@@ -45,16 +47,41 @@ class OptionFactory
* @return Option
*
*/
- public function newInstance(array $params)
+ public function newInstance(array $args)
{
- $params = array_merge($this->params, $params);
+ $args = array_merge($this->params, $args);
+
+ // always need a name
+ if (! $args['name']) {
+ throw new Exception\OptionName;
+ }
+
+ // always need a long format or a short format.
+ if (! $args['long'] && ! $args['short']) {
+ // auto-add a long format
+ $args['long'] = str_replace('_', '-', $args['name']);
+ }
+
+ // always need a param value
+ if (! $args['param']) {
+ $args['param'] = Option::PARAM_OPTIONAL;
+ }
+
+ $ok = $args['param'] === Option::PARAM_REQUIRED
+ || $args['param'] === Option::PARAM_REJECTED
+ || $args['param'] === Option::PARAM_OPTIONAL;
+
+ if (! $ok) {
+ throw new Exception\OptionParam;
+ }
+
return new Option(
- $params['name'],
- $params['long'],
- $params['short'],
- $params['param'],
- $params['multi'],
- $params['default']
+ $args['name'],
+ $args['long'],
+ $args['short'],
+ $args['param'],
+ $args['multi'],
+ $args['default']
);
}
}
@@ -134,4 +134,3 @@ public function isPosixTty()
return @posix_isatty($this->handle);
}
}
-
Oops, something went wrong.

0 comments on commit d60df70

Please sign in to comment.