3 namespace Illuminate\Support
;
5 class NamespacedItemResolver
8 * A cache of the parsed items.
12 protected $parsed = [];
15 * Parse a key into namespace, group, and item.
20 public function parseKey($key)
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];
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 (strpos($key, '::') === false) {
33 $segments = explode('.', $key);
35 $parsed = $this->parseBasicSegments($segments);
37 $parsed = $this->parseNamespacedSegments($key);
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;
47 * Parse an array of basic segments.
49 * @param array $segments
52 protected function parseBasicSegments(array $segments)
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];
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
64 : implode('.', array_slice($segments, 1));
66 return [null, $group, $item];
70 * Parse an array of namespaced segments.
75 protected function parseNamespacedSegments($key)
77 list($namespace, $item) = explode('::', $key);
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);
84 $groupAndItem = array_slice(
85 $this->parseBasicSegments($itemSegments), 1
88 return array_merge([$namespace], $groupAndItem);
92 * Set the parsed value of a key.
95 * @param array $parsed
98 public function setParsedKey($key, $parsed)
100 $this->parsed
[$key] = $parsed;