Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Support;
4
 
5
class NamespacedItemResolver
6
{
7
    /**
8
     * A cache of the parsed items.
9
     *
10
     * @var array
11
     */
12
    protected $parsed = [];
13
 
14
    /**
15
     * Parse a key into namespace, group, and item.
16
     *
17
     * @param  string  $key
18
     * @return array
19
     */
20
    public function parseKey($key)
21
    {
22
        // If we've already parsed the given key, we'll return the cached version we
23
        // already have, as this will save us some processing. We cache off every
24
        // key we parse so we can quickly return it on all subsequent requests.
25
        if (isset($this->parsed[$key])) {
26
            return $this->parsed[$key];
27
        }
28
 
29
        // If the key does not contain a double colon, it means the key is not in a
30
        // namespace, and is just a regular configuration item. Namespaces are a
31
        // tool for organizing configuration items for things such as modules.
32
        if (! str_contains($key, '::')) {
33
            $segments = explode('.', $key);
34
 
35
            $parsed = $this->parseBasicSegments($segments);
36
        } else {
37
            $parsed = $this->parseNamespacedSegments($key);
38
        }
39
 
40
        // Once we have the parsed array of this key's elements, such as its groups
41
        // and namespace, we will cache each array inside a simple list that has
42
        // the key and the parsed array for quick look-ups for later requests.
43
        return $this->parsed[$key] = $parsed;
44
    }
45
 
46
    /**
47
     * Parse an array of basic segments.
48
     *
49
     * @param  array  $segments
50
     * @return array
51
     */
52
    protected function parseBasicSegments(array $segments)
53
    {
54
        // The first segment in a basic array will always be the group, so we can go
55
        // ahead and grab that segment. If there is only one total segment we are
56
        // just pulling an entire group out of the array and not a single item.
57
        $group = $segments[0];
58
 
59
        // If there is more than one segment in this group, it means we are pulling
60
        // a specific item out of a group and will need to return this item name
61
        // as well as the group so we know which item to pull from the arrays.
62
        $item = count($segments) === 1
63
                    ? null
64
                    : implode('.', array_slice($segments, 1));
65
 
66
        return [null, $group, $item];
67
    }
68
 
69
    /**
70
     * Parse an array of namespaced segments.
71
     *
72
     * @param  string  $key
73
     * @return array
74
     */
75
    protected function parseNamespacedSegments($key)
76
    {
77
        [$namespace, $item] = explode('::', $key);
78
 
79
        // First we'll just explode the first segment to get the namespace and group
80
        // since the item should be in the remaining segments. Once we have these
81
        // two pieces of data we can proceed with parsing out the item's value.
82
        $itemSegments = explode('.', $item);
83
 
84
        $groupAndItem = array_slice(
85
            $this->parseBasicSegments($itemSegments), 1
86
        );
87
 
88
        return array_merge([$namespace], $groupAndItem);
89
    }
90
 
91
    /**
92
     * Set the parsed value of a key.
93
     *
94
     * @param  string  $key
95
     * @param  array  $parsed
96
     * @return void
97
     */
98
    public function setParsedKey($key, $parsed)
99
    {
100
        $this->parsed[$key] = $parsed;
101
    }
102
 
103
    /**
104
     * Flush the cache of parsed keys.
105
     *
106
     * @return void
107
     */
108
    public function flushParsedKeys()
109
    {
110
        $this->parsed = [];
111
    }
112
}