abstract class BufferBase in GraphQL 8.3
Same name and namespace in other branches
- 8.4 src/GraphQL/Buffers/BufferBase.php \Drupal\graphql\GraphQL\Buffers\BufferBase
Base class for field buffering services.
Hierarchy
- class \Drupal\graphql\GraphQL\Buffers\BufferBase
Expanded class hierarchy of BufferBase
1 file declares its use of BufferBase
- BufferedFieldTest.php in tests/
src/ Kernel/ Framework/ BufferedFieldTest.php
File
- src/
GraphQL/ Buffers/ BufferBase.php, line 8
Namespace
Drupal\graphql\GraphQL\BuffersView source
abstract class BufferBase {
/**
* The the array of buffers.
*
* @var \SplObjectStorage[]
*/
protected $buffers = [];
/**
* The array of result sets.
*
* @var \SplObjectStorage[]
*/
protected $results = [];
/**
* @param object $item
* The item to get the buffer id for.
* @return string
* The buffer id.
*/
protected function getBufferId($item) {
return "";
}
/**
* Helper function to create a resolver for a singular buffer.
*
* @param object $item
* The item to add to the buffer.
*
* @return \Closure
* The callback to invoke to load the result for this buffer item.
*/
public function createBufferResolver($item) {
$bufferId = $this
->getBufferId($item);
if (!isset($this->buffers[$bufferId])) {
$this->buffers[$bufferId] = new \SplObjectStorage();
}
if (!isset($this->results[$bufferId])) {
$this->results[$bufferId] = new \SplObjectStorage();
}
// Add the created item to the buffer.
$this->buffers[$bufferId]
->attach($item, $item);
// Return a callback that can be used to resolve the buffer item.
return $this
->createResolver($item, $this->buffers[$bufferId], $this->results[$bufferId]);
}
/**
* Creates a callback to invoke to load the result for this buffer item.
*
* @param object $item
* The item to add to create the resolver for.
* @param \SplObjectStorage $buffer
* The buffer.
* @param \SplObjectStorage $result
* The result set.
*
* @return \Closure
* The callback to invoke to load the result for this buffer item.
*/
protected function createResolver($item, \SplObjectStorage $buffer, \SplObjectStorage $result) {
// Return the closure that will resolve and return the result for the item.
return function () use ($item, $buffer, $result) {
return $this
->resolveItem($item, $buffer, $result);
};
}
/**
* Returns the result of the given item after processing the buffer if needed.
*
* @param object $item
* The buffer item to retrieve the result for.
* @param \SplObjectStorage $buffer
* The buffer.
* @param \SplObjectStorage $result
* The result set.
*
* @return mixed
* The result of resolving the given buffer item.
*/
protected function resolveItem($item, \SplObjectStorage $buffer, \SplObjectStorage $result) {
if ($buffer
->contains($item)) {
$results = $this
->resolveBuffer($buffer);
// Remove the resolved items from the buffer and add them to the results.
$buffer
->removeAll($results);
$result
->addAll($results);
}
if ($result
->contains($item)) {
return $result[$item];
}
throw new \LogicException('Failed to resolve item.');
}
/**
* Resolves the given buffer wholly.
*
* @param \SplObjectStorage $buffer
* The buffer to be resolved wholly.
*
* @return \SplObjectStorage
* The resolved results for the given buffer, keyed by the corresponding
* buffer items.
*/
protected function resolveBuffer(\SplObjectStorage $buffer) {
// Convert the buffer to an array that we can later use to map the results
// to the correct batch items.
$buffer = iterator_to_array($buffer, FALSE);
// Assign the loaded items to their corresponding batch items.
$output = new \SplObjectStorage();
foreach ($this
->resolveBufferArray($buffer) as $key => $item) {
$output
->attach($buffer[$key], $item);
}
return $output;
}
/**
* Resolve the buffer as an array.
*
* Simplifies sub-class implementations by concealing the object storage
* details of the buffer object.
*
* @param array $buffer
* The buffer as an array.
*
* @return array
* The resolved results, keyed by their corresponding buffer item array key.
*/
protected function resolveBufferArray(array $buffer) {
throw new \LogicException('Method not implemented.');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BufferBase:: |
protected | property | The the array of buffers. | |
BufferBase:: |
protected | property | The array of result sets. | |
BufferBase:: |
public | function | Helper function to create a resolver for a singular buffer. | |
BufferBase:: |
protected | function | Creates a callback to invoke to load the result for this buffer item. | |
BufferBase:: |
protected | function | 2 | |
BufferBase:: |
protected | function | Resolves the given buffer wholly. | |
BufferBase:: |
protected | function | Resolve the buffer as an array. | 2 |
BufferBase:: |
protected | function | Returns the result of the given item after processing the buffer if needed. |