JsonDescriptor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Console\Descriptor;
  11. use Symfony\Component\Form\ResolvedFormTypeInterface;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. /**
  14. * @author Yonel Ceruto <yonelceruto@gmail.com>
  15. *
  16. * @internal
  17. */
  18. class JsonDescriptor extends Descriptor
  19. {
  20. protected function describeDefaults(array $options)
  21. {
  22. $data['builtin_form_types'] = $options['core_types'];
  23. $data['service_form_types'] = $options['service_types'];
  24. if (!$options['show_deprecated']) {
  25. $data['type_extensions'] = $options['extensions'];
  26. $data['type_guessers'] = $options['guessers'];
  27. }
  28. $this->writeData($data, $options);
  29. }
  30. protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedFormType, array $options = [])
  31. {
  32. $this->collectOptions($resolvedFormType);
  33. if ($options['show_deprecated']) {
  34. $this->filterOptionsByDeprecated($resolvedFormType);
  35. }
  36. $formOptions = [
  37. 'own' => $this->ownOptions,
  38. 'overridden' => $this->overriddenOptions,
  39. 'parent' => $this->parentOptions,
  40. 'extension' => $this->extensionOptions,
  41. 'required' => $this->requiredOptions,
  42. ];
  43. $this->sortOptions($formOptions);
  44. $data = [
  45. 'class' => \get_class($resolvedFormType->getInnerType()),
  46. 'block_prefix' => $resolvedFormType->getInnerType()->getBlockPrefix(),
  47. 'options' => $formOptions,
  48. 'parent_types' => $this->parents,
  49. 'type_extensions' => $this->extensions,
  50. ];
  51. $this->writeData($data, $options);
  52. }
  53. protected function describeOption(OptionsResolver $optionsResolver, array $options)
  54. {
  55. $definition = $this->getOptionDefinition($optionsResolver, $options['option']);
  56. $map = [];
  57. if ($definition['deprecated']) {
  58. $map['deprecated'] = 'deprecated';
  59. if (\is_string($definition['deprecationMessage'])) {
  60. $map['deprecation_message'] = 'deprecationMessage';
  61. }
  62. }
  63. $map += [
  64. 'info' => 'info',
  65. 'required' => 'required',
  66. 'default' => 'default',
  67. 'allowed_types' => 'allowedTypes',
  68. 'allowed_values' => 'allowedValues',
  69. ];
  70. foreach ($map as $label => $name) {
  71. if (\array_key_exists($name, $definition)) {
  72. $data[$label] = $definition[$name];
  73. if ('default' === $name) {
  74. $data['is_lazy'] = isset($definition['lazy']);
  75. }
  76. }
  77. }
  78. $data['has_normalizer'] = isset($definition['normalizers']);
  79. $this->writeData($data, $options);
  80. }
  81. private function writeData(array $data, array $options)
  82. {
  83. $flags = $options['json_encoding'] ?? 0;
  84. $this->output->write(json_encode($data, $flags | \JSON_PRETTY_PRINT)."\n");
  85. }
  86. private function sortOptions(array &$options)
  87. {
  88. foreach ($options as &$opts) {
  89. $sorted = false;
  90. foreach ($opts as &$opt) {
  91. if (\is_array($opt)) {
  92. sort($opt);
  93. $sorted = true;
  94. }
  95. }
  96. if (!$sorted) {
  97. sort($opts);
  98. }
  99. }
  100. }
  101. }