Admin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * @var bool
  80. */
  81. public $keepOpen = false;
  82. /**
  83. * @var bool
  84. */
  85. public $onTop = false;
  86. /**
  87. * @param ClassMetadata $metadata
  88. */
  89. public function processMetadata(ClassMetadata $metadata)
  90. {
  91. $this->generateFallback($this->class);
  92. $this->validate();
  93. $tag = array(
  94. 'manager_type' => $this->managerType,
  95. 'group' => $this->group,
  96. 'label' => $this->label,
  97. 'show_in_dashboard' => $this->showInDashboard,
  98. 'icon' => $this->icon,
  99. 'pager_type' => $this->pagerType,
  100. 'persist_filters' => $this->persistFilters,
  101. 'keep_open' => $this->keepOpen,
  102. 'on_top' => $this->onTop,
  103. );
  104. $tag = array_filter($tag, function ($v) {
  105. return !is_null($v);
  106. });
  107. if (!empty($this->id)) {
  108. $metadata->id = $this->id;
  109. }
  110. $metadata->tags['sonata.admin'][] = $tag;
  111. $metadata->arguments = array($this->id, $this->class, $this->baseControllerName);
  112. if ($this->translationDomain) {
  113. $metadata->methodCalls[] = array('setTranslationDomain', array($this->translationDomain));
  114. }
  115. }
  116. /**
  117. * Check if all the required fields are given.
  118. */
  119. private function validate()
  120. {
  121. if (!$this->showInDashboard) {
  122. return;
  123. }
  124. if (empty($this->group) || empty($this->label)) {
  125. throw new \LogicException(
  126. sprintf(
  127. 'Unable to generate admin group and label for class %s.',
  128. $this->class
  129. )
  130. );
  131. }
  132. }
  133. /**
  134. * Set group and label from class name it not set.
  135. *
  136. * @param $name
  137. */
  138. private function generateFallback($name)
  139. {
  140. if (empty($name)) {
  141. return;
  142. }
  143. if (preg_match(AdminClass::CLASS_REGEX, $name, $matches)) {
  144. if (empty($this->group)) {
  145. $this->group = $matches[3];
  146. }
  147. if (empty($this->label)) {
  148. $this->label = $matches[5];
  149. }
  150. }
  151. }
  152. }