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$
  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.         $nodes $doc->getElementsByTagName('command');
  63.         $root  $nodes->item(0);
  64.         return self::_parseCommandNode($roottrue);
  65.     }
  66.  
  67.     // }}}
  68.     // parseString() {{{
  69.  
  70.     /**
  71.      * Parses the given xml definition string and returns a
  72.      * Console_CommandLine instance constructed with the xml data.
  73.      *
  74.      * @param string $xmlstr The xml string to parse
  75.      *
  76.      * @return Console_CommandLine A parser instance
  77.      */
  78.     public static function parseString($xmlstr)
  79.     {
  80.         $doc = new DomDocument();
  81.         $doc->loadXml($xmlstr);
  82.         self::validate($doc);
  83.         $nodes $doc->getElementsByTagName('command');
  84.         $root  $nodes->item(0);
  85.         return self::_parseCommandNode($roottrue);
  86.     }
  87.  
  88.     // }}}
  89.     // validate() {{{
  90.  
  91.     /**
  92.      * Validates the xml definition using Relax NG.
  93.      *
  94.      * @param DomDocument $doc The document to validate
  95.      *
  96.      * @return boolean Whether the xml data is valid or not.
  97.      * @throws Console_CommandLine_Exception
  98.      * @todo use exceptions
  99.      */
  100.     public static function validate($doc)
  101.     {
  102.         if (is_dir('@data_dir@' . DIRECTORY_SEPARATOR . 'Console_CommandLine')) {
  103.             $rngfile '@data_dir@' . DIRECTORY_SEPARATOR
  104.                 . 'Console_CommandLine' . DIRECTORY_SEPARATOR . 'data'
  105.                 . DIRECTORY_SEPARATOR . 'xmlschema.rng';
  106.         else {
  107.             $rngfile dirname(__FILE__. DIRECTORY_SEPARATOR . '..'
  108.                 . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data'
  109.                 . DIRECTORY_SEPARATOR . 'xmlschema.rng';
  110.         }
  111.         if (!is_readable($rngfile)) {
  112.             Console_CommandLine::triggerError('invalid_xml_file',
  113.                 E_USER_ERRORarray('{$file}' => $rngfile));
  114.         }
  115.         return $doc->relaxNGValidate($rngfile);
  116.     }
  117.  
  118.     // }}}
  119.     // _parseCommandNode() {{{
  120.  
  121.     /**
  122.      * Parses the root command node or a command node and returns the
  123.      * constructed Console_CommandLine or Console_CommandLine_Command instance.
  124.      *
  125.      * @param DomDocumentNode $node       The node to parse
  126.      * @param bool            $isRootNode Whether it is a root node or not
  127.      *
  128.      * @return mixed Console_CommandLine or Console_CommandLine_Command
  129.      */
  130.     private static function _parseCommandNode($node$isRootNode = false)
  131.     {
  132.         if ($isRootNode{
  133.             $obj = new Console_CommandLine();
  134.         else {
  135.             include_once 'Console/CommandLine/Command.php';
  136.             $obj = new Console_CommandLine_Command();
  137.         }
  138.         foreach ($node->childNodes as $cNode{
  139.             $cNodeName $cNode->nodeName;
  140.             switch ($cNodeName{
  141.             case 'name':
  142.             case 'description':
  143.             case 'version':
  144.                 $obj->$cNodeName trim($cNode->nodeValue);
  145.                 break;
  146.             case 'add_help_option':
  147.             case 'add_version_option':
  148.             case 'force_posix':
  149.                 $obj->$cNodeName = self::_bool(trim($cNode->nodeValue));
  150.                 break;
  151.             case 'option':
  152.                 $obj->addOption(self::_parseOptionNode($cNode));
  153.                 break;
  154.             case 'argument':
  155.                 $obj->addArgument(self::_parseArgumentNode($cNode));
  156.                 break;
  157.             case 'command':
  158.                 $obj->addCommand(self::_parseCommandNode($cNode));
  159.                 break;
  160.             case 'aliases':
  161.                 if (!$isRootNode{
  162.                     foreach ($cNode->childNodes as $subChildNode{
  163.                         if ($subChildNode->nodeName == 'alias'{
  164.                             $obj->aliases[trim($subChildNode->nodeValue);
  165.                         }
  166.                     }
  167.                 }
  168.                 break;
  169.             case 'messages':
  170.                 $obj->messages = self::_messages($cNode);
  171.                 break;
  172.             default:
  173.                 break;
  174.             }
  175.         }
  176.         return $obj;
  177.     }
  178.  
  179.     // }}}
  180.     // _parseOptionNode() {{{
  181.  
  182.     /**
  183.      * Parses an option node and returns the constructed
  184.      * Console_CommandLine_Option instance.
  185.      *
  186.      * @param DomDocumentNode $node The node to parse
  187.      *
  188.      * @return Console_CommandLine_Option The built option
  189.      */
  190.     private static function _parseOptionNode($node)
  191.     {
  192.         include_once 'Console/CommandLine/Option.php';
  193.         $obj = new Console_CommandLine_Option($node->getAttribute('name'));
  194.         foreach ($node->childNodes as $cNode{
  195.             $cNodeName $cNode->nodeName;
  196.             switch ($cNodeName{
  197.             case 'choices':
  198.                 foreach ($cNode->childNodes as $subChildNode{
  199.                     if ($subChildNode->nodeName == 'choice'{
  200.                         $obj->choices[trim($subChildNode->nodeValue);
  201.                     }
  202.                 }
  203.                 break;
  204.             case 'messages':
  205.                 $obj->messages = self::_messages($cNode);
  206.                 break;
  207.             default:
  208.                 if (property_exists($obj$cNodeName)) {
  209.                     $obj->$cNodeName trim($cNode->nodeValue);
  210.                 }
  211.                 break;
  212.             }
  213.         }
  214.         if ($obj->action == 'Password'{
  215.             $obj->argument_optional = true;
  216.         }
  217.         return $obj;
  218.     }
  219.  
  220.     // }}}
  221.     // _parseArgumentNode() {{{
  222.  
  223.     /**
  224.      * Parses an argument node and returns the constructed
  225.      * Console_CommandLine_Argument instance.
  226.      *
  227.      * @param DomDocumentNode $node The node to parse
  228.      *
  229.      * @return Console_CommandLine_Argument The built argument
  230.      */
  231.     private static function _parseArgumentNode($node)
  232.     {
  233.         include_once 'Console/CommandLine/Argument.php';
  234.         $obj = new Console_CommandLine_Argument($node->getAttribute('name'));
  235.         foreach ($node->childNodes as $cNode{
  236.             $cNodeName $cNode->nodeName;
  237.             switch ($cNodeName{
  238.             case 'description':
  239.             case 'help_name':
  240.             case 'default':
  241.                 $obj->$cNodeName trim($cNode->nodeValue);
  242.                 break;
  243.             case 'multiple':
  244.                 $obj->multiple = self::_bool(trim($cNode->nodeValue));
  245.                 break;
  246.             case 'optional':
  247.                 $obj->optional = self::_bool(trim($cNode->nodeValue));
  248.                 break;
  249.             case 'choices':
  250.                 foreach ($cNode->childNodes as $subChildNode{
  251.                     if ($subChildNode->nodeName == 'choice'{
  252.                         $obj->choices[trim($subChildNode->nodeValue);
  253.                     }
  254.                 }
  255.                 break;
  256.             case 'messages':
  257.                 $obj->messages = self::_messages($cNode);
  258.                 break;
  259.             default:
  260.                 break;
  261.             }
  262.         }
  263.         return $obj;
  264.     }
  265.  
  266.     // }}}
  267.     // _bool() {{{
  268.  
  269.     /**
  270.      * Returns a boolean according to true/false possible strings.
  271.      *
  272.      * @param string $str The string to process
  273.      *
  274.      * @return boolean 
  275.      */
  276.     private static function _bool($str)
  277.     {
  278.         return in_array(strtolower((string)$str)array('true''1''on''yes'));
  279.     }
  280.  
  281.     // }}}
  282.     // _messages() {{{
  283.  
  284.     /**
  285.      * Returns an array of custom messages for the element
  286.      *
  287.      * @param DOMNode $node The messages node to process
  288.      *
  289.      * @return array an array of messages
  290.      *
  291.      * @see Console_CommandLine::$messages
  292.      * @see Console_CommandLine_Element::$messages
  293.      */
  294.     private static function _messages(DOMNode $node)
  295.     {
  296.         $messages = array();
  297.  
  298.         foreach ($node->childNodes as $cNode{
  299.             if ($cNode->nodeType == XML_ELEMENT_NODE{
  300.                 $name  $cNode->getAttribute('name');
  301.                 $value trim($cNode->nodeValue);
  302.  
  303.                 $messages[$name$value;
  304.             }
  305.         }
  306.  
  307.         return $messages;
  308.     }
  309.  
  310.     // }}}
  311. }

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