Pool.php 7.8 KB

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