PHP code example of tito10047 / persistent-preference-bundle

1. Go to this page and download the library: Download tito10047/persistent-preference-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tito10047 / persistent-preference-bundle example snippets


return [
    // ...
    Tito10047\PersistentStateBundle\PersistentStateBundle::class => ['all' => true],
];

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( 'cart',$user);
        $cartSelection->select($product, [
            'quantity' => 1,
            'added_at' => new \DateTime()
        ]);
    }

    public function clear(User $user)
    {
        $this->selectionManager->getSelection('cart',$user)->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);
    }
}

public function deleteSelected(User $user)
{
    $selection = $this->selectionManager->getSelection('main_logs', $user);
    $ids = $selection->getSelectedIdentifiers();
    
    // ... perform deletion
}
twig
<body class="theme-{{ preference(app.user, 'theme', 'light') }}">
    Company Setting: {{ company|pref('some_setting') }}
</body>
bash
php bin/console debug:preference "user_15" --manager=default