123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Admin;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\PropertyAccess\PropertyAccess;
- use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
- /**
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- class Pool
- {
- /**
- * @var ContainerInterface
- */
- protected $container;
- /**
- * @var string[]
- */
- protected $adminServiceIds = array();
- /**
- * @var array
- */
- protected $adminGroups = array();
- /**
- * @var array
- */
- protected $adminClasses = array();
- /**
- * @var string[]
- */
- protected $templates = array();
- /**
- * @var array
- */
- protected $assets = array();
- /**
- * @var string
- */
- protected $title;
- /**
- * @var string
- */
- protected $titleLogo;
- /**
- * @var array
- */
- protected $options;
- /**
- * @var PropertyAccessorInterface
- */
- protected $propertyAccessor;
- /**
- * @param ContainerInterface $container
- * @param string $title
- * @param string $logoTitle
- * @param array $options
- */
- public function __construct(ContainerInterface $container, $title, $logoTitle, $options = array(), PropertyAccessorInterface $propertyAccessor = null)
- {
- $this->container = $container;
- $this->title = $title;
- $this->titleLogo = $logoTitle;
- $this->options = $options;
- $this->propertyAccessor = $propertyAccessor;
- }
- /**
- * @return array
- */
- public function getGroups()
- {
- $groups = $this->adminGroups;
- foreach ($this->adminGroups as $name => $adminGroup) {
- foreach ($adminGroup as $id => $options) {
- $groups[$name][$id] = $this->getInstance($id);
- }
- }
- return $groups;
- }
- /**
- * Returns whether an admin group exists or not.
- *
- * @param string $group
- *
- * @return bool
- */
- public function hasGroup($group)
- {
- return isset($this->adminGroups[$group]);
- }
- /**
- * @return array
- */
- public function getDashboardGroups()
- {
- $groups = $this->adminGroups;
- foreach ($this->adminGroups as $name => $adminGroup) {
- if (isset($adminGroup['items'])) {
- foreach ($adminGroup['items'] as $key => $item) {
- // Only Admin Group should be returned
- if ('' != $item['admin']) {
- $admin = $this->getInstance($item['admin']);
- if ($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)) {
- $groups[$name]['items'][$key] = $admin;
- } else {
- unset($groups[$name]['items'][$key]);
- }
- } else {
- unset($groups[$name]['items'][$key]);
- }
- }
- }
- if (empty($groups[$name]['items'])) {
- unset($groups[$name]);
- }
- }
- return $groups;
- }
- /**
- * Returns all admins related to the given $group.
- *
- * @param string $group
- *
- * @return array
- *
- * @throws \InvalidArgumentException
- */
- public function getAdminsByGroup($group)
- {
- if (!isset($this->adminGroups[$group])) {
- throw new \InvalidArgumentException(sprintf('Group "%s" not found in admin pool.', $group));
- }
- $admins = array();
- if (!isset($this->adminGroups[$group]['items'])) {
- return $admins;
- }
- foreach ($this->adminGroups[$group]['items'] as $item) {
- $admins[] = $this->getInstance($item['admin']);
- }
- return $admins;
- }
- /**
- * Return the admin related to the given $class.
- *
- * @param string $class
- *
- * @return \Sonata\AdminBundle\Admin\AdminInterface|null
- */
- public function getAdminByClass($class)
- {
- if (!$this->hasAdminByClass($class)) {
- return;
- }
- if (!is_array($this->adminClasses[$class])) {
- throw new \RuntimeException('Invalid format for the Pool::adminClass property');
- }
- if (count($this->adminClasses[$class]) > 1) {
- 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])
- ));
- }
- return $this->getInstance($this->adminClasses[$class][0]);
- }
- /**
- * @param string $class
- *
- * @return bool
- */
- public function hasAdminByClass($class)
- {
- return isset($this->adminClasses[$class]);
- }
- /**
- * Returns an admin class by its Admin code
- * ie : sonata.news.admin.post|sonata.news.admin.comment => return the child class of post.
- *
- * @param string $adminCode
- *
- * @return \Sonata\AdminBundle\Admin\AdminInterface|false|null
- */
- public function getAdminByAdminCode($adminCode)
- {
- $codes = explode('|', $adminCode);
- $admin = false;
- foreach ($codes as $code) {
- if ($admin == false) {
- $admin = $this->getInstance($code);
- } elseif ($admin->hasChild($code)) {
- $admin = $admin->getChild($code);
- }
- }
- return $admin;
- }
- /**
- * Returns a new admin instance depends on the given code.
- *
- * @param string $id
- *
- * @return AdminInterface
- *
- * @throws \InvalidArgumentException
- */
- public function getInstance($id)
- {
- if (!in_array($id, $this->adminServiceIds)) {
- throw new \InvalidArgumentException(sprintf('Admin service "%s" not found in admin pool.', $id));
- }
- return $this->container->get($id);
- }
- /**
- * @return ContainerInterface|null
- */
- public function getContainer()
- {
- return $this->container;
- }
- /**
- * @param array $adminGroups
- */
- public function setAdminGroups(array $adminGroups)
- {
- $this->adminGroups = $adminGroups;
- }
- /**
- * @return array
- */
- public function getAdminGroups()
- {
- return $this->adminGroups;
- }
- /**
- * @param array $adminServiceIds
- */
- public function setAdminServiceIds(array $adminServiceIds)
- {
- $this->adminServiceIds = $adminServiceIds;
- }
- /**
- * @return array
- */
- public function getAdminServiceIds()
- {
- return $this->adminServiceIds;
- }
- /**
- * @param array $adminClasses
- */
- public function setAdminClasses(array $adminClasses)
- {
- $this->adminClasses = $adminClasses;
- }
- /**
- * @return array
- */
- public function getAdminClasses()
- {
- return $this->adminClasses;
- }
- /**
- * @param array $templates
- */
- public function setTemplates(array $templates)
- {
- $this->templates = $templates;
- }
- /**
- * @return array
- */
- public function getTemplates()
- {
- return $this->templates;
- }
- /**
- * @param string $name
- *
- * @return null|string
- */
- public function getTemplate($name)
- {
- if (isset($this->templates[$name])) {
- return $this->templates[$name];
- }
- }
- /**
- * @return string
- */
- public function getTitleLogo()
- {
- return $this->titleLogo;
- }
- /**
- * @return string
- */
- public function getTitle()
- {
- return $this->title;
- }
- /**
- * @param string $name
- * @param mixed $default
- *
- * @return mixed
- */
- public function getOption($name, $default = null)
- {
- if (isset($this->options[$name])) {
- return $this->options[$name];
- }
- return $default;
- }
- public function getPropertyAccessor()
- {
- if (null === $this->propertyAccessor) {
- $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
- }
- return $this->propertyAccessor;
- }
- }
|