1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Protocol\Bin;
38
39use Hoa\Console;
40use Hoa\Protocol;
41
42/**
43 * Class \Hoa\Protocol\Bin\Resolve.
44 *
45 * This command resolves some `hoa://` paths.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50class Resolve extends Console\Dispatcher\Kit
51{
52    /**
53     * Options description.
54     *
55     * @var array
56     */
57    protected $options = [
58        ['exists',     Console\GetOption::NO_ARGUMENT, 'E'],
59        ['unfold',     Console\GetOption::NO_ARGUMENT, 'u'],
60        ['tree',       Console\GetOption::NO_ARGUMENT, 't'],
61        ['no-verbose', Console\GetOption::NO_ARGUMENT, 'V'],
62        ['help',       Console\GetOption::NO_ARGUMENT, 'h'],
63        ['help',       Console\GetOption::NO_ARGUMENT, '?']
64    ];
65
66
67
68    /**
69     * The entry method.
70     *
71     * @return  int
72     */
73    public function main()
74    {
75        $exists  = true;
76        $unfold  = false;
77        $tree    = false;
78        $verbose = Console::isDirect(STDOUT);
79
80        while (false !== $c = $this->getOption($v)) {
81            switch ($c) {
82                case 'E':
83                    $exists = false;
84
85                    break;
86
87                case 'u':
88                    $unfold = true;
89
90                    break;
91
92                case 't':
93                    $tree = true;
94
95                    break;
96
97                case 'V':
98                    $verbose = false;
99
100                    break;
101
102                case 'h':
103                case '?':
104                    return $this->usage();
105
106                case '__ambiguous':
107                    $this->resolveOptionAmbiguity($v);
108
109                    break;
110            }
111        }
112
113        $this->parser->listInputs($path);
114
115        if (null === $path) {
116            return $this->usage();
117        }
118
119        if (true === $tree) {
120            $protocol = Protocol::getInstance();
121            $foo      = substr($path, 0, 6);
122
123            if ('hoa://' !== $foo) {
124                return;
125            }
126
127            $path    = substr($path, 6);
128            $current = $protocol;
129
130            foreach (explode('/', $path) as $component) {
131                if (!isset($current[$component])) {
132                    break;
133                }
134
135                $current = $current[$component];
136            }
137
138            echo $current;
139
140            return;
141        }
142
143        if (true === $verbose) {
144            echo
145                Console\Cursor::colorize('foreground(yellow)'),
146                $path,
147                Console\Cursor::colorize('normal'),
148                ' is equivalent to:', "\n";
149        }
150
151        $resolved = resolve($path, $exists, $unfold);
152
153        foreach ((array) $resolved as $r) {
154            echo $r, "\n";
155        }
156
157        return;
158    }
159
160    /**
161     * The command usage.
162     *
163     * @return  int
164     */
165    public function usage()
166    {
167        echo
168            'Usage   : protocol:resolve <options> path', "\n",
169            'Options :', "\n",
170            $this->makeUsageOptionsList([
171                'E'    => 'Do not check if the resolution result exists.',
172                'u'    => 'Unfold all possible results.',
173                't'    => 'Print the tree from the path.',
174                'V'    => 'No-verbose, i.e. be as quiet as possible, just print ' .
175                          'essential information.',
176                'help' => 'This help.'
177            ]), "\n";
178
179        return;
180    }
181}
182
183__halt_compiler();
184Resolve `hoa://` paths.
185