Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
37
namespace Psy\Readline\Hoa;
38
 
39
/**
40
 * Root of the `hoa://` protocol.
41
 */
42
class Protocol extends ProtocolNode
43
{
44
    /**
45
     * No resolution value.
46
     *
47
     * @const string
48
     */
49
    const NO_RESOLUTION = '/hoa/flatland';
50
 
51
    /**
52
     * Singleton.
53
     */
54
    private static $_instance = null;
55
 
56
    /**
57
     * Cache of resolver.
58
     */
59
    private static $_cache = [];
60
 
61
    /**
62
     * Initialize the protocol.
63
     */
64
    public function __construct()
65
    {
66
        $this->initialize();
67
 
68
        return;
69
    }
70
 
71
    /**
72
     * Singleton.
73
     * To use the `hoa://` protocol shared by everyone.
74
     */
75
    public static function getInstance(): self
76
    {
77
        if (null === static::$_instance) {
78
            static::$_instance = new self();
79
        }
80
 
81
        return static::$_instance;
82
    }
83
 
84
    /**
85
     * Initialize the protocol.
86
     */
87
    protected function initialize()
88
    {
89
        $root = \dirname(__DIR__, 3);
90
        $argv0 = \realpath($_SERVER['argv'][0]);
91
 
92
        $cwd =
93
            'cli' === \PHP_SAPI
94
                ? false !== $argv0 ? \dirname($argv0) : ''
95
                : \getcwd();
96
 
97
        $this[] = new ProtocolNode(
98
            'Application',
99
            $cwd.\DIRECTORY_SEPARATOR,
100
            [
101
                new ProtocolNode('Public', 'Public'.\DIRECTORY_SEPARATOR),
102
            ]
103
        );
104
 
105
        $this[] = new ProtocolNode(
106
            'Data',
107
            \dirname($cwd).\DIRECTORY_SEPARATOR,
108
            [
109
                new ProtocolNode(
110
                    'Etc',
111
                    'Etc'.\DIRECTORY_SEPARATOR,
112
                    [
113
                        new ProtocolNode('Configuration', 'Configuration'.\DIRECTORY_SEPARATOR),
114
                        new ProtocolNode('Locale', 'Locale'.\DIRECTORY_SEPARATOR),
115
                    ]
116
                ),
117
                new ProtocolNode('Lost+found', 'Lost+found'.\DIRECTORY_SEPARATOR),
118
                new ProtocolNode('Temporary', 'Temporary'.\DIRECTORY_SEPARATOR),
119
                new ProtocolNode(
120
                    'Variable',
121
                    'Variable'.\DIRECTORY_SEPARATOR,
122
                    [
123
                        new ProtocolNode('Cache', 'Cache'.\DIRECTORY_SEPARATOR),
124
                        new ProtocolNode('Database', 'Database'.\DIRECTORY_SEPARATOR),
125
                        new ProtocolNode('Log', 'Log'.\DIRECTORY_SEPARATOR),
126
                        new ProtocolNode('Private', 'Private'.\DIRECTORY_SEPARATOR),
127
                        new ProtocolNode('Run', 'Run'.\DIRECTORY_SEPARATOR),
128
                        new ProtocolNode('Test', 'Test'.\DIRECTORY_SEPARATOR),
129
                    ]
130
                ),
131
            ]
132
        );
133
 
134
        $this[] = new ProtocolNodeLibrary(
135
            'Library',
136
            $root.\DIRECTORY_SEPARATOR.'Hoathis'.\DIRECTORY_SEPARATOR.';'.
137
            $root.\DIRECTORY_SEPARATOR.'Hoa'.\DIRECTORY_SEPARATOR
138
        );
139
    }
140
 
141
    /**
142
     * Resolve (unfold) an `hoa://` path to its real resource.
143
     *
144
     * If `$exists` is set to `true`, try to find the first that exists,
145
     * otherwise returns the first solution.  If `$unfold` is set to `true`,
146
     * it returns all the paths.
147
     */
148
    public function resolve(string $path, bool $exists = true, bool $unfold = false)
149
    {
150
        if (\substr($path, 0, 6) !== 'hoa://') {
151
            if (true === \is_dir($path)) {
152
                $path = \rtrim($path, '/\\');
153
 
154
                if ('' === $path) {
155
                    $path = '/';
156
                }
157
            }
158
 
159
            return $path;
160
        }
161
 
162
        if (isset(self::$_cache[$path])) {
163
            $handle = self::$_cache[$path];
164
        } else {
165
            $out = $this->_resolve($path, $handle);
166
 
167
            // Not a path but a resource.
168
            if (!\is_array($handle)) {
169
                return $out;
170
            }
171
 
172
            $handle = \array_values(\array_unique($handle, \SORT_REGULAR));
173
 
174
            foreach ($handle as &$entry) {
175
                if (true === \is_dir($entry)) {
176
                    $entry = \rtrim($entry, '/\\');
177
 
178
                    if ('' === $entry) {
179
                        $entry = '/';
180
                    }
181
                }
182
            }
183
 
184
            self::$_cache[$path] = $handle;
185
        }
186
 
187
        if (true === $unfold) {
188
            if (true !== $exists) {
189
                return $handle;
190
            }
191
 
192
            $out = [];
193
 
194
            foreach ($handle as $solution) {
195
                if (\file_exists($solution)) {
196
                    $out[] = $solution;
197
                }
198
            }
199
 
200
            return $out;
201
        }
202
 
203
        if (true !== $exists) {
204
            return $handle[0];
205
        }
206
 
207
        foreach ($handle as $solution) {
208
            if (\file_exists($solution)) {
209
                return $solution;
210
            }
211
        }
212
 
213
        return static::NO_RESOLUTION;
214
    }
215
 
216
    /**
217
     * Clear the cache.
218
     */
219
    public static function clearCache()
220
    {
221
        self::$_cache = [];
222
    }
223
}