AdminExtractor.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. *
  98. * @return \Sonata\AdminBundle\Admin\AdminInterface
  99. */
  100. private function getAdmin($id)
  101. {
  102. return $this->adminPool->getContainer()->get($id);
  103. }
  104. /**
  105. * @param string $id
  106. * @param string $domain
  107. */
  108. private function addMessage($id, $domain)
  109. {
  110. $message = new Message($id, $domain);
  111. // $this->logger->debug(sprintf('extract: %s - domain:%s', $id, $domain));
  112. $trace = debug_backtrace(false);
  113. if (isset($trace[1]['file'])) {
  114. $message->addSource(new FileSource($trace[1]['file']));
  115. }
  116. $this->catalogue->add($message);
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  122. {
  123. $this->addMessage($id, $domain);
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  129. {
  130. $this->addMessage($id, $domain);
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public function setLocale($locale)
  136. {
  137. $this->translator->setLocale($locale);
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public function getLocale()
  143. {
  144. return $this->translator->getLocale();
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  150. {
  151. return true;
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public function buildSecurityInformation(AdminInterface $admin)
  157. {
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. public function createObjectSecurity(AdminInterface $admin, $object)
  163. {
  164. }
  165. /**
  166. * {@inheritDoc}
  167. */
  168. public function deleteObjectSecurity(AdminInterface $admin, $object)
  169. {
  170. }
  171. /**
  172. * {@inheritDoc}
  173. */
  174. public function getBaseRole(AdminInterface $admin)
  175. {
  176. }
  177. /**
  178. * {@inheritDoc}
  179. */
  180. public function getLabel($label, $context = '', $type = '')
  181. {
  182. $label = $this->labelStrategy->getLabel($label, $context, $type);
  183. $this->addMessage($label, $this->domain);
  184. return $label;
  185. }
  186. }