Pool.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /*
  3. * This file is part of the Sonata 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\Admin;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. class Pool
  13. {
  14. protected $container = null;
  15. protected $adminServiceIds = array();
  16. protected $adminGroups = array();
  17. protected $adminClasses = array();
  18. protected $templates = array();
  19. protected $title;
  20. protected $titleLogo;
  21. /**
  22. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  23. */
  24. public function __construct(ContainerInterface $container, $title, $logoTitle)
  25. {
  26. $this->container = $container;
  27. $this->title = $title;
  28. $this->titleLogo = $logoTitle;
  29. }
  30. /**
  31. * @return array
  32. */
  33. public function getGroups()
  34. {
  35. $groups = $this->adminGroups;
  36. foreach ($this->adminGroups as $name => $adminGroup) {
  37. foreach ($adminGroup as $id => $options) {
  38. $groups[$name][$id] = $this->getInstance($id);
  39. }
  40. }
  41. return $groups;
  42. }
  43. /**
  44. * @return array
  45. */
  46. public function getDashboardGroups()
  47. {
  48. $groups = $this->adminGroups;
  49. foreach ($this->adminGroups as $name => $adminGroup) {
  50. if (isset($adminGroup['items'])) {
  51. foreach ($adminGroup['items'] as $key => $id) {
  52. $groups[$name]['items'][$key] = $this->getInstance($id);
  53. }
  54. }
  55. if (empty($groups[$name])) {
  56. unset($groups[$name]);
  57. }
  58. }
  59. return $groups;
  60. }
  61. /**
  62. * return the admin related to the given $class
  63. *
  64. * @param string $class
  65. * @return \Sonata\AdminBundle\Admin\AdminInterface|null
  66. */
  67. public function getAdminByClass($class)
  68. {
  69. if (!isset($this->adminClasses[$class])) {
  70. return null;
  71. }
  72. return $this->getInstance($this->adminClasses[$class]);
  73. }
  74. /**
  75. * Returns an admin class by its Admin code
  76. * ie : sonata.news.admin.post|sonata.news.admin.comment => return the child class of post
  77. *
  78. * @param string $adminCode
  79. * @return \Sonata\AdminBundle\Admin\AdminInterface|null
  80. */
  81. public function getAdminByAdminCode($adminCode)
  82. {
  83. $codes = explode('|', $adminCode);
  84. $admin = false;
  85. foreach ($codes as $code) {
  86. if ($admin == false) {
  87. $admin = $this->getInstance($code);
  88. } else if ($admin->hasChild($code)) {
  89. $admin = $admin->getChild($code);
  90. }
  91. }
  92. return $admin;
  93. }
  94. /**
  95. * Returns a new admin instance depends on the given code
  96. *
  97. * @param $id
  98. * @return \Sonata\AdminBundle\Admin\AdminInterface
  99. */
  100. public function getInstance($id)
  101. {
  102. return $this->container->get($id);
  103. }
  104. /**
  105. * @return null|\Symfony\Component\DependencyInjection\ContainerInterface
  106. */
  107. public function getContainer()
  108. {
  109. return $this->container;
  110. }
  111. /**
  112. * @param array $adminGroups
  113. * @return void
  114. */
  115. public function setAdminGroups(array $adminGroups)
  116. {
  117. $this->adminGroups = $adminGroups;
  118. }
  119. /**
  120. * @return array
  121. */
  122. public function getAdminGroups()
  123. {
  124. return $this->adminGroups;
  125. }
  126. /**
  127. * @param array $adminServiceIds
  128. * @return void
  129. */
  130. public function setAdminServiceIds(array $adminServiceIds)
  131. {
  132. $this->adminServiceIds = $adminServiceIds;
  133. }
  134. /**
  135. * @return array
  136. */
  137. public function getAdminServiceIds()
  138. {
  139. return $this->adminServiceIds;
  140. }
  141. /**
  142. * @param array $adminClasses
  143. * @return void
  144. */
  145. public function setAdminClasses(array $adminClasses)
  146. {
  147. $this->adminClasses = $adminClasses;
  148. }
  149. /**
  150. * @return array
  151. */
  152. public function getAdminClasses()
  153. {
  154. return $this->adminClasses;
  155. }
  156. /**
  157. * @param array $templates
  158. * @return void
  159. */
  160. public function setTemplates(array $templates)
  161. {
  162. $this->templates = $templates;
  163. }
  164. /**
  165. * @return array
  166. */
  167. public function getTemplates()
  168. {
  169. return $this->templates;
  170. }
  171. /**
  172. * @param $name
  173. * @return null|string
  174. */
  175. public function getTemplate($name)
  176. {
  177. if (isset($this->templates[$name])) {
  178. return $this->templates[$name];
  179. }
  180. return null;
  181. }
  182. /**
  183. * @return string
  184. */
  185. public function getTitleLogo()
  186. {
  187. return $this->titleLogo;
  188. }
  189. /**
  190. * @return string
  191. */
  192. public function getTitle()
  193. {
  194. return $this->title;
  195. }
  196. }