Pool.php 8.4 KB

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