Pool.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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\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. *
  54. * @return bool
  55. */
  56. public function hasGroup($group)
  57. {
  58. return isset($this->adminGroups[$group]);
  59. }
  60. /**
  61. * @return array
  62. */
  63. public function getDashboardGroups()
  64. {
  65. $groups = $this->adminGroups;
  66. foreach ($this->adminGroups as $name => $adminGroup) {
  67. if (isset($adminGroup['items'])) {
  68. foreach ($adminGroup['items'] as $key => $id) {
  69. $admin = $this->getInstance($id);
  70. if ($admin->showIn(Admin::CONTEXT_DASHBOARD)) {
  71. $groups[$name]['items'][$key] = $admin;
  72. } else {
  73. unset($groups[$name]['items'][$key]);
  74. }
  75. }
  76. }
  77. if (empty($groups[$name]['items'])) {
  78. unset($groups[$name]);
  79. }
  80. }
  81. return $groups;
  82. }
  83. /**
  84. * Returns all admins related to the given $group.
  85. *
  86. * @param string $group
  87. *
  88. * @return array
  89. *
  90. * @throws \InvalidArgumentException
  91. */
  92. public function getAdminsByGroup($group)
  93. {
  94. if (!isset($this->adminGroups[$group])) {
  95. throw new \InvalidArgumentException(sprintf('Group "%s" not found in admin pool.', $group));
  96. }
  97. $admins = array();
  98. if (!isset($this->adminGroups[$group]['items'])) {
  99. return $admins;
  100. }
  101. foreach ($this->adminGroups[$group]['items'] as $id) {
  102. $admins[] = $this->getInstance($id);
  103. }
  104. return $admins;
  105. }
  106. /**
  107. * Return the admin related to the given $class.
  108. *
  109. * @param string $class
  110. *
  111. * @return \Sonata\AdminBundle\Admin\AdminInterface|null
  112. */
  113. public function getAdminByClass($class)
  114. {
  115. if (!$this->hasAdminByClass($class)) {
  116. return;
  117. }
  118. if (!is_array($this->adminClasses[$class])) {
  119. throw new \RuntimeException('Invalid format for the Pool::adminClass property');
  120. }
  121. if (count($this->adminClasses[$class]) > 1) {
  122. 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])));
  123. }
  124. return $this->getInstance($this->adminClasses[$class][0]);
  125. }
  126. /**
  127. * @param string $class
  128. *
  129. * @return bool
  130. */
  131. public function hasAdminByClass($class)
  132. {
  133. return isset($this->adminClasses[$class]);
  134. }
  135. /**
  136. * Returns an admin class by its Admin code
  137. * ie : sonata.news.admin.post|sonata.news.admin.comment => return the child class of post.
  138. *
  139. * @param string $adminCode
  140. *
  141. * @return \Sonata\AdminBundle\Admin\AdminInterface|null
  142. */
  143. public function getAdminByAdminCode($adminCode)
  144. {
  145. $codes = explode('|', $adminCode);
  146. $admin = false;
  147. foreach ($codes as $code) {
  148. if ($admin == false) {
  149. $admin = $this->getInstance($code);
  150. } elseif ($admin->hasChild($code)) {
  151. $admin = $admin->getChild($code);
  152. }
  153. }
  154. return $admin;
  155. }
  156. /**
  157. * Returns a new admin instance depends on the given code.
  158. *
  159. * @param string $id
  160. *
  161. * @return \Sonata\AdminBundle\Admin\AdminInterface
  162. *
  163. * @throws \InvalidArgumentException
  164. */
  165. public function getInstance($id)
  166. {
  167. if (!in_array($id, $this->adminServiceIds)) {
  168. throw new \InvalidArgumentException(sprintf('Admin service "%s" not found in admin pool.', $id));
  169. }
  170. return $this->container->get($id);
  171. }
  172. /**
  173. * @return null|\Symfony\Component\DependencyInjection\ContainerInterface
  174. */
  175. public function getContainer()
  176. {
  177. return $this->container;
  178. }
  179. /**
  180. * @param array $adminGroups
  181. */
  182. public function setAdminGroups(array $adminGroups)
  183. {
  184. $this->adminGroups = $adminGroups;
  185. }
  186. /**
  187. * @return array
  188. */
  189. public function getAdminGroups()
  190. {
  191. return $this->adminGroups;
  192. }
  193. /**
  194. * @param array $adminServiceIds
  195. */
  196. public function setAdminServiceIds(array $adminServiceIds)
  197. {
  198. $this->adminServiceIds = $adminServiceIds;
  199. }
  200. /**
  201. * @return array
  202. */
  203. public function getAdminServiceIds()
  204. {
  205. return $this->adminServiceIds;
  206. }
  207. /**
  208. * @param array $adminClasses
  209. */
  210. public function setAdminClasses(array $adminClasses)
  211. {
  212. $this->adminClasses = $adminClasses;
  213. }
  214. /**
  215. * @return array
  216. */
  217. public function getAdminClasses()
  218. {
  219. return $this->adminClasses;
  220. }
  221. /**
  222. * @param array $templates
  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;
  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. }