|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Form\Console\Descriptor; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Descriptor\DescriptorInterface; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 17 | +use Symfony\Component\Form\ResolvedFormTypeInterface; |
| 18 | +use Symfony\Component\Form\Util\OptionsResolverWrapper; |
| 19 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Yonel Ceruto <yonelceruto@gmail.com> |
| 23 | + * |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +abstract class Descriptor implements DescriptorInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var SymfonyStyle |
| 30 | + */ |
| 31 | + protected $output; |
| 32 | + protected $type; |
| 33 | + protected $ownOptions = array(); |
| 34 | + protected $overriddenOptions = array(); |
| 35 | + protected $parentOptions = array(); |
| 36 | + protected $extensionOptions = array(); |
| 37 | + protected $requiredOptions = array(); |
| 38 | + protected $parents = array(); |
| 39 | + protected $extensions = array(); |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + public function describe(OutputInterface $output, $object, array $options = array()) |
| 45 | + { |
| 46 | + $this->output = $output; |
| 47 | + |
| 48 | + switch (true) { |
| 49 | + case $object instanceof ResolvedFormTypeInterface: |
| 50 | + $this->describeResolvedFormType($object, $options); |
| 51 | + break; |
| 52 | + default: |
| 53 | + throw new \InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object))); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + abstract protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedFormType, array $options = array()); |
| 58 | + |
| 59 | + protected function collectOptions(ResolvedFormTypeInterface $type) |
| 60 | + { |
| 61 | + $this->parents = array(); |
| 62 | + $this->extensions = array(); |
| 63 | + |
| 64 | + if (null !== $type->getParent()) { |
| 65 | + $optionsResolver = clone $this->getParentOptionsResolver($type->getParent()); |
| 66 | + } else { |
| 67 | + $optionsResolver = new OptionsResolver(); |
| 68 | + } |
| 69 | + |
| 70 | + $type->getInnerType()->configureOptions($ownOptionsResolver = new OptionsResolverWrapper()); |
| 71 | + $this->ownOptions = array_diff($ownOptionsResolver->getDefinedOptions(), $optionsResolver->getDefinedOptions()); |
| 72 | + $overriddenOptions = array_intersect(array_merge($ownOptionsResolver->getDefinedOptions(), $ownOptionsResolver->getUndefinedOptions()), $optionsResolver->getDefinedOptions()); |
| 73 | + |
| 74 | + $this->parentOptions = array(); |
| 75 | + foreach ($this->parents as $class => $parentOptions) { |
| 76 | + $this->overriddenOptions[$class] = array_intersect($overriddenOptions, $parentOptions); |
| 77 | + $this->parentOptions[$class] = array_diff($parentOptions, $overriddenOptions); |
| 78 | + } |
| 79 | + |
| 80 | + $type->getInnerType()->configureOptions($optionsResolver); |
| 81 | + $this->collectTypeExtensionsOptions($type, $optionsResolver); |
| 82 | + $this->extensionOptions = array(); |
| 83 | + foreach ($this->extensions as $class => $extensionOptions) { |
| 84 | + $this->overriddenOptions[$class] = array_intersect($overriddenOptions, $extensionOptions); |
| 85 | + $this->extensionOptions[$class] = array_diff($extensionOptions, $overriddenOptions); |
| 86 | + } |
| 87 | + |
| 88 | + $this->overriddenOptions = array_filter($this->overriddenOptions); |
| 89 | + $this->requiredOptions = $optionsResolver->getRequiredOptions(); |
| 90 | + |
| 91 | + $this->parents = array_keys($this->parents); |
| 92 | + $this->extensions = array_keys($this->extensions); |
| 93 | + } |
| 94 | + |
| 95 | + private function getParentOptionsResolver(ResolvedFormTypeInterface $type) |
| 96 | + { |
| 97 | + $this->parents[$class = get_class($type->getInnerType())] = array(); |
| 98 | + |
| 99 | + if (null !== $type->getParent()) { |
| 100 | + $optionsResolver = clone $this->getParentOptionsResolver($type->getParent()); |
| 101 | + } else { |
| 102 | + $optionsResolver = new OptionsResolver(); |
| 103 | + } |
| 104 | + |
| 105 | + $inheritedOptions = $optionsResolver->getDefinedOptions(); |
| 106 | + $type->getInnerType()->configureOptions($optionsResolver); |
| 107 | + $this->parents[$class] = array_diff($optionsResolver->getDefinedOptions(), $inheritedOptions); |
| 108 | + |
| 109 | + $this->collectTypeExtensionsOptions($type, $optionsResolver); |
| 110 | + |
| 111 | + return $optionsResolver; |
| 112 | + } |
| 113 | + |
| 114 | + private function collectTypeExtensionsOptions(ResolvedFormTypeInterface $type, OptionsResolver $optionsResolver) |
| 115 | + { |
| 116 | + foreach ($type->getTypeExtensions() as $extension) { |
| 117 | + $inheritedOptions = $optionsResolver->getDefinedOptions(); |
| 118 | + $extension->configureOptions($optionsResolver); |
| 119 | + $this->extensions[get_class($extension)] = array_diff($optionsResolver->getDefinedOptions(), $inheritedOptions); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments