lib/config-bundle/src/Service/Config.php line 61
<?phpnamespace GroupIN\ConfigBundle\Service;use GroupIN\ConfigBundle\Store\PropertyStore;use GroupIN\ConfigBundle\Utils\ClassSourceManipulator;use Symfony\Bundle\MakerBundle\FileManager;use Symfony\Bundle\MakerBundle\Generator;use Symfony\Bundle\MakerBundle\Util\ClassDetails;use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;use Symfony\Component\Filesystem\Filesystem;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\RequestStack;use Symfony\Component\HttpKernel\KernelInterface;use Symfony\Component\OptionsResolver\OptionsResolver;use Twig\Environment;use function Symfony\Component\String\u;/*** Class Config for the general configuration* @package GroupIN\ConfigBundle\Service*/class Config extends PropertyStore{private Request $request;public static function getPropertiesStore(){return dirname(__DIR__) . "/Store/PropertyStore.php";}public static function getFormTemplate(){return file_get_contents(dirname(__DIR__) . "/Resources/templates/config_property_type.php.twig");}public static function getControllerTemplate(){return file_get_contents(dirname(__DIR__) . "/Resources/templates/config_property_controller.php.twig");}public static function getTemplate($name = 'config'){return file_get_contents(dirname(__DIR__) . "/Resources/templates/config_template_$name.html.twig");}public function __construct(?RequestStack $requestStack,private KernelInterface $kernel,private ParameterBagInterface $parameterBag,private Environment $twig){$config = self::read();if (!empty($requestStack->getCurrentRequest())) {$this->request = $requestStack->getCurrentRequest();$session = $this->request->getSession();$session->set('config', $config);}if (is_array($config) && !empty($config)) {$this->hydrate($config);}}public function __invoke(){return $this->toArray();}public function getFile(){return $this->getPath() . $this->getFilename();}private function getFilename(){return ($this->parameterBag->get('group_in_config.filename') ?? "config") . '.in';}private function getPath(): string{return u($this->parameterBag->get('group_in_config.path'))->trimEnd('/')->trimEnd('\\')->append('/')->prepend($this->kernel->getProjectDir(). '/');}public function get(string $property){$property = u($property)->snake();return $this->$property;}public function hydrate(array $data = []){foreach ($data as $key => $value) {$method = 'set' . u($key)->camel()->title();// If the matching setter existsif (method_exists($this, $method)) {$this->$method($value);}}}public function read(){if (!file_exists($this->getFile())) {return $this->init();}return include $this->getFile();}public function write(array $config){$this->init($config);}public function init(array $config = []){$config = var_export($config, true);file_put_contents($this->getFile(), "<?php return $config ;");return $config;}public function save(){$session = $this->request->getSession();$configs = $this->toArray();unset($configs['request']);$this->write($configs);$config = $this->read();$session->set('config', $config);}public function toArray(){$records = [];foreach ($this as $key => $value) {if (!in_array($key, ['request', 'kernel', 'parameterBag', 'twig']))$records[$key] = $value;}return $records;}public function generate(){$contents = file_get_contents(Config::getPropertiesStore());$manipulator = new ClassSourceManipulator($contents, true, false);$properties = $this->parameterBag->get('group_in_config.properties');foreach ($properties as $property) {$manipulator->addProperty(name: $property['name'],defaultValue: $property['default'],propertyType: $property['nullable'] ? "?{$property['type']}" : $property['type'],isProtected: true);$manipulator->addGetter($property['name'], $property['type'], $property['nullable']);$manipulator->addSetter($property['name'], $property['type'], $property['nullable']);}file_put_contents(Config::getPropertiesStore(), $manipulator->getSourceCode());}public function erase(){$contents = <<<EOT<?phpnamespace GroupIN\ConfigBundle\Store;class PropertyStore{}EOT;file_put_contents(Config::getPropertiesStore(), $contents);}public function generateForm(){$template = $this->twig->createTemplate(self::getFormTemplate());$contents = $this->twig->render($template, ['fields' => array_keys($this->toArray()),'name' => $this->parameterBag->get('group_in_config.form_name'),'namespace' => $this->parameterBag->get('group_in_config.form_namespace')]);file_put_contents($this->formPath(), $contents);}public function generateController(){$controllerName = $this->parameterBag->get('group_in_config.controller_name');$controllerPath = u($this->parameterBag->get('group_in_config.controller_path'))->trimEnd('/')->trimEnd('\\')->append('/')->append($controllerName)->append('.php')->prepend($this->kernel->getProjectDir(). '/');$template = $this->twig->createTemplate(self::getControllerTemplate());$contents = $this->twig->render($template, ['controller_name' => $controllerName,'fields' => array_keys($this->toArray()),'namespace' => $this->parameterBag->get('group_in_config.controller_namespace'),'route_name' => $this->parameterBag->get('group_in_config.route_name'),'route_prefix' => 'in_config','form_name' => $this->parameterBag->get('group_in_config.form_name'),'form_namespace' => $this->parameterBag->get('group_in_config.form_namespace'),'message_edited' => $this->parameterBag->get('group_in_config.message_edited'),//'with_index' => false,]);file_put_contents($controllerPath, $contents);}public function generateTemplate($name = 'config', bool $force = false){$templatePath = $this->kernel->getProjectDir(). "/templates/in_config";$fs = new Filesystem();if (!$fs->exists($templatePath)){$fs->mkdir($templatePath);}$templateFile = "$templatePath/$name.html.twig";$force = $fs->exists($templateFile) ? $force : true;if ($force) {$template = $this->twig->createTemplate(self::getTemplate($name));$contents = $this->twig->render($template, ['fields' => array_keys($this->toArray()),]);file_put_contents($templateFile, $contents);}return $force;}private function formPath(): string{return u($this->parameterBag->get('group_in_config.form_path'))->trimEnd('/')->trimEnd('\\')->append($this->parameterBag->get('group_in_config.form_name'))->append('.php')->prepend($this->kernel->getProjectDir(). '/');}}