Pool.php 8.3 KB

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