Pool.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\BaseApplicationBundle\Admin;
  11. class Pool
  12. {
  13. protected $container = null;
  14. protected $configuration = array();
  15. public function addConfiguration($code, $configuration)
  16. {
  17. $configuration['code'] = $code;
  18. $this->configuration[$code] = $configuration;
  19. }
  20. public function getGroups()
  21. {
  22. $groups = array();
  23. foreach ($this->configuration as $configuration) {
  24. if (!isset($groups[$configuration['group']])) {
  25. $groups[$configuration['group']] = array();
  26. }
  27. $groups[$configuration['group']][$configuration['code']] = $this->getInstance($configuration['code']);
  28. }
  29. return $groups;
  30. }
  31. /**
  32. * The admin classes are lazy loaded to avoid overhead
  33. *
  34. * @throws RuntimeException
  35. * @param $name
  36. * @return
  37. */
  38. public function getAdminByControllerName($name)
  39. {
  40. $configuration_code = false;
  41. foreach ($this->configuration as $code => $configuration) {
  42. if ($configuration['controller'] == $name) {
  43. $configuration_code = $code;
  44. }
  45. }
  46. if (!$configuration_code) {
  47. return null;
  48. }
  49. return $this->getInstance($configuration_code);
  50. }
  51. public function getAdminByClass($class)
  52. {
  53. $configuration_code = false;
  54. foreach ($this->configuration as $code => $configuration) {
  55. if ($configuration['entity'] == $class) {
  56. $configuration_code = $code;
  57. break;
  58. }
  59. }
  60. if (!$configuration_code) {
  61. return null;
  62. }
  63. return $this->getInstance($code);
  64. }
  65. /**
  66. *
  67. * return a new admin instance depends on the given code
  68. *
  69. * @param $code
  70. * @return
  71. */
  72. public function getInstance($code)
  73. {
  74. $class = $this->configuration[$code]['class'];
  75. $instance = new $class;
  76. $instance->setContainer($this->getContainer());
  77. $instance->setConfigurationPool($this);
  78. $instance->setCode($code);
  79. $instance->setLabel($this->configuration[$code]['label']);
  80. $instance->configure();
  81. return $instance;
  82. }
  83. /**
  84. * return a group of admin instance
  85. *
  86. * @return array
  87. */
  88. public function getInstances()
  89. {
  90. $instances = array();
  91. foreach ($this->configuration as $code => $configuration) {
  92. $instances[] = $this->getInstance($code);
  93. }
  94. return $instances;
  95. }
  96. public function setConfiguration(array $configuration = array())
  97. {
  98. $this->configuration = $configuration;
  99. }
  100. public function getConfiguration()
  101. {
  102. return $this->configuration;
  103. }
  104. public function setContainer($container)
  105. {
  106. $this->container = $container;
  107. }
  108. public function getContainer()
  109. {
  110. return $this->container;
  111. }
  112. }