Pool.php 7.5 KB

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