| @@ 8-149 (lines=142) @@ | ||
| 5 | /** |
|
| 6 | * Base class for field buffering services. |
|
| 7 | */ |
|
| 8 | abstract class BufferBase { |
|
| 9 | ||
| 10 | ||
| 11 | /** |
|
| 12 | * The the array of buffers. |
|
| 13 | * |
|
| 14 | * @var \SplObjectStorage[] |
|
| 15 | */ |
|
| 16 | protected $buffers = []; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * The array of result sets. |
|
| 20 | * |
|
| 21 | * @var \SplObjectStorage[] |
|
| 22 | */ |
|
| 23 | protected $results = []; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @param object $item |
|
| 27 | * The item to get the buffer id for. |
|
| 28 | * @return string |
|
| 29 | * The buffer id. |
|
| 30 | */ |
|
| 31 | protected function getBufferId($item) { |
|
| 32 | return ""; |
|
| 33 | } |
|
| 34 | ||
| 35 | /** |
|
| 36 | * Helper function to create a resolver for a singular buffer. |
|
| 37 | * |
|
| 38 | * @param object $item |
|
| 39 | * The item to add to the buffer. |
|
| 40 | * |
|
| 41 | * @return \Closure |
|
| 42 | * The callback to invoke to load the result for this buffer item. |
|
| 43 | */ |
|
| 44 | protected function createBufferResolver($item) { |
|
| 45 | $bufferId = $this->getBufferId($item); |
|
| 46 | if (!isset($this->buffers[$bufferId])) { |
|
| 47 | $this->buffers[$bufferId] = new \SplObjectStorage(); |
|
| 48 | } |
|
| 49 | ||
| 50 | if (!isset($this->results[$bufferId])) { |
|
| 51 | $this->results[$bufferId] = new \SplObjectStorage(); |
|
| 52 | } |
|
| 53 | ||
| 54 | // Add the created item to the buffer. |
|
| 55 | $this->buffers[$bufferId]->attach($item, $item); |
|
| 56 | ||
| 57 | // Return a callback that can be used to resolve the buffer item. |
|
| 58 | return $this->createResolver($item, $this->buffers[$bufferId], $this->results[$bufferId]); |
|
| 59 | } |
|
| 60 | ||
| 61 | /** |
|
| 62 | * Creates a callback to invoke to load the result for this buffer item. |
|
| 63 | * |
|
| 64 | * @param object $item |
|
| 65 | * The item to add to create the resolver for. |
|
| 66 | * @param \SplObjectStorage $buffer |
|
| 67 | * The buffer. |
|
| 68 | * @param \SplObjectStorage $result |
|
| 69 | * The result set. |
|
| 70 | * |
|
| 71 | * @return \Closure |
|
| 72 | * The callback to invoke to load the result for this buffer item. |
|
| 73 | */ |
|
| 74 | protected function createResolver($item, \SplObjectStorage $buffer, \SplObjectStorage $result) { |
|
| 75 | // Return the closure that will resolve and return the result for the item. |
|
| 76 | return function () use ($item, $buffer, $result) { |
|
| 77 | return $this->resolveItem($item, $buffer, $result); |
|
| 78 | }; |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * Returns the result of the given item after processing the buffer if needed. |
|
| 83 | * |
|
| 84 | * @param object $item |
|
| 85 | * The buffer item to retrieve the result for. |
|
| 86 | * @param \SplObjectStorage $buffer |
|
| 87 | * The buffer. |
|
| 88 | * @param \SplObjectStorage $result |
|
| 89 | * The result set. |
|
| 90 | * |
|
| 91 | * @return mixed |
|
| 92 | * The result of resolving the given buffer item. |
|
| 93 | */ |
|
| 94 | protected function resolveItem($item, \SplObjectStorage $buffer, \SplObjectStorage $result) { |
|
| 95 | if ($buffer->contains($item)) { |
|
| 96 | $results = $this->resolveBuffer($buffer); |
|
| 97 | ||
| 98 | // Remove the resolved items from the buffer and add them to the results. |
|
| 99 | $buffer->removeAll($results); |
|
| 100 | $result->addAll($results); |
|
| 101 | } |
|
| 102 | ||
| 103 | if ($result->contains($item)) { |
|
| 104 | return $result[$item]; |
|
| 105 | } |
|
| 106 | ||
| 107 | throw new \LogicException('Failed to resolve item.'); |
|
| 108 | } |
|
| 109 | ||
| 110 | /** |
|
| 111 | * Resolves the given buffer wholly. |
|
| 112 | * |
|
| 113 | * @param \SplObjectStorage $buffer |
|
| 114 | * The buffer to be resolved wholly. |
|
| 115 | * |
|
| 116 | * @return \SplObjectStorage |
|
| 117 | * The resolved results for the given buffer, keyed by the corresponding |
|
| 118 | * buffer items. |
|
| 119 | */ |
|
| 120 | protected function resolveBuffer(\SplObjectStorage $buffer) { |
|
| 121 | // Convert the buffer to an array that we can later use to map the results |
|
| 122 | // to the correct batch items. |
|
| 123 | $buffer = iterator_to_array($buffer, FALSE); |
|
| 124 | ||
| 125 | // Assign the loaded items to their corresponding batch items. |
|
| 126 | $output = new \SplObjectStorage(); |
|
| 127 | foreach ($this->resolveBufferArray($buffer) as $key => $item) { |
|
| 128 | $output->attach($buffer[$key], $item); |
|
| 129 | } |
|
| 130 | ||
| 131 | return $output; |
|
| 132 | } |
|
| 133 | ||
| 134 | /** |
|
| 135 | * Resolve the buffer as an array. |
|
| 136 | * |
|
| 137 | * Simplifies sub-class implementations by concealing the object storage |
|
| 138 | * details of the buffer object. |
|
| 139 | * |
|
| 140 | * @param array $buffer |
|
| 141 | * The buffer as an array. |
|
| 142 | * |
|
| 143 | * @return array |
|
| 144 | * The resolved results, keyed by their corresponding buffer item array key. |
|
| 145 | */ |
|
| 146 | protected function resolveBufferArray(array $buffer) { |
|
| 147 | throw new \LogicException('Method not implemented.'); |
|
| 148 | } |
|
| 149 | } |
|
| @@ 8-148 (lines=141) @@ | ||
| 5 | /** |
|
| 6 | * Base class for field buffering services. |
|
| 7 | */ |
|
| 8 | abstract class BufferBase { |
|
| 9 | ||
| 10 | /** |
|
| 11 | * The the array of buffers. |
|
| 12 | * |
|
| 13 | * @var \SplObjectStorage[] |
|
| 14 | */ |
|
| 15 | protected $buffers = []; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * The array of result sets. |
|
| 19 | * |
|
| 20 | * @var \SplObjectStorage[] |
|
| 21 | */ |
|
| 22 | protected $results = []; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @param object $item |
|
| 26 | * The item to get the buffer id for. |
|
| 27 | * @return string |
|
| 28 | * The buffer id. |
|
| 29 | */ |
|
| 30 | protected function getBufferId($item) { |
|
| 31 | return ""; |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Helper function to create a resolver for a singular buffer. |
|
| 36 | * |
|
| 37 | * @param object $item |
|
| 38 | * The item to add to the buffer. |
|
| 39 | * |
|
| 40 | * @return \Closure |
|
| 41 | * The callback to invoke to load the result for this buffer item. |
|
| 42 | */ |
|
| 43 | protected function createBufferResolver($item) { |
|
| 44 | $bufferId = $this->getBufferId($item); |
|
| 45 | if (!isset($this->buffers[$bufferId])) { |
|
| 46 | $this->buffers[$bufferId] = new \SplObjectStorage(); |
|
| 47 | } |
|
| 48 | ||
| 49 | if (!isset($this->results[$bufferId])) { |
|
| 50 | $this->results[$bufferId] = new \SplObjectStorage(); |
|
| 51 | } |
|
| 52 | ||
| 53 | // Add the created item to the buffer. |
|
| 54 | $this->buffers[$bufferId]->attach($item, $item); |
|
| 55 | ||
| 56 | // Return a callback that can be used to resolve the buffer item. |
|
| 57 | return $this->createResolver($item, $this->buffers[$bufferId], $this->results[$bufferId]); |
|
| 58 | } |
|
| 59 | ||
| 60 | /** |
|
| 61 | * Creates a callback to invoke to load the result for this buffer item. |
|
| 62 | * |
|
| 63 | * @param object $item |
|
| 64 | * The item to add to create the resolver for. |
|
| 65 | * @param \SplObjectStorage $buffer |
|
| 66 | * The buffer. |
|
| 67 | * @param \SplObjectStorage $result |
|
| 68 | * The result set. |
|
| 69 | * |
|
| 70 | * @return \Closure |
|
| 71 | * The callback to invoke to load the result for this buffer item. |
|
| 72 | */ |
|
| 73 | protected function createResolver($item, \SplObjectStorage $buffer, \SplObjectStorage $result) { |
|
| 74 | // Return the closure that will resolve and return the result for the item. |
|
| 75 | return function () use ($item, $buffer, $result) { |
|
| 76 | return $this->resolveItem($item, $buffer, $result); |
|
| 77 | }; |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * Returns the result of the given item after processing the buffer if needed. |
|
| 82 | * |
|
| 83 | * @param object $item |
|
| 84 | * The buffer item to retrieve the result for. |
|
| 85 | * @param \SplObjectStorage $buffer |
|
| 86 | * The buffer. |
|
| 87 | * @param \SplObjectStorage $result |
|
| 88 | * The result set. |
|
| 89 | * |
|
| 90 | * @return mixed |
|
| 91 | * The result of resolving the given buffer item. |
|
| 92 | */ |
|
| 93 | protected function resolveItem($item, \SplObjectStorage $buffer, \SplObjectStorage $result) { |
|
| 94 | if ($buffer->contains($item)) { |
|
| 95 | $results = $this->resolveBuffer($buffer); |
|
| 96 | ||
| 97 | // Remove the resolved items from the buffer and add them to the results. |
|
| 98 | $buffer->removeAll($results); |
|
| 99 | $result->addAll($results); |
|
| 100 | } |
|
| 101 | ||
| 102 | if ($result->contains($item)) { |
|
| 103 | return $result[$item]; |
|
| 104 | } |
|
| 105 | ||
| 106 | throw new \LogicException('Failed to resolve item.'); |
|
| 107 | } |
|
| 108 | ||
| 109 | /** |
|
| 110 | * Resolves the given buffer wholly. |
|
| 111 | * |
|
| 112 | * @param \SplObjectStorage $buffer |
|
| 113 | * The buffer to be resolved wholly. |
|
| 114 | * |
|
| 115 | * @return \SplObjectStorage |
|
| 116 | * The resolved results for the given buffer, keyed by the corresponding |
|
| 117 | * buffer items. |
|
| 118 | */ |
|
| 119 | protected function resolveBuffer(\SplObjectStorage $buffer) { |
|
| 120 | // Convert the buffer to an array that we can later use to map the results |
|
| 121 | // to the correct batch items. |
|
| 122 | $buffer = iterator_to_array($buffer, FALSE); |
|
| 123 | ||
| 124 | // Assign the loaded items to their corresponding batch items. |
|
| 125 | $output = new \SplObjectStorage(); |
|
| 126 | foreach ($this->resolveBufferArray($buffer) as $key => $item) { |
|
| 127 | $output->attach($buffer[$key], $item); |
|
| 128 | } |
|
| 129 | ||
| 130 | return $output; |
|
| 131 | } |
|
| 132 | ||
| 133 | /** |
|
| 134 | * Resolve the buffer as an array. |
|
| 135 | * |
|
| 136 | * Simplifies sub-class implementations by concealing the object storage |
|
| 137 | * details of the buffer object. |
|
| 138 | * |
|
| 139 | * @param array $buffer |
|
| 140 | * The buffer as an array. |
|
| 141 | * |
|
| 142 | * @return array |
|
| 143 | * The resolved results, keyed by their corresponding buffer item array key. |
|
| 144 | */ |
|
| 145 | protected function resolveBufferArray(array $buffer) { |
|
| 146 | throw new \LogicException('Method not implemented.'); |
|
| 147 | } |
|
| 148 | } |
|