PHP code example of sherlockode / configuration-bundle

1. Go to this page and download the library: Download sherlockode/configuration-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/ */

    

sherlockode / configuration-bundle example snippets



// config/bundles.php

return [
    // ...
    Sherlockode\ConfigurationBundle\SherlockodeConfigurationBundle::class => ['all' => true],
];



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sherlockode\ConfigurationBundle\Model\Parameter as BaseParameter;

#[ORM\Entity]
#[ORM\Table(name: 'parameter')]
class Parameter extends BaseParameter
{
    #[ORM\Id]
    #[ORM\Column(name: 'id', type: 'integer')]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    protected int $id;

    #[ORM\Column(name: 'path', type: 'string')]
    protected string $path;

    #[ORM\Column(name: 'value', type: 'text', nullable: true)]
    protected ?string $value = null;
}


use Sherlockode\ConfigurationBundle\Form\Type\ParametersType;
use Sherlockode\ConfigurationBundle\Manager\ParameterManagerInterface;

// $parameterManager has been injected
/** @var ParameterManagerInterface $parameterManager */
$data = $parameterManager->getAll();
// or using an associative array:
// $data = ['contact_email' => '[email protected]', 'max_user_login_attempts' => 5];

$form = $this->createForm(ParametersType::class, $data);
// handle form submission
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    $params = $form->getData();
    foreach ($params as $path => $value) {
        $parameterManager->set($path, $value);
    }
    $parameterManager->save();
}
//...

$email = $parameterManager->get('contact_email');
$maxAttempts = $parameterManager->get('max_user_login_attempts', 5);

public function getModelTransformer(ParameterDefinition $definition): ?TransformerInterface
{
    return new CallbackTransformer(
        function ($data) {
            if (!$data) {
                return null;
            }
            if (false !== ($unserialized = @unserialize($data))) {
                return $unserialized;
            }
            return $data;
        },
        function ($data) {
            if (is_array($data)) {
                return serialize($data);
            }
            return $data;
        }
    );
}