Installation:
composer require circle/utilitybundle
Add to AppKernel.php:
new Circle\UtilityBundle\CircleUtilityBundle(),
First Use Case: Immutable Container (e.g., storing configuration):
use Circle\UtilityBundle\Container\ImmutableContainer;
$container = new ImmutableContainer(['key' => 'value']);
echo $container->get('key'); // 'value'
Queue (e.g., processing tasks):
use Circle\UtilityBundle\Queue\Queue;
$queue = new Queue();
$queue->push('task1');
$queue->push('task2');
echo $queue->pop(); // 'task1'
Tree (e.g., hierarchical data):
use Circle\UtilityBundle\Tree\Tree;
$tree = new Tree('root');
$child = $tree->addChild('child1');
$child->addChild('grandchild1');
$tree->render(); // Outputs tree structure
Immutable Container:
$config = new ImmutableContainer($this->getConfig());
$service = new Service($config);
Tree Structures:
$category = new Tree('Electronics');
$laptop = $category->addChild('Laptops');
$laptop->addChild('MacBook Pro');
getChildren() or render().Queue Management:
$queue = new Queue();
while (!$queue->isEmpty()) {
$task = $queue->pop();
$this->process($task);
}
- Use `ImmutableQueue` for thread-safe operations.
Multi-Child Tree:
$multiTree = new MultiChildTree('root');
$multiTree->addChild('tag1', 'parent1');
$multiTree->addChild('tag2', 'parent1');
$multiTree->getChildren('parent1'); // Returns ['tag1', 'tag2']
services.yml:
services:
app.tree:
class: Circle\UtilityBundle\Tree\Tree
arguments: ['root']
EventDispatcher to trigger actions on queue events (e.g., onPush, onPop).Deprecation Risk:
Immutable Container:
// ❌ Fails silently
$container->set('new_key', 'value'); // No setter method!
Tree/Queue Methods:
serialize()/unserialize() for persistence.Performance:
render() outputs HTML; avoid in performance-critical loops.getKeys() to inspect available keys:
print_r($container->getKeys()); // ['key1', 'key2']
count() or isEmpty() before popping to avoid UnderflowException.TreeNode to add metadata:
class CustomNode extends TreeNode {
public function __construct($name, $metadata = []) {
parent::__construct($name);
$this->metadata = $metadata;
}
}
Queue to support priority:
class PriorityQueue extends Queue {
public function push($item, $priority = 0) { ... }
protected function sortItems() { ... }
}
class DynamicImmutableContainer extends ImmutableContainer {
public function set($key, $value) {
$this->container[$key] = $value; // ⚠️ Breaks immutability!
}
}
How can I help you explore Laravel packages today?