Pool.php 7.4 KB

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