Admin.php 3.4 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\Annotation;
  11. use JMS\DiExtraBundle\Annotation\MetadataProcessorInterface;
  12. use JMS\DiExtraBundle\Metadata\ClassMetadata;
  13. use Sonata\AdminBundle\Admin\AbstractAdmin as AdminClass;
  14. /**
  15. * Use annotations to define admin classes.
  16. *
  17. * @Annotation
  18. * @Target("CLASS")
  19. */
  20. class Admin implements MetadataProcessorInterface
  21. {
  22. /**
  23. * Service id - autogenerated per default.
  24. *
  25. * @var string
  26. */
  27. public $id;
  28. /**
  29. * Admin class.
  30. *
  31. * @var string
  32. */
  33. public $class;
  34. /**
  35. * Data storage.
  36. *
  37. * @var string
  38. */
  39. public $managerType = 'orm';
  40. /**
  41. * @var string
  42. */
  43. public $pagerType;
  44. /**
  45. * @var string
  46. */
  47. public $persistFilters;
  48. /**
  49. * Admin group with fallback to class.
  50. *
  51. * @var string
  52. */
  53. public $group;
  54. /**
  55. * Icon for admin group default is '<i class="fa fa-folder"></i>'.
  56. *
  57. * @var string
  58. */
  59. public $icon;
  60. /**
  61. * Admin label with fallback to class.
  62. *
  63. * @var string
  64. */
  65. public $label;
  66. /**
  67. * @var string
  68. */
  69. public $baseControllerName = 'SonataAdminBundle:CRUD';
  70. /**
  71. * @var string
  72. */
  73. public $translationDomain;
  74. /**
  75. * @var bool
  76. */
  77. public $showInDashboard = true;
  78. /**
  79. * @param ClassMetadata $metadata
  80. */
  81. public function processMetadata(ClassMetadata $metadata)
  82. {
  83. $this->generateFallback($this->class);
  84. $this->validate();
  85. $tag = array(
  86. 'manager_type' => $this->managerType,
  87. 'group' => $this->group,
  88. 'label' => $this->label,
  89. 'show_in_dashboard' => $this->showInDashboard,
  90. 'icon' => $this->icon,
  91. 'pager_type' => $this->pagerType,
  92. 'persist_filters' => $this->persistFilters,
  93. );
  94. $tag = array_filter($tag, function ($v) {
  95. return !is_null($v);
  96. });
  97. $metadata->tags['sonata.admin'][] = $tag;
  98. $metadata->arguments = array($this->id, $this->class, $this->baseControllerName);
  99. if ($this->translationDomain) {
  100. $metadata->methodCalls[] = array('setTranslationDomain', array($this->translationDomain));
  101. }
  102. }
  103. /**
  104. * Check if all the required fields are given.
  105. */
  106. private function validate()
  107. {
  108. if (!$this->showInDashboard) {
  109. return;
  110. }
  111. if (empty($this->group) || empty($this->label)) {
  112. throw new \LogicException(
  113. sprintf(
  114. 'Unable to generate admin group and label for class %s.',
  115. $this->class
  116. )
  117. );
  118. }
  119. }
  120. /**
  121. * Set group and label from class name it not set.
  122. *
  123. * @param $name
  124. */
  125. private function generateFallback($name)
  126. {
  127. if (empty($name)) {
  128. return;
  129. }
  130. if (preg_match(AdminClass::CLASS_REGEX, $name, $matches)) {
  131. if (empty($this->group)) {
  132. $this->group = $matches[3];
  133. }
  134. if (empty($this->label)) {
  135. $this->label = $matches[5];
  136. }
  137. }
  138. }
  139. }