Pool.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. protected $options;
  22. /**
  23. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  24. * @param string $title
  25. * @param string $logoTitle
  26. * @param array $options
  27. */
  28. public function __construct(ContainerInterface $container, $title, $logoTitle, $options = array())
  29. {
  30. $this->container = $container;
  31. $this->title = $title;
  32. $this->titleLogo = $logoTitle;
  33. $this->options = $options;
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function getGroups()
  39. {
  40. $groups = $this->adminGroups;
  41. foreach ($this->adminGroups as $name => $adminGroup) {
  42. foreach ($adminGroup as $id => $options) {
  43. $groups[$name][$id] = $this->getInstance($id);
  44. }
  45. }
  46. return $groups;
  47. }
  48. /**
  49. * Returns whether an admin group exists or not.
  50. *
  51. * @param string $group
  52. * @return bool
  53. */
  54. public function hasGroup($group)
  55. {
  56. return isset($this->adminGroups[$group]);
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function getDashboardGroups()
  62. {
  63. $groups = $this->adminGroups;
  64. foreach ($this->adminGroups as $name => $adminGroup) {
  65. if (isset($adminGroup['items'])) {
  66. foreach ($adminGroup['items'] as $key => $id) {
  67. $admin = $this->getInstance($id);
  68. if ($admin->showIn(Admin::CONTEXT_DASHBOARD)) {
  69. $groups[$name]['items'][$key] = $admin;
  70. } else {
  71. unset($groups[$name]['items'][$key]);
  72. }
  73. }
  74. }
  75. if (empty($groups[$name]['items'])) {
  76. unset($groups[$name]);
  77. }
  78. }
  79. return $groups;
  80. }
  81. /**
  82. * Returns all admins related to the given $group
  83. *
  84. * @param string $group
  85. * @return array
  86. * @throws \InvalidArgumentException
  87. */
  88. public function getAdminsByGroup($group)
  89. {
  90. if (!isset($this->adminGroups[$group])) {
  91. throw new \InvalidArgumentException(sprintf('Group "%s" not found in admin pool.', $group));
  92. }
  93. $admins = array();
  94. if (!isset($this->adminGroups[$group]['items'])) {
  95. return $admins;
  96. }
  97. foreach ($this->adminGroups[$group]['items'] as $id) {
  98. $admins[] = $this->getInstance($id);
  99. }
  100. return $admins;
  101. }
  102. /**
  103. * return the admin related to the given $class
  104. *
  105. * @param string $class
  106. *
  107. * @return \Sonata\AdminBundle\Admin\AdminInterface|null
  108. */
  109. public function getAdminByClass($class)
  110. {
  111. if (!$this->hasAdminByClass($class)) {
  112. return null;
  113. }
  114. if (!is_array($this->adminClasses[$class])) {
  115. throw new \RuntimeException("Invalid format for the Pool::adminClass property");
  116. }
  117. if (count($this->adminClasses[$class]) > 1) {
  118. throw new \RuntimeException(sprintf('Unable to found a valid admin for the class: %s, get too many admin registered: %s', $class, implode(",", $this->adminClasses[$class])));
  119. }
  120. return $this->getInstance($this->adminClasses[$class][0]);
  121. }
  122. /**
  123. * @param string $class
  124. *
  125. * @return bool
  126. */
  127. public function hasAdminByClass($class)
  128. {
  129. return isset($this->adminClasses[$class]);
  130. }
  131. /**
  132. * Returns an admin class by its Admin code
  133. * ie : sonata.news.admin.post|sonata.news.admin.comment => return the child class of post
  134. *
  135. * @param string $adminCode
  136. *
  137. * @return \Sonata\AdminBundle\Admin\AdminInterface|null
  138. */
  139. public function getAdminByAdminCode($adminCode)
  140. {
  141. $codes = explode('|', $adminCode);
  142. $admin = false;
  143. foreach ($codes as $code) {
  144. if ($admin == false) {
  145. $admin = $this->getInstance($code);
  146. } elseif ($admin->hasChild($code)) {
  147. $admin = $admin->getChild($code);
  148. }
  149. }
  150. return $admin;
  151. }
  152. /**
  153. * Returns a new admin instance depends on the given code
  154. *
  155. * @param string $id
  156. *
  157. * @return \Sonata\AdminBundle\Admin\AdminInterface
  158. */
  159. public function getInstance($id)
  160. {
  161. return $this->container->get($id);
  162. }
  163. /**
  164. * @return null|\Symfony\Component\DependencyInjection\ContainerInterface
  165. */
  166. public function getContainer()
  167. {
  168. return $this->container;
  169. }
  170. /**
  171. * @param array $adminGroups
  172. *
  173. * @return void
  174. */
  175. public function setAdminGroups(array $adminGroups)
  176. {
  177. $this->adminGroups = $adminGroups;
  178. }
  179. /**
  180. * @return array
  181. */
  182. public function getAdminGroups()
  183. {
  184. return $this->adminGroups;
  185. }
  186. /**
  187. * @param array $adminServiceIds
  188. *
  189. * @return void
  190. */
  191. public function setAdminServiceIds(array $adminServiceIds)
  192. {
  193. $this->adminServiceIds = $adminServiceIds;
  194. }
  195. /**
  196. * @return array
  197. */
  198. public function getAdminServiceIds()
  199. {
  200. return $this->adminServiceIds;
  201. }
  202. /**
  203. * @param array $adminClasses
  204. *
  205. * @return void
  206. */
  207. public function setAdminClasses(array $adminClasses)
  208. {
  209. $this->adminClasses = $adminClasses;
  210. }
  211. /**
  212. * @return array
  213. */
  214. public function getAdminClasses()
  215. {
  216. return $this->adminClasses;
  217. }
  218. /**
  219. * @param array $templates
  220. *
  221. * @return void
  222. */
  223. public function setTemplates(array $templates)
  224. {
  225. $this->templates = $templates;
  226. }
  227. /**
  228. * @return array
  229. */
  230. public function getTemplates()
  231. {
  232. return $this->templates;
  233. }
  234. /**
  235. * @param string $name
  236. *
  237. * @return null|string
  238. */
  239. public function getTemplate($name)
  240. {
  241. if (isset($this->templates[$name])) {
  242. return $this->templates[$name];
  243. }
  244. return null;
  245. }
  246. /**
  247. * @return string
  248. */
  249. public function getTitleLogo()
  250. {
  251. return $this->titleLogo;
  252. }
  253. /**
  254. * @return string
  255. */
  256. public function getTitle()
  257. {
  258. return $this->title;
  259. }
  260. /**
  261. * @param $name
  262. *
  263. * @return mixed
  264. */
  265. public function getOption($name)
  266. {
  267. if (isset($this->options[$name])) {
  268. return $this->options[$name];
  269. }
  270. return null;
  271. }
  272. }