Admin.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. $metadata->tags['sonata.admin'][] = $tag;
  108. $metadata->arguments = array($this->id, $this->class, $this->baseControllerName);
  109. if ($this->translationDomain) {
  110. $metadata->methodCalls[] = array('setTranslationDomain', array($this->translationDomain));
  111. }
  112. }
  113. /**
  114. * Check if all the required fields are given.
  115. */
  116. private function validate()
  117. {
  118. if (!$this->showInDashboard) {
  119. return;
  120. }
  121. if (empty($this->group) || empty($this->label)) {
  122. throw new \LogicException(
  123. sprintf(
  124. 'Unable to generate admin group and label for class %s.',
  125. $this->class
  126. )
  127. );
  128. }
  129. }
  130. /**
  131. * Set group and label from class name it not set.
  132. *
  133. * @param $name
  134. */
  135. private function generateFallback($name)
  136. {
  137. if (empty($name)) {
  138. return;
  139. }
  140. if (preg_match(AdminClass::CLASS_REGEX, $name, $matches)) {
  141. if (empty($this->group)) {
  142. $this->group = $matches[3];
  143. }
  144. if (empty($this->label)) {
  145. $this->label = $matches[5];
  146. }
  147. }
  148. }
  149. }