ExplainAdminCommand.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Command;
  11. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  17. */
  18. class ExplainAdminCommand extends ContainerAwareCommand
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function configure()
  24. {
  25. $this->setName('sonata:admin:explain');
  26. $this->setDescription('Explain an admin service');
  27. $this->addArgument('admin', InputArgument::REQUIRED, 'The admin service id');
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function execute(InputInterface $input, OutputInterface $output)
  33. {
  34. $admin = $this->getContainer()->get($input->getArgument('admin'));
  35. if (!$admin instanceof \Sonata\AdminBundle\Admin\AdminInterface) {
  36. throw new \RuntimeException(sprintf('Service "%s" is not an admin class', $input->getArgument('admin')));
  37. }
  38. $output->writeln('<comment>AdminBundle Information</comment>');
  39. $output->writeln(sprintf('<info>% -20s</info> : %s', 'id', $admin->getCode()));
  40. $output->writeln(sprintf('<info>% -20s</info> : %s', 'Admin', get_class($admin)));
  41. $output->writeln(sprintf('<info>% -20s</info> : %s', 'Model', $admin->getClass()));
  42. $output->writeln(sprintf('<info>% -20s</info> : %s', 'Controller', $admin->getBaseControllerName()));
  43. $output->writeln(sprintf('<info>% -20s</info> : %s', 'Model Manager', get_class($admin->getModelManager())));
  44. $output->writeln(sprintf('<info>% -20s</info> : %s', 'Form Builder', get_class($admin->getFormBuilder())));
  45. $output->writeln(sprintf('<info>% -20s</info> : %s', 'Datagrid Builder', get_class($admin->getDatagridBuilder())));
  46. $output->writeln(sprintf('<info>% -20s</info> : %s', 'List Builder', get_class($admin->getListBuilder())));
  47. if ($admin->isChild()) {
  48. $output->writeln(sprintf('<info>% -15s</info> : %s', 'Parent', $admin->getParent()->getCode()));
  49. }
  50. $output->writeln('');
  51. $output->writeln('<info>Routes</info>');
  52. foreach ($admin->getRoutes()->getElements() as $route) {
  53. $output->writeln(sprintf(' - % -25s %s', $route->getDefault('_sonata_name'), $route->getPath()));
  54. }
  55. $output->writeln('');
  56. $output->writeln('<info>Datagrid Columns</info>');
  57. foreach ($admin->getListFieldDescriptions() as $name => $fieldDescription) {
  58. $output->writeln(sprintf(
  59. ' - % -25s % -15s % -15s',
  60. $name,
  61. $fieldDescription->getType(),
  62. $fieldDescription->getTemplate()
  63. ));
  64. }
  65. $output->writeln('');
  66. $output->writeln('<info>Datagrid Filters</info>');
  67. foreach ($admin->getFilterFieldDescriptions() as $name => $fieldDescription) {
  68. $output->writeln(sprintf(
  69. ' - % -25s % -15s % -15s',
  70. $name,
  71. $fieldDescription->getType(),
  72. $fieldDescription->getTemplate()
  73. ));
  74. }
  75. $output->writeln('');
  76. $output->writeln('<info>Form theme(s)</info>');
  77. foreach ($admin->getFormTheme() as $template) {
  78. $output->writeln(sprintf(' - %s', $template));
  79. }
  80. $output->writeln('');
  81. $output->writeln('<info>Form Fields</info>');
  82. foreach ($admin->getFormFieldDescriptions() as $name => $fieldDescription) {
  83. $output->writeln(sprintf(
  84. ' - % -25s % -15s % -15s',
  85. $name,
  86. $fieldDescription->getType(),
  87. $fieldDescription->getTemplate()
  88. ));
  89. }
  90. $metadata = false;
  91. if ($this->getContainer()->has('validator.validator_factory')) {
  92. $factory = $this->getContainer()->get('validator.validator_factory');
  93. if (method_exists($factory, 'getMetadataFor')) {
  94. $metadata = $factory->getMetadataFor($admin->getClass());
  95. }
  96. }
  97. // NEXT_MAJOR: remove method check in next major release
  98. if (!$metadata) {
  99. $metadata = $this->getContainer()->get('validator')->getMetadataFor($admin->getClass());
  100. }
  101. $output->writeln('');
  102. $output->writeln('<comment>Validation Framework</comment> - http://symfony.com/doc/3.0/book/validation.html');
  103. $output->writeln('<info>Properties constraints</info>');
  104. if (count($metadata->properties) == 0) {
  105. $output->writeln(' <error>no property constraints defined !!</error>');
  106. } else {
  107. foreach ($metadata->properties as $name => $property) {
  108. $output->writeln(sprintf(' - %s', $name));
  109. foreach ($property->getConstraints() as $constraint) {
  110. $output->writeln(sprintf(
  111. ' % -70s %s',
  112. get_class($constraint),
  113. implode('|', $constraint->groups)
  114. ));
  115. }
  116. }
  117. }
  118. $output->writeln('');
  119. $output->writeln('<info>Getters constraints</info>');
  120. if (count($metadata->getters) == 0) {
  121. $output->writeln(' <error>no getter constraints defined !!</error>');
  122. } else {
  123. foreach ($metadata->getters as $name => $property) {
  124. $output->writeln(sprintf(' - %s', $name));
  125. foreach ($property->getConstraints() as $constraint) {
  126. $output->writeln(sprintf(
  127. ' % -70s %s',
  128. get_class($constraint),
  129. implode('|', $constraint->groups)
  130. ));
  131. }
  132. }
  133. }
  134. $output->writeln('');
  135. $output->writeln('<info>done!</info>');
  136. }
  137. }