Pool.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. if (false === $codes) {
  193. return false;
  194. }
  195. $admin = $this->getInstance($codes[0]);
  196. array_shift($codes);
  197. if (empty($codes)) {
  198. return $admin;
  199. }
  200. foreach ($codes as $code) {
  201. if (!$admin->hasChild($code)) {
  202. return false;
  203. }
  204. $admin = $admin->getChild($code);
  205. }
  206. return $admin;
  207. }
  208. /**
  209. * Returns a new admin instance depends on the given code.
  210. *
  211. * @param string $id
  212. *
  213. * @return AdminInterface
  214. *
  215. * @throws \InvalidArgumentException
  216. */
  217. public function getInstance($id)
  218. {
  219. if (!in_array($id, $this->adminServiceIds)) {
  220. throw new \InvalidArgumentException(sprintf('Admin service "%s" not found in admin pool.', $id));
  221. }
  222. return $this->container->get($id);
  223. }
  224. /**
  225. * @return ContainerInterface|null
  226. */
  227. public function getContainer()
  228. {
  229. return $this->container;
  230. }
  231. /**
  232. * @param array $adminGroups
  233. */
  234. public function setAdminGroups(array $adminGroups)
  235. {
  236. $this->adminGroups = $adminGroups;
  237. }
  238. /**
  239. * @return array
  240. */
  241. public function getAdminGroups()
  242. {
  243. return $this->adminGroups;
  244. }
  245. /**
  246. * @param array $adminServiceIds
  247. */
  248. public function setAdminServiceIds(array $adminServiceIds)
  249. {
  250. $this->adminServiceIds = $adminServiceIds;
  251. }
  252. /**
  253. * @return array
  254. */
  255. public function getAdminServiceIds()
  256. {
  257. return $this->adminServiceIds;
  258. }
  259. /**
  260. * @param array $adminClasses
  261. */
  262. public function setAdminClasses(array $adminClasses)
  263. {
  264. $this->adminClasses = $adminClasses;
  265. }
  266. /**
  267. * @return array
  268. */
  269. public function getAdminClasses()
  270. {
  271. return $this->adminClasses;
  272. }
  273. /**
  274. * @param array $templates
  275. */
  276. public function setTemplates(array $templates)
  277. {
  278. $this->templates = $templates;
  279. }
  280. /**
  281. * @return array
  282. */
  283. public function getTemplates()
  284. {
  285. return $this->templates;
  286. }
  287. /**
  288. * @param string $name
  289. *
  290. * @return null|string
  291. */
  292. public function getTemplate($name)
  293. {
  294. if (isset($this->templates[$name])) {
  295. return $this->templates[$name];
  296. }
  297. }
  298. /**
  299. * @return string
  300. */
  301. public function getTitleLogo()
  302. {
  303. return $this->titleLogo;
  304. }
  305. /**
  306. * @return string
  307. */
  308. public function getTitle()
  309. {
  310. return $this->title;
  311. }
  312. /**
  313. * @param string $name
  314. * @param mixed $default
  315. *
  316. * @return mixed
  317. */
  318. public function getOption($name, $default = null)
  319. {
  320. if (isset($this->options[$name])) {
  321. return $this->options[$name];
  322. }
  323. return $default;
  324. }
  325. public function getPropertyAccessor()
  326. {
  327. if (null === $this->propertyAccessor) {
  328. $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
  329. }
  330. return $this->propertyAccessor;
  331. }
  332. }