AdminExtractor.php 5.5 KB

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