Admin.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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) { return !is_null($v); });
  95. $metadata->tags['sonata.admin'][] = $tag;
  96. $metadata->arguments = array($this->id, $this->class, $this->baseControllerName);
  97. if ($this->translationDomain) {
  98. $metadata->methodCalls[] = array('setTranslationDomain', array($this->translationDomain));
  99. }
  100. }
  101. /**
  102. * Check if all the required fields are given.
  103. */
  104. private function validate()
  105. {
  106. if (!$this->showInDashboard) {
  107. return;
  108. }
  109. if (empty($this->group) || empty($this->label)) {
  110. throw new \LogicException(
  111. sprintf(
  112. 'Unable to generate admin group and label for class %s.',
  113. $this->class
  114. )
  115. );
  116. }
  117. }
  118. /**
  119. * Set group and label from class name it not set.
  120. *
  121. * @param $name
  122. */
  123. private function generateFallback($name)
  124. {
  125. if (empty($name)) {
  126. return;
  127. }
  128. if (preg_match(AdminClass::CLASS_REGEX, $name, $matches)) {
  129. if (empty($this->group)) {
  130. $this->group = $matches[3];
  131. }
  132. if (empty($this->label)) {
  133. $this->label = $matches[5];
  134. }
  135. }
  136. }
  137. }