Exemplo n.º 1
0
 function handle($doc, $argv = null)
 {
     try {
         if ($argv === null && isset($_SERVER['argv'])) {
             $argv = array_slice($_SERVER['argv'], 1);
         }
         $usageSections = parse_section('usage:', $doc);
         if (count($usageSections) == 0) {
             throw new LanguageError('"usage:" (case-insensitive) not found.');
         } elseif (count($usageSections) > 1) {
             throw new LanguageError('More than one "usage:" (case-insensitive).');
         }
         $usage = $usageSections[0];
         // temp fix until python port provides solution
         ExitException::$usage = $usage;
         $options = parse_defaults($doc);
         $formalUse = formal_usage($usage);
         $pattern = parse_pattern($formalUse, $options);
         $argv = parse_argv(new Tokens($argv), $options, $this->optionsFirst);
         $patternOptions = $pattern->flat('Option');
         foreach ($pattern->flat('OptionsShortcut') as $optionsShortcut) {
             $docOptions = parse_defaults($doc);
             $optionsShortcut->children = array_diff((array) $docOptions, $patternOptions);
         }
         extras($this->help, $this->version, $argv, $doc);
         list($matched, $left, $collected) = $pattern->fix()->match($argv);
         if ($matched && !$left) {
             $return = array();
             foreach (array_merge($pattern->flat(), $collected) as $a) {
                 $name = $a->name;
                 if ($name) {
                     $return[$name] = $a->value;
                 }
             }
             return new Response($return);
         }
         throw new ExitException();
     } catch (ExitException $ex) {
         $this->handleExit($ex);
         return new Response(null, $ex->status, $ex->getMessage());
     }
 }
Exemplo n.º 2
0
/**
 * Processes each sub-section of the sitemap,
 * recursively calling itself for each linked sub-sub-section, etc.
 *
 * Allows for infinitely nested levels of navigation.
 */
function parse_sub_section($section, $sub_section)
{
    if (is_numeric($section)) {
        # make link from page name
        $sub = slug_name($sub_section) . '.php';
    } else {
        if (is_string($sub_section)) {
            return $sub_section;
        } elseif (is_array($sub_section)) {
            $sub = array();
            # recursively process each section that has a sub-section
            foreach ($sub_section as $sub_key => $sub_value) {
                $sub = parse_section($sub, $sub_key, $sub_value);
            }
        } else {
            $sub = 'error parsing sitemap';
        }
    }
    return $sub;
}