AdminExtractor.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Sonata\AdminBundle\Translator\Extractor\JMSTranslatorBundle;
  3. use Symfony\Component\Translation\TranslatorInterface;
  4. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  5. use Sonata\AdminBundle\Admin\Pool;
  6. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  7. use Sonata\AdminBundle\Admin\AdminInterface;
  8. use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
  9. use JMS\TranslationBundle\Translation\ExtractorInterface;
  10. use JMS\TranslationBundle\Model\MessageCatalogue;
  11. use JMS\TranslationBundle\Model\Message;
  12. use JMS\TranslationBundle\Model\FileSource;
  13. class AdminExtractor implements ExtractorInterface, TranslatorInterface, SecurityHandlerInterface, LabelTranslatorStrategyInterface
  14. {
  15. private $logger;
  16. private $adminPool;
  17. private $catalogue;
  18. private $translator;
  19. private $labelStrategy;
  20. private $domain;
  21. /**
  22. * @param \Sonata\AdminBundle\Admin\Pool $adminPool
  23. * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
  24. */
  25. public function __construct(Pool $adminPool, LoggerInterface $logger = null)
  26. {
  27. $this->logger = $logger;
  28. $this->adminPool = $adminPool;
  29. // state variable
  30. $this->catalogue = false;
  31. $this->translator = false;
  32. $this->labelStrategy = false;
  33. $this->domain = false;
  34. }
  35. /**
  36. * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
  37. */
  38. public function setLogger(LoggerInterface $logger)
  39. {
  40. $this->logger = $logger;
  41. }
  42. /**
  43. * @return bool
  44. * @throws \Exception|\RuntimeException
  45. */
  46. public function extract()
  47. {
  48. if ($this->catalogue) {
  49. throw new \RuntimeException('Invalid state');
  50. }
  51. $this->catalogue = new MessageCatalogue;
  52. foreach ($this->adminPool->getAdminServiceIds() as $id) {
  53. $admin = $this->getAdmin($id);
  54. $this->translator = $admin->getTranslator();
  55. $this->labelStrategy = $admin->getLabelTranslatorStrategy();
  56. $this->domain = $admin->getTranslationDomain();
  57. $admin->setTranslator($this);
  58. $admin->setSecurityHandler($this);
  59. $admin->setLabelTranslatorStrategy($this);
  60. // foreach ($admin->getChildren() as $child) {
  61. // $child->setTranslator($this);
  62. // }
  63. // call the different public method
  64. $methods = array(
  65. 'getShow' => array(array()),
  66. 'getDatagrid' => array(array()),
  67. 'getList' => array(array()),
  68. 'getForm' => array(array()),
  69. 'getBreadcrumbs' => array(
  70. array('list'),
  71. array('edit'),
  72. array('create'),
  73. array('update'),
  74. array('batch'),
  75. array('delete'),
  76. )
  77. );
  78. if ($this->logger) {
  79. $this->logger->info(sprintf('Retrieving message from admin:%s - class: %s', $admin->getCode(), get_class($admin)));
  80. }
  81. foreach ($methods as $method => $calls) {
  82. foreach ($calls as $args) {
  83. try {
  84. call_user_func_array(array($admin, $method), $args);
  85. } catch (\Exception $e) {
  86. if ($this->logger) {
  87. $this->logger->err(sprintf('ERROR : admin:%s - Raise an exception : %s', $admin->getCode(), $e->getMessage()));
  88. }
  89. throw $e;
  90. }
  91. }
  92. }
  93. }
  94. $catalogue = $this->catalogue;
  95. $this->catalogue = false;
  96. return $catalogue;
  97. }
  98. /**
  99. * @param string $id
  100. *
  101. * @return \Sonata\AdminBundle\Admin\AdminInterface
  102. */
  103. private function getAdmin($id)
  104. {
  105. return $this->adminPool->getContainer()->get($id);
  106. }
  107. /**
  108. * @param string $id
  109. * @param string $domain
  110. */
  111. private function addMessage($id, $domain)
  112. {
  113. $message = new Message($id, $domain);
  114. // $this->logger->debug(sprintf('extract: %s - domain:%s', $id, $domain));
  115. $trace = debug_backtrace(false);
  116. if (isset($trace[1]['file'])) {
  117. $message->addSource(new FileSource($trace[1]['file']));
  118. }
  119. $this->catalogue->add($message);
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  125. {
  126. $this->addMessage($id, $domain);
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  132. {
  133. $this->addMessage($id, $domain);
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function setLocale($locale)
  139. {
  140. $this->translator->setLocale($locale);
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. public function getLocale()
  146. {
  147. return $this->translator->getLocale();
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  153. {
  154. return true;
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function buildSecurityInformation(AdminInterface $admin)
  160. {
  161. }
  162. /**
  163. * {@inheritDoc}
  164. */
  165. public function createObjectSecurity(AdminInterface $admin, $object)
  166. {
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. public function deleteObjectSecurity(AdminInterface $admin, $object)
  172. {
  173. }
  174. /**
  175. * {@inheritDoc}
  176. */
  177. public function getBaseRole(AdminInterface $admin)
  178. {
  179. }
  180. /**
  181. * {@inheritDoc}
  182. */
  183. public function getLabel($label, $context = '', $type = '')
  184. {
  185. $label = $this->labelStrategy->getLabel($label, $context, $type);
  186. $this->addMessage($label, $this->domain);
  187. return $label;
  188. }
  189. }