This Symfony bundle provides a unified and robust solution for managing persistent user interface state. It handles two key areas:
- Selections: Efficient handling of bulk operations (e.g., "Select All" across multiple pages of a paginated list).
- Preferences: Storing key-value settings for various contexts (e.g., user, company).
It features a flexible architecture based on context resolvers and support for various storage backends (Session, Redis, Doctrine), allowing you to elegantly solve UI state persistence without boilerplate code in your controllers.
- Selection Management: Track selected items (IDs) across pagination.
- Preference Management: Store and retrieve arbitrary user/context settings.
- Context Resolvers: Automatically resolve context (e.g., current user) for storage.
- Twig Integration: Easy-to-use Twig functions and filters.
- Stimulus Integration: Ready-to-use Stimulus controller for interactive selections.
- Multiple Storages: Support for Session (default), Doctrine, and easily extensible for others (e.g., Redis).
- CLI Support: Debug preferences directly from the terminal.
- PHP: ^8.1
- Symfony: ^6.4 | ^7.4 | ^8.0
Install the bundle via Composer:
composer require tito10047/persistent-state-bundleIf you are using Symfony Flex, the bundle will be registered automatically. Otherwise, add it to your config/bundles.php:
return [
// ...
Tito10047\PersistentStateBundle\PersistentStateBundle::class => ['all' => true],
];For Stimulus integration, ensure you have Symfony UX Stimulus installed and configured.
use Tito10047\PersistentStateBundle\Selection\Service\SelectionManagerInterface;
class CartController
{
public function __construct(
private readonly SelectionManagerInterface $selectionManager,
) {}
public function add(User $user, Product $product)
{
$cartSelection = $this->selectionManager->getSelection($user, 'cart');
$cartSelection->select($product, [
'quantity' => 1,
'added_at' => new \DateTime()
]);
}
public function clear(User $user)
{
$this->selectionManager->getSelection($user, 'cart')->destroy();
}
}use Tito10047\PersistentStateBundle\Preference\Service\PreferenceManagerInterface;
class SettingsController
{
public function __construct(
private readonly PreferenceManagerInterface $prefManager,
) {}
public function updateTheme(User $user, string $theme)
{
$preferences = $this->prefManager->getPreference($user);
$preferences->set('theme', $theme);
}
}In Twig:
<body class="theme-{{ preference(app.user, 'theme', 'light') }}">
Company Setting: {{ company|pref('some_setting') }}
</body>This bundle provides a Stimulus controller to handle "Select All" and individual row selections.
<div {{ persistent_selection_stimulus_controller("main_logs", null, {
selectAllClass: 'btn-primary',
unselectAllClass: 'btn-outline-secondary',
}) }}>
<div class="mb-2">
<button class="btn btn-sm" data-action="tito10047--persistent-state-bundle--selection#selectCurrentPage">
Select Visible
</button>
<button class="btn btn-sm" data-action="tito10047--persistent-state-bundle--selection#selectAll">
Select All
</button>
</div>
<ul class="list-group">
{% for log in logs %}
<li class="list-group-item">
{{ persistent_selection_row_selector("main_logs", log) }}
{{ log.name }}
</li>
{% endfor %}
</ul>
</div>In your controller to perform the action:
public function deleteSelected(User $user)
{
$selection = $this->selectionManager->getSelection($user, 'main_logs');
$ids = $selection->getSelectedIdentifiers();
// ... perform deletion
}Default configuration (optional to override in config/packages/persistent_state.yaml):
persistent_state:
preference:
managers:
default:
storage: '@persistent_state.preference.storage.session'
selection:
managers:
default:
storage: '@persistent_state.selection.storage.session'See docs/full_config.md for more advanced configuration, including Doctrine storage setup.
Inspect stored preferences for a specific context.
php bin/console debug:preference "user_15" --manager=defaultThe following scripts are available via Composer:
composer test: Runs the PHPUnit test suite.
assets/: Stimulus controllers and frontend assets.config/: Bundle configuration and service definitions.docs/: Additional documentation and images.src/: Core bundle logic.Command/: CLI commands.Controller/: Ajax endpoints for state updates.Preference/: Preference management logic.Selection/: Selection management logic.Storage/: Storage implementations (Session, Doctrine).Twig/: Twig extensions and runtimes.
templates/: Default templates and Twig components.tests/: Integration and unit tests.
Run tests using PHPUnit:
composer testThis bundle is released under the MIT License.
