lib/config-bundle/src/Service/Config.php line 61

  1. <?php
  2. namespace GroupIN\ConfigBundle\Service;
  3. use GroupIN\ConfigBundle\Store\PropertyStore;
  4. use GroupIN\ConfigBundle\Utils\ClassSourceManipulator;
  5. use Symfony\Bundle\MakerBundle\FileManager;
  6. use Symfony\Bundle\MakerBundle\Generator;
  7. use Symfony\Bundle\MakerBundle\Util\ClassDetails;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\Filesystem\Filesystem;
  10. use Symfony\Component\Form\AbstractType;
  11. use Symfony\Component\Form\FormBuilderInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Twig\Environment;
  17. use function Symfony\Component\String\u;
  18. /**
  19.  * Class Config for the general configuration
  20.  * @package GroupIN\ConfigBundle\Service
  21.  */
  22. class Config extends PropertyStore
  23. {
  24.     private Request $request;
  25.     public static function getPropertiesStore()
  26.     {
  27.         return dirname(__DIR__) . "/Store/PropertyStore.php";
  28.     }
  29.     public static function getFormTemplate()
  30.     {
  31.         return file_get_contents(dirname(__DIR__) . "/Resources/templates/config_property_type.php.twig");
  32.     }
  33.     public static function getControllerTemplate()
  34.     {
  35.         return file_get_contents(dirname(__DIR__) . "/Resources/templates/config_property_controller.php.twig");
  36.     }
  37.     public static function getTemplate($name 'config')
  38.     {
  39.         return file_get_contents(dirname(__DIR__) . "/Resources/templates/config_template_$name.html.twig");
  40.     }
  41.     public function __construct(
  42.         ?RequestStack $requestStack,
  43.         private KernelInterface $kernel,
  44.         private ParameterBagInterface $parameterBag,
  45.         private Environment $twig
  46.     )
  47.     {
  48.         $config self::read();
  49.         if (!empty($requestStack->getCurrentRequest())) {
  50.             $this->request $requestStack->getCurrentRequest();
  51.             $session $this->request->getSession();
  52.             $session->set('config'$config);
  53.         }
  54.         if (is_array($config) && !empty($config)) {
  55.             $this->hydrate($config);
  56.         }
  57.     }
  58.     public function __invoke()
  59.     {
  60.         return $this->toArray();
  61.     }
  62.     public function getFile()
  63.     {
  64.         return $this->getPath() . $this->getFilename();
  65.     }
  66.     private function getFilename()
  67.     {
  68.         return ($this->parameterBag->get('group_in_config.filename') ?? "config") . '.in';
  69.     }
  70.     private function getPath(): string
  71.     {
  72.         return u($this->parameterBag->get('group_in_config.path'))
  73.             ->trimEnd('/')
  74.             ->trimEnd('\\')
  75.             ->append('/')
  76.             ->prepend($this->kernel->getProjectDir(). '/');
  77.     }
  78.     public function get(string $property)
  79.     {
  80.         $property u($property)->snake();
  81.         return $this->$property;
  82.     }
  83.     public function hydrate(array $data = [])
  84.     {
  85.         foreach ($data as $key => $value) {
  86.             $method 'set' u($key)->camel()->title();
  87.             // If the matching setter exists
  88.             if (method_exists($this$method)) {
  89.                 $this->$method($value);
  90.             }
  91.         }
  92.     }
  93.     public function read()
  94.     {
  95.         if (!file_exists($this->getFile())) {
  96.             return $this->init();
  97.         }
  98.         return include $this->getFile();
  99.     }
  100.     public function write(array $config)
  101.     {
  102.         $this->init($config);
  103.     }
  104.     public function init(array $config = [])
  105.     {
  106.         $config var_export($configtrue);
  107.         file_put_contents($this->getFile(), "<?php return $config ;");
  108.         return $config;
  109.     }
  110.     public function save()
  111.     {
  112.         $session $this->request->getSession();
  113.         $configs $this->toArray();
  114.         unset($configs['request']);
  115.         $this->write($configs);
  116.         $config $this->read();
  117.         $session->set('config'$config);
  118.     }
  119.     public function toArray()
  120.     {
  121.         $records = [];
  122.         foreach ($this as $key => $value) {
  123.             if (!in_array($key, ['request''kernel''parameterBag''twig']))
  124.                 $records[$key] = $value;
  125.         }
  126.         return $records;
  127.     }
  128.     public function generate()
  129.     {
  130.         $contents file_get_contents(Config::getPropertiesStore());
  131.         $manipulator = new ClassSourceManipulator($contentstruefalse);
  132.         $properties $this->parameterBag->get('group_in_config.properties');
  133.         foreach ($properties as $property) {
  134.             $manipulator->addProperty(
  135.                 name$property['name'],
  136.                 defaultValue$property['default'],
  137.                 propertyType$property['nullable'] ? "?{$property['type']}$property['type'],
  138.                 isProtectedtrue
  139.             );
  140.             $manipulator->addGetter($property['name'], $property['type'], $property['nullable']);
  141.             $manipulator->addSetter($property['name'], $property['type'], $property['nullable']);
  142.         }
  143.         file_put_contents(Config::getPropertiesStore(), $manipulator->getSourceCode());
  144.     }
  145.     public function erase()
  146.     {
  147.         $contents = <<<EOT
  148. <?php
  149. namespace GroupIN\ConfigBundle\Store;
  150. class PropertyStore
  151. {
  152.     
  153. }
  154. EOT;
  155.         file_put_contents(Config::getPropertiesStore(), $contents);
  156.     }
  157.     public function generateForm()
  158.     {
  159.         $template $this->twig->createTemplate(self::getFormTemplate());
  160.         $contents $this->twig->render($template, [
  161.             'fields' => array_keys($this->toArray()),
  162.             'name' =>  $this->parameterBag->get('group_in_config.form_name'),
  163.             'namespace' =>  $this->parameterBag->get('group_in_config.form_namespace')
  164.         ]);
  165.         file_put_contents($this->formPath(), $contents);
  166.     }
  167.     public function generateController()
  168.     {
  169.         $controllerName $this->parameterBag->get('group_in_config.controller_name');
  170.         $controllerPath u($this->parameterBag->get('group_in_config.controller_path'))
  171.             ->trimEnd('/')
  172.             ->trimEnd('\\')
  173.             ->append('/')
  174.             ->append($controllerName)
  175.             ->append('.php')
  176.             ->prepend($this->kernel->getProjectDir(). '/');
  177.         $template $this->twig->createTemplate(self::getControllerTemplate());
  178.         $contents $this->twig->render($template, [
  179.             'controller_name' => $controllerName,
  180.             'fields' => array_keys($this->toArray()),
  181.             'namespace' =>  $this->parameterBag->get('group_in_config.controller_namespace'),
  182.             'route_name' =>  $this->parameterBag->get('group_in_config.route_name'),
  183.             'route_prefix' =>  'in_config',
  184.             'form_name' => $this->parameterBag->get('group_in_config.form_name'),
  185.             'form_namespace' => $this->parameterBag->get('group_in_config.form_namespace'),
  186.             'message_edited' => $this->parameterBag->get('group_in_config.message_edited'),
  187.             //'with_index' => false,
  188.         ]);
  189.         file_put_contents($controllerPath$contents);
  190.     }
  191.     public function generateTemplate($name 'config'bool $force false)
  192.     {
  193.         $templatePath $this->kernel->getProjectDir(). "/templates/in_config";
  194.         $fs = new Filesystem();
  195.         if (!$fs->exists($templatePath)){
  196.             $fs->mkdir($templatePath);
  197.         }
  198.         $templateFile "$templatePath/$name.html.twig";
  199.         $force $fs->exists($templateFile) ? $force true;
  200.         if ($force) {
  201.             $template $this->twig->createTemplate(self::getTemplate($name));
  202.             $contents $this->twig->render($template, [
  203.                 'fields' => array_keys($this->toArray()),
  204.             ]);
  205.             file_put_contents($templateFile$contents);
  206.         }
  207.         return $force;
  208.     }
  209.     private function formPath(): string
  210.     {
  211.         return u($this->parameterBag->get('group_in_config.form_path'))
  212.             ->trimEnd('/')
  213.             ->trimEnd('\\')
  214.             ->append($this->parameterBag->get('group_in_config.form_name'))
  215.             ->append('.php')
  216.             ->prepend($this->kernel->getProjectDir(). '/');
  217.     }
  218. }