Pool.php 7.1 KB

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