Pool.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. *
  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 null;
  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. * @return void
  183. */
  184. public function setAdminGroups(array $adminGroups)
  185. {
  186. $this->adminGroups = $adminGroups;
  187. }
  188. /**
  189. * @return array
  190. */
  191. public function getAdminGroups()
  192. {
  193. return $this->adminGroups;
  194. }
  195. /**
  196. * @param array $adminServiceIds
  197. *
  198. * @return void
  199. */
  200. public function setAdminServiceIds(array $adminServiceIds)
  201. {
  202. $this->adminServiceIds = $adminServiceIds;
  203. }
  204. /**
  205. * @return array
  206. */
  207. public function getAdminServiceIds()
  208. {
  209. return $this->adminServiceIds;
  210. }
  211. /**
  212. * @param array $adminClasses
  213. *
  214. * @return void
  215. */
  216. public function setAdminClasses(array $adminClasses)
  217. {
  218. $this->adminClasses = $adminClasses;
  219. }
  220. /**
  221. * @return array
  222. */
  223. public function getAdminClasses()
  224. {
  225. return $this->adminClasses;
  226. }
  227. /**
  228. * @param array $templates
  229. *
  230. * @return void
  231. */
  232. public function setTemplates(array $templates)
  233. {
  234. $this->templates = $templates;
  235. }
  236. /**
  237. * @return array
  238. */
  239. public function getTemplates()
  240. {
  241. return $this->templates;
  242. }
  243. /**
  244. * @param string $name
  245. *
  246. * @return null|string
  247. */
  248. public function getTemplate($name)
  249. {
  250. if (isset($this->templates[$name])) {
  251. return $this->templates[$name];
  252. }
  253. return null;
  254. }
  255. /**
  256. * @return string
  257. */
  258. public function getTitleLogo()
  259. {
  260. return $this->titleLogo;
  261. }
  262. /**
  263. * @return string
  264. */
  265. public function getTitle()
  266. {
  267. return $this->title;
  268. }
  269. /**
  270. * @param string $name
  271. * @param mixed $default
  272. *
  273. * @return mixed
  274. */
  275. public function getOption($name, $default = null)
  276. {
  277. if (isset($this->options[$name])) {
  278. return $this->options[$name];
  279. }
  280. return $default;
  281. }
  282. }