1. Go to this page and download the library: Download jbtronics/settings-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/ */
// src/Settings/TestSettings.php
namespace App\Settings;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\ParameterTypes\StringType;
use Jbtronics\SettingsBundle\ParameterTypes\IntType;
use Jbtronics\SettingsBundle\Storage\JSONFileStorageAdapter;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
use Symfony\Component\Validator\Constraints as Assert;
// The settings attribute makes a simple class to settings
// You can configure the storage backend (here JSON files) to use either here or globally in the bundle configuration
#[Settings(storageAdapter: JSONFileStorageAdapter::class)]
class TestSettings {
use SettingsTrait; // Disable constructor and __clone methods
//The properties are public here for simplicity, but it can also be protected or private
//In many cases this attribute with zero config is enough, the type mapper is then derived from the declared type of the property
#[SettingsParameter()]
public string $myString = 'default value'; // The default value can be set right here in most cases
//Or you can explicitly set the type mapper and some options
#[SettingsParameter(type: IntType::class, label: 'My Integer', description: 'This value is shown as help in forms.')]
#[Assert\Range(min: 5, max: 10,)] // You can use symfony/validator to restrict possible values
public ?int $myInt = null;
}
use Jbtronics\SettingsBundle\Settings\SettingsManagerInterface;
class ExampleService {
public function __construct(private SettingsManagerInterface $settingsManager) {}
public function accessAndSaveSettings(): void
{
/** @var TestSettings $settings This is an instance of our previously defined setting class, containing the stored settings */
$settings = $this->settingsManager->get(TestSettings::class);
//To read the current settings value, just access the property
dump('My string is: ' . $settings->myString);
//To change the settings, just change the property (or call the setter)
$settings->myString = 'new value';
//And save the settings to the storage backend
$this->settingsManager->save($settings);
//You can also access the settings via a given name (which is the part before the "Settings" suffix of the class name in lowercase, by default)
$settings = $this->settingsManager->get('test');
//You can set an invalid value to the parameters
$settings->myInt = 42;
//But the bundle will throw an exception, when you try to save the settings
$this->settingsManager->save($settings); // Throws an excpetion
}
}
class ExampleService {
public function __construct(private TestSettings $settings) {
//This is equivalent to calling $settings = $settingsManager->get(TestSettings::class, lazy: true)
//The settings are lazy, meaning that they are only loaded from storage, when you access a property
if ($this->settings->myString === 'some value') {
//Do something
}
}
}
class SettingsFormController {
public function __construct(
private SettingsManagerInterface $settingsManager,
private SettingsFormFactoryInterface $settingsFormFactory,
) {}
#[Route('/settings', name: 'settings')]
public function settingsForm(Request $request): Response
{
//Create a temporary copy of the settings object, which we can modify in the form without breaking anything with invalid data
$settings = $this->settingsManager->createTemporaryCopy(TestSettings::class);
//Create a builder for the settings form
$builder = $this->settingsFormFactory->createSettingsFormBuilder($settings);
//Add a submit button, so we can save the form
$builder->add('submit', SubmitType::class);
//Create the form
$form = $builder->getForm();
//Handle the form submission
$form->handleRequest($request);
//If the form was submitted and the data is valid, then it
if ($form->isSubmitted() && $form->isValid()) {
//Merge the valid data back into the managed instance
$this->settingsManager->mergeTemporaryCopy($settings);
//Save the settings to storage
$this->settingsManager->save();
}
//Render the form
return $this->render('settings.html.twig', [
'form' => $form->createView(),
]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.