Pool.php 7.5 KB

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