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