Pool.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /**
  52. * return the admin related to the given $class
  53. *
  54. * @param string $class
  55. * @return Admin|null
  56. */
  57. public function getAdminByClass($class)
  58. {
  59. $configuration_code = false;
  60. foreach ($this->configuration as $code => $configuration) {
  61. if ($configuration['entity'] == $class) {
  62. $configuration_code = $code;
  63. break;
  64. }
  65. }
  66. if (!$configuration_code) {
  67. return null;
  68. }
  69. return $this->getInstance($code);
  70. }
  71. /**
  72. * return the admin related to the given $actionName
  73. *
  74. * @param string $actionName
  75. * @return Admin|null
  76. */
  77. public function getAdminByActionName($actionName)
  78. {
  79. $codes = explode('.', $actionName);
  80. $instance = false;
  81. foreach ($codes as $pos => $code) {
  82. if ($pos == 0) {
  83. $instance = $this->getInstance($code);
  84. } else if($instance->hasChildren()) {
  85. if(!$instance->hasChild($code)) {
  86. break;
  87. }
  88. $instance = $instance->getChild($code);
  89. if(!$instance instanceof Admin) {
  90. throw new \RuntimeException(sprintf('unable to retrieve the child admin related to the actionName : `%s`', $actionName));
  91. }
  92. } else {
  93. break;
  94. }
  95. }
  96. if(!$instance instanceof Admin) {
  97. throw new \RuntimeException(sprintf('unable to retrieve the admin related to the actionName : `%s`', $actionName));
  98. }
  99. return $instance;
  100. }
  101. /**
  102. *
  103. * return a new admin instance depends on the given code
  104. *
  105. * @param $code
  106. * @return
  107. */
  108. public function getInstance($code)
  109. {
  110. if(!isset($this->configuration[$code])) {
  111. throw new \RuntimeException(sprintf('The code `%s` does not exist', $code));
  112. }
  113. return $this->getInstanceFromConfiguration($code, $this->configuration[$code]);
  114. }
  115. protected function getInstanceFromConfiguration($code, $configuration)
  116. {
  117. $class = $configuration['class'];
  118. $instance = new $class($code, $this->getContainer(), $configuration['entity']);
  119. $instance->setLabel($configuration['label']);
  120. if(isset($configuration['children'])) {
  121. foreach($configuration['children'] as $code => $child) {
  122. $instance->addChild($code, $this->getInstanceFromConfiguration($code, $child));
  123. }
  124. }
  125. return $instance;
  126. }
  127. /**
  128. * return a group of admin instance
  129. *
  130. * @return array
  131. */
  132. public function getInstances()
  133. {
  134. $instances = array();
  135. foreach ($this->configuration as $code => $configuration) {
  136. $instances[] = $this->getInstance($code);
  137. }
  138. return $instances;
  139. }
  140. public function setConfiguration(array $configuration = array())
  141. {
  142. $this->configuration = $configuration;
  143. }
  144. public function getConfiguration()
  145. {
  146. return $this->configuration;
  147. }
  148. public function setContainer($container)
  149. {
  150. $this->container = $container;
  151. }
  152. public function getContainer()
  153. {
  154. return $this->container;
  155. }
  156. }