Pool.php 5.3 KB

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