]> granicus.if.org Git - php/commitdiff
Added layer checks and reorganize a little the code
authorTomas V.V.Cox <cox@php.net>
Mon, 1 Apr 2002 19:16:01 +0000 (19:16 +0000)
committerTomas V.V.Cox <cox@php.net>
Mon, 1 Apr 2002 19:16:01 +0000 (19:16 +0000)
pear/PEAR/Command/Config.php

index 99624fb49dac98e4747d14a98807319c430321be..009c2d243ebf5210edb2d77aed20b0e4e011c03f 100644 (file)
@@ -14,6 +14,8 @@
 // | license@php.net so we can mail you a copy immediately.               |
 // +----------------------------------------------------------------------+
 // | Author: Stig Bakken <ssb@fast.no>                                    |
+// |         Tomas V.V.Cox <cox@idecnet.com>                              |
+// |                                                                      |
 // +----------------------------------------------------------------------+
 //
 // $Id$
@@ -82,36 +84,39 @@ class PEAR_Command_Config extends PEAR_Command_Common
 
     function run($command, $options, $params)
     {
-        $cf =$this->config;
+        $cf = &$this->config;
         $failmsg = '';
         switch ($command) {
             case 'config-show': {
+                // $params[0] -> the layer
+                if ($error = $this->_checkLayer(@$params[0])) {
+                    $failmsg .= $error;
+                    break;
+                }
                 $keys = $cf->getKeys();
                 sort($keys);
                 $this->ui->startTable(array('caption' => 'Configuration:'));
-                if (isset($params[0]) && $cf->isDefined($params[0])) {
-                    foreach ($keys as $key) {
-                        $type = $cf->getType($key);
-                        $value = $cf->get($key, $params[0]);
-                        if ($type == 'password' && $value) {
-                            $value = '********';
-                        }
-                        $this->ui->tableRow(array($key, $value));
+                foreach ($keys as $key) {
+                    $type = $cf->getType($key);
+                    $value = $cf->get($key, @$params[0]);
+                    if ($type == 'password' && $value) {
+                        $value = '********';
                     }
-                } else {
-                    foreach ($keys as $key) {
-                        $type = $cf->getType($key);
-                        $value = $cf->get($key, @$params[0]);
-                        if ($type == 'password' && $value) {
-                            $value = '********';
-                        }
-                        $this->ui->tableRow(array($key, $value));
+                    if (empty($value)) {
+                        $value = '<not set>';
                     }
+                    $this->ui->tableRow(array($key, $value));
                 }
                 $this->ui->endTable();
                 break;
             }
             case 'config-get': {
+                // $params[0] -> the parameter
+                // $params[1] -> the layer
+                if ($error = $this->_checkLayer(@$params[1])) {
+                    $failmsg .= $error;
+                    break;
+                }
                 if (sizeof($params) < 1 || sizeof($params) > 2) {
                     $failmsg .= "config-get expects 1 or 2 parameters. Try \"help config-get\" for help";
                 } elseif (sizeof($params) == 1) {
@@ -123,17 +128,22 @@ class PEAR_Command_Config extends PEAR_Command_Common
                 break;
             }
             case 'config-set': {
+                // $param[0] -> a parameter to set
+                // $param[1] -> the value for the parameter
+                // $param[2] -> the layer
                 if (sizeof($params) < 2 || sizeof($params) > 3) {
                     $failmsg .= "config-set expects 2 or 3 parameters. Try \"help config-set\" for help";
                     break;
+                }
+                if ($error = $this->_checkLayer(@$params[2])) {
+                    $failmsg .= $error;
+                    break;
+                }
+                if (!call_user_func_array(array(&$cf, 'set'), $params))
+                {
+                    $failmsg = "config-set (" . implode(", ", $params) . ") failed";
                 } else {
-                    if (!call_user_func_array(array(&$cf, 'set'), $params))
-                    {
-                        $failmsg = "config-set (" .
-                             implode(", ", $params) . ") failed";
-                    } else {
-                        $cf->store();
-                    }
+                    $cf->store();
                 }
                 break;
             }
@@ -148,6 +158,24 @@ class PEAR_Command_Config extends PEAR_Command_Common
     }
 
     // }}}
+
+    /**
+    * Checks if a layer is defined or not
+    *
+    * @param string $layer The layer to search for
+    * @return mixed False on no error or the error message
+    */
+    function _checkLayer($layer = null)
+    {
+        if (!empty($layer)) {
+            $layers = $this->config->getLayers();
+            if (!in_array($layer, $layers)) {
+                return " only the layers: \"" . implode('" or "', $layers) . "\" are supported";
+            }
+        }
+        return false;
+    }
+
 }
 
 ?>
\ No newline at end of file