Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
[NS4] Migrated Zend_Console to namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Apr 2, 2010
1 parent c7fcbfa commit e2d22c3
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 200 deletions.
36 changes: 36 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Console
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/

/**
* @namespace
*/
namespace Zend\Console;

/**
* @uses Zend\Exception
* @category Zend
* @package Zend_Console
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface Exception
{
}
72 changes: 38 additions & 34 deletions src/Getopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Console_Getopt
* @package Zend_Console
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/

/**
* Zend_Console_Getopt is a class to parse options for command-line
* @namespace
*/
namespace Zend\Console;

/**
* Getopt is a class to parse options for command-line
* applications.
*
* Terminology:
Expand Down Expand Up @@ -123,10 +128,10 @@
*
* @todo Feature request to implement callbacks.
* e.g. if -a is specified, run function 'handleOptionA'().
* @uses Zend_Console_Getopt_Exception
* @uses Zend_Json
* @uses \Zend\Console\GetoptException
* @uses \Zend\Json\Json
*/
class Zend_Console_Getopt
class Getopt
{

/**
Expand Down Expand Up @@ -245,11 +250,11 @@ public function __construct($rules, $argv = null, $getoptConfig = array())
{
if (!isset($_SERVER['argv'])) {
if (ini_get('register_argc_argv') == false) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"argv is not available, because ini option 'register_argc_argv' is set Off"
);
} else {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
'$_SERVER["argv"] is not set, but Zend_Console_Getopt cannot work without this information.'
);
}
Expand Down Expand Up @@ -345,13 +350,13 @@ public function __unset($key)
* These are appended to those defined when the constructor was called.
*
* @param array $argv
* @throws Zend_Console_Getopt_Exception When not given an array as parameter
* @return Zend_Console_Getopt Provides a fluent interface
* @throws \Zend\Console\Getopt\Exception When not given an array as parameter
* @return \Zend\Console\Getopt\Getopt Provides a fluent interface
*/
public function addArguments($argv)
{
if(!is_array($argv)) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Parameter #1 to addArguments should be an array");
}
$this->_argv = array_merge($this->_argv, $argv);
Expand All @@ -364,13 +369,13 @@ public function addArguments($argv)
* These replace any currently defined.
*
* @param array $argv
* @throws Zend_Console_Getopt_Exception When not given an array as parameter
* @return Zend_Console_Getopt Provides a fluent interface
* @throws \Zend\Console\Getopt\Exception When not given an array as parameter
* @return \Zend\Console\Getopt\Getopt Provides a fluent interface
*/
public function setArguments($argv)
{
if(!is_array($argv)) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Parameter #1 to setArguments should be an array");
}
$this->_argv = $argv;
Expand All @@ -384,7 +389,7 @@ public function setArguments($argv)
* the behavior of Zend_Console_Getopt.
*
* @param array $getoptConfig
* @return Zend_Console_Getopt Provides a fluent interface
* @return \Zend\Console\Getopt\Getopt Provides a fluent interface
*/
public function setOptions($getoptConfig)
{
Expand All @@ -403,7 +408,7 @@ public function setOptions($getoptConfig)
*
* @param string $configKey
* @param string $configValue
* @return Zend_Console_Getopt Provides a fluent interface
* @return \Zend\Console\Getopt\Getopt Provides a fluent interface
*/
public function setOption($configKey, $configValue)
{
Expand All @@ -418,7 +423,7 @@ public function setOption($configKey, $configValue)
* These are appended to the rules defined when the constructor was called.
*
* @param array $rules
* @return Zend_Console_Getopt Provides a fluent interface
* @return \Zend\Console\Getopt\Getopt Provides a fluent interface
*/
public function addRules($rules)
{
Expand Down Expand Up @@ -501,7 +506,7 @@ public function toJson()
);
}

$json = Zend_Json::encode($j);
$json = \Zend\Json\Json::encode($j);
return $json;
}

Expand All @@ -513,7 +518,7 @@ public function toJson()
public function toXml()
{
$this->parse();
$doc = new DomDocument('1.0', 'utf-8');
$doc = new \DomDocument('1.0', 'utf-8');
$optionsNode = $doc->createElement('options');
$doc->appendChild($optionsNode);
foreach ($this->_options as $flag => $value) {
Expand Down Expand Up @@ -631,8 +636,8 @@ public function getUsageMessage()
* mapping option name (short or long) to an alias.
*
* @param array $aliasMap
* @throws Zend_Console_Getopt_Exception
* @return Zend_Console_Getopt Provides a fluent interface
* @throws \Zend\Console\Getopt\Exception
* @return \Zend\Console\Getopt\Getopt Provides a fluent interface
*/
public function setAliases($aliasMap)
{
Expand All @@ -648,7 +653,7 @@ public function setAliases($aliasMap)
$flag = $this->_ruleMap[$flag];
if (isset($this->_rules[$alias]) || isset($this->_ruleMap[$alias])) {
$o = (strlen($alias) == 1 ? '-' : '--') . $alias;
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Option \"$o\" is being defined more than once.");
}
$this->_rules[$flag]['alias'][] = $alias;
Expand All @@ -664,7 +669,7 @@ public function setAliases($aliasMap)
* mapping option name (short or long) to the help string.
*
* @param array $helpMap
* @return Zend_Console_Getopt Provides a fluent interface
* @return \Zend\Console\Getopt\Getopt Provides a fluent interface
*/
public function setHelp($helpMap)
{
Expand All @@ -686,7 +691,7 @@ public function setHelp($helpMap)
* Also find option parameters, and remaining arguments after
* all options have been parsed.
*
* @return Zend_Console_Getopt|null Provides a fluent interface
* @return \Zend\Console\Getopt\Getopt|null Provides a fluent interface
*/
public function parse()
{
Expand Down Expand Up @@ -764,7 +769,7 @@ protected function _parseShortOptionCluster(&$argv)
*
* @param string $flag
* @param mixed $argv
* @throws Zend_Console_Getopt_Exception
* @throws \Zend\Console\Getopt\Exception
* @return void
*/
protected function _parseSingleOption($flag, &$argv)
Expand All @@ -773,7 +778,7 @@ protected function _parseSingleOption($flag, &$argv)
$flag = strtolower($flag);
}
if (!isset($this->_ruleMap[$flag])) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Option \"$flag\" is not recognized.",
$this->getUsageMessage());
}
Expand All @@ -784,7 +789,7 @@ protected function _parseSingleOption($flag, &$argv)
$param = array_shift($argv);
$this->_checkParameterType($realFlag, $param);
} else {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Option \"$flag\" requires a parameter.",
$this->getUsageMessage());
}
Expand All @@ -810,7 +815,7 @@ protected function _parseSingleOption($flag, &$argv)
*
* @param string $flag
* @param string $param
* @throws Zend_Console_Getopt_Exception
* @throws \Zend\Console\Getopt\Exception
* @return bool
*/
protected function _checkParameterType($flag, $param)
Expand All @@ -822,14 +827,14 @@ protected function _checkParameterType($flag, $param)
switch ($type) {
case 'word':
if (preg_match('/\W/', $param)) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Option \"$flag\" requires a single-word parameter, but was given \"$param\".",
$this->getUsageMessage());
}
break;
case 'integer':
if (preg_match('/\D/', $param)) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Option \"$flag\" requires an integer parameter, but was given \"$param\".",
$this->getUsageMessage());
}
Expand Down Expand Up @@ -879,7 +884,7 @@ protected function _addRulesModeGnu($rules)
* Define legal options using the Zend-style format.
*
* @param array $rules
* @throws Zend_Console_Getopt_Exception
* @throws \Zend\Console\Getopt\Exception
* @return void
*/
protected function _addRulesModeZend($rules)
Expand All @@ -905,19 +910,19 @@ protected function _addRulesModeZend($rules)
$mainFlag = $flags[0];
foreach ($flags as $flag) {
if (empty($flag)) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Blank flag not allowed in rule \"$ruleCode\".");
}
if (strlen($flag) == 1) {
if (isset($this->_ruleMap[$flag])) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Option \"-$flag\" is being defined more than once.");
}
$this->_ruleMap[$flag] = $mainFlag;
$rule['alias'][] = $flag;
} else {
if (isset($this->_rules[$flag]) || isset($this->_ruleMap[$flag])) {
throw new Zend_Console_Getopt_Exception(
throw new GetoptException(
"Option \"--$flag\" is being defined more than once.");
}
$this->_ruleMap[$flag] = $mainFlag;
Expand Down Expand Up @@ -951,5 +956,4 @@ protected function _addRulesModeZend($rules)
$this->_rules[$mainFlag] = $rule;
}
}

}
9 changes: 7 additions & 2 deletions src/Getopt/Exception.php → src/GetoptException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
*/

/**
* @uses Zend_Exception
* @namespace
*/
namespace Zend\Console;

/**
* @uses \Zend\Exception
* @category Zend
* @package Zend_Console_Getopt
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Console_Getopt_Exception extends Zend_Exception
class GetoptException extends \DomainException implements Exception
{
/**
* Usage
Expand Down
Loading

0 comments on commit e2d22c3

Please sign in to comment.