Console_CommandLine
[ class tree: Console_CommandLine ] [ index: Console_CommandLine ] [ all elements ]

Source for file XmlParser.php

Documentation is available at XmlParser.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Console_CommandLine package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Console
  15.  * @package   Console_CommandLine
  16.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  17.  * @copyright 2007 David JEAN LOUIS
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id: XmlParser.php,v 1.8 2008/10/09 10:44:54 izi Exp $
  20.  * @link      http://pear.php.net/package/Console_CommandLine
  21.  * @since     File available since release 0.1.0
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * Required file
  27.  */
  28. require_once 'Console/CommandLine.php';
  29.  
  30. /**
  31.  * Parser for command line xml definitions.
  32.  *
  33.  * @category  Console
  34.  * @package   Console_CommandLine
  35.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  36.  * @copyright 2007 David JEAN LOUIS
  37.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  38.  * @version   Release: @package_version@
  39.  * @link      http://pear.php.net/package/Console_CommandLine
  40.  * @since     Class available since release 0.1.0
  41.  */
  42. {
  43.     // parse() {{{
  44.  
  45.     /**
  46.      * Parses the given xml definition file and returns a
  47.      * Console_CommandLine instance constructed with the xml data.
  48.      *
  49.      * @param string $xmlfile The xml file to parse
  50.      *
  51.      * @return Console_CommandLine A parser instance
  52.      */
  53.     public static function parse($xmlfile
  54.     {
  55.         if (!is_readable($xmlfile)) {
  56.             Console_CommandLine::triggerError('invalid_xml_file',
  57.                 E_USER_ERRORarray('{$file}' => $xmlfile));
  58.         }
  59.         $doc = new DomDocument();
  60.         $doc->load($xmlfile);
  61.         self::validate($doc);
  62.         $root $doc->childNodes->item(0);
  63.         return self::_parseCommandNode($roottrue);
  64.     }
  65.  
  66.     // }}}
  67.     // parseString() {{{
  68.  
  69.     /**
  70.      * Parses the given xml definition string and returns a
  71.      * Console_CommandLine instance constructed with the xml data.
  72.      *
  73.      * @param string $xmlstr The xml string to parse
  74.      *
  75.      * @return Console_CommandLine A parser instance
  76.      */
  77.     public static function parseString($xmlstr
  78.     {
  79.         $doc = new DomDocument();
  80.         $doc->loadXml($xmlstr);
  81.         self::validate($doc);
  82.         $root $doc->childNodes->item(0);
  83.         return self::_parseCommandNode($roottrue);
  84.     }
  85.  
  86.     // }}}
  87.     // validate() {{{
  88.  
  89.     /**
  90.      * Validates the xml definition using Relax NG.
  91.      *
  92.      * @param DomDocument $doc The document to validate
  93.      *
  94.      * @return boolean Whether the xml data is valid or not.
  95.      * @throws Console_CommandLine_Exception
  96.      * @todo use exceptions
  97.      */
  98.     public static function validate($doc
  99.     {
  100.         if (is_dir('@data_dir@' . DIRECTORY_SEPARATOR . 'Console_CommandLine')) {
  101.             $rngfile '@data_dir@' . DIRECTORY_SEPARATOR
  102.                 . 'Console_CommandLine' . DIRECTORY_SEPARATOR . 'data' 
  103.                 . DIRECTORY_SEPARATOR . 'xmlschema.rng';
  104.         else {
  105.             $rngfile dirname(__FILE__. DIRECTORY_SEPARATOR . '..' 
  106.                 . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR 
  107.                 . 'xmlschema.rng';
  108.         }
  109.         if (!is_readable($rngfile)) {
  110.             Console_CommandLine::triggerError('invalid_xml_file',
  111.                 E_USER_ERRORarray('{$file}' => $rngfile));
  112.         }
  113.         return $doc->relaxNGValidate($rngfile);
  114.     }
  115.  
  116.     // }}}
  117.     // _parseCommandNode() {{{
  118.  
  119.     /**
  120.      * Parses the root command node or a command node and returns the
  121.      * constructed Console_CommandLine or Console_CommandLine_Command instance.
  122.      *
  123.      * @param DomDocumentNode $node       The node to parse
  124.      * @param bool            $isRootNode Whether it is a root node or not
  125.      *
  126.      * @return mixed Console_CommandLine or Console_CommandLine_Command
  127.      */
  128.     private static function _parseCommandNode($node$isRootNode = false
  129.     {
  130.         if ($isRootNode
  131.             $obj = new Console_CommandLine();
  132.         else {
  133.             include_once 'Console/CommandLine/Command.php';
  134.             $obj = new Console_CommandLine_Command();
  135.         }
  136.         foreach ($node->childNodes as $cNode{
  137.             $cNodeName $cNode->nodeName;
  138.             switch ($cNodeName{
  139.             case 'name':
  140.             case 'description':
  141.             case 'version':
  142.                 $obj->$cNodeName trim($cNode->nodeValue);
  143.                 break;
  144.             case 'add_help_option':
  145.             case 'add_version_option':
  146.             case 'force_posix':
  147.                 $obj->$cNodeName = self::_bool(trim($cNode->nodeValue));
  148.                 break;
  149.             case 'option':
  150.                 $obj->addOption(self::_parseOptionNode($cNode));
  151.                 break;
  152.             case 'argument':
  153.                 $obj->addArgument(self::_parseArgumentNode($cNode));
  154.                 break;
  155.             case 'command':
  156.                 $obj->addCommand(self::_parseCommandNode($cNode));
  157.                 break;
  158.             default:
  159.                 break;
  160.             }
  161.         }
  162.         return $obj;
  163.     }
  164.  
  165.     // }}}
  166.     // _parseOptionNode() {{{
  167.  
  168.     /**
  169.      * Parses an option node and returns the constructed
  170.      * Console_CommandLine_Option instance.
  171.      *
  172.      * @param DomDocumentNode $node The node to parse
  173.      *
  174.      * @return Console_CommandLine_Option The built option
  175.      */
  176.     private static function _parseOptionNode($node
  177.     {
  178.         include_once 'Console/CommandLine/Option.php';
  179.         $obj = new Console_CommandLine_Option($node->getAttribute('name'));
  180.         foreach ($node->childNodes as $cNode{
  181.             $cNodeName $cNode->nodeName;
  182.             if ($cNodeName == 'choices'{
  183.                 foreach ($cNode->childNodes as $subChildNode{
  184.                     if ($subChildNode->nodeName == 'choice'{
  185.                         $obj->choices[trim($subChildNode->nodeValue);
  186.                     }
  187.                 }
  188.             elseif (property_exists($obj$cNodeName)) {
  189.                 $obj->$cNodeName trim($cNode->nodeValue);
  190.             }
  191.         }
  192.         if ($obj->action == 'Password'{
  193.             $obj->argument_optional = true;
  194.         }
  195.         return $obj;
  196.     }
  197.  
  198.     // }}}
  199.     // _parseArgumentNode() {{{
  200.  
  201.     /**
  202.      * Parses an argument node and returns the constructed
  203.      * Console_CommandLine_Argument instance.
  204.      *
  205.      * @param DomDocumentNode $node The node to parse
  206.      *
  207.      * @return Console_CommandLine_Argument The built argument
  208.      */
  209.     private static function _parseArgumentNode($node
  210.     {
  211.         include_once 'Console/CommandLine/Argument.php';
  212.         $obj = new Console_CommandLine_Argument($node->getAttribute('name'));
  213.         foreach ($node->childNodes as $cNode{
  214.             $cNodeName $cNode->nodeName;
  215.             switch ($cNodeName{
  216.             case 'description':
  217.             case 'help_name':
  218.             case 'default':
  219.                 $obj->$cNodeName trim($cNode->nodeValue);
  220.                 break;
  221.             case 'multiple':
  222.                 $obj->multiple = self::_bool(trim($cNode->nodeValue));
  223.                 break;
  224.             default:
  225.                 break;
  226.             }
  227.         }
  228.         return $obj;
  229.     }
  230.  
  231.     // }}}
  232.     // _bool() {{{
  233.  
  234.     /**
  235.      * Returns a boolean according to true/false possible strings.
  236.      * 
  237.      * @param string $str The string to process
  238.      *
  239.      * @return boolean 
  240.      */
  241.     private static function _bool($str)
  242.     {
  243.         return in_array((string)$strarray('true''1''on''yes'));
  244.     }
  245.  
  246.     // }}}
  247. }

Documentation generated on Mon, 11 Mar 2019 15:26:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.