NamespacedItemResolver.php 3.2KB

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