AdminExtractor.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * Extract messages to MessageCatalogue
  44. *
  45. * @return MessageCatalogue
  46. *
  47. * @throws \Exception|\RuntimeException
  48. */
  49. public function extract()
  50. {
  51. if ($this->catalogue) {
  52. throw new \RuntimeException('Invalid state');
  53. }
  54. $this->catalogue = new MessageCatalogue();
  55. foreach ($this->adminPool->getAdminServiceIds() as $id) {
  56. $admin = $this->getAdmin($id);
  57. $this->translator = $admin->getTranslator();
  58. $this->labelStrategy = $admin->getLabelTranslatorStrategy();
  59. $this->domain = $admin->getTranslationDomain();
  60. $admin->setTranslator($this);
  61. $admin->setSecurityHandler($this);
  62. $admin->setLabelTranslatorStrategy($this);
  63. // foreach ($admin->getChildren() as $child) {
  64. // $child->setTranslator($this);
  65. // }
  66. // call the different public method
  67. $methods = array(
  68. 'getShow' => array(array()),
  69. 'getDatagrid' => array(array()),
  70. 'getList' => array(array()),
  71. 'getForm' => array(array()),
  72. 'getBreadcrumbs' => array(
  73. array('list'),
  74. array('edit'),
  75. array('create'),
  76. array('update'),
  77. array('batch'),
  78. array('delete'),
  79. )
  80. );
  81. if ($this->logger) {
  82. $this->logger->info(sprintf('Retrieving message from admin:%s - class: %s', $admin->getCode(), get_class($admin)));
  83. }
  84. foreach ($methods as $method => $calls) {
  85. foreach ($calls as $args) {
  86. try {
  87. call_user_func_array(array($admin, $method), $args);
  88. } catch (\Exception $e) {
  89. if ($this->logger) {
  90. $this->logger->error(sprintf('ERROR : admin:%s - Raise an exception : %s', $admin->getCode(), $e->getMessage()));
  91. }
  92. throw $e;
  93. }
  94. }
  95. }
  96. }
  97. $catalogue = $this->catalogue;
  98. $this->catalogue = false;
  99. return $catalogue;
  100. }
  101. /**
  102. * @param string $id
  103. *
  104. * @return \Sonata\AdminBundle\Admin\AdminInterface
  105. */
  106. private function getAdmin($id)
  107. {
  108. return $this->adminPool->getContainer()->get($id);
  109. }
  110. /**
  111. * @param string $id
  112. * @param string $domain
  113. */
  114. private function addMessage($id, $domain)
  115. {
  116. $message = new Message($id, $domain);
  117. // $this->logger->debug(sprintf('extract: %s - domain:%s', $id, $domain));
  118. $trace = debug_backtrace(false);
  119. if (isset($trace[1]['file'])) {
  120. $message->addSource(new FileSource($trace[1]['file']));
  121. }
  122. $this->catalogue->add($message);
  123. }
  124. /**
  125. * {@inheritDoc}
  126. */
  127. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  128. {
  129. $this->addMessage($id, $domain);
  130. return $id;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  136. {
  137. $this->addMessage($id, $domain);
  138. return $id;
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function setLocale($locale)
  144. {
  145. $this->translator->setLocale($locale);
  146. }
  147. /**
  148. * {@inheritDoc}
  149. */
  150. public function getLocale()
  151. {
  152. return $this->translator->getLocale();
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  158. {
  159. return true;
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public function buildSecurityInformation(AdminInterface $admin)
  165. {
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. public function createObjectSecurity(AdminInterface $admin, $object)
  171. {
  172. }
  173. /**
  174. * {@inheritDoc}
  175. */
  176. public function deleteObjectSecurity(AdminInterface $admin, $object)
  177. {
  178. }
  179. /**
  180. * {@inheritDoc}
  181. */
  182. public function getBaseRole(AdminInterface $admin)
  183. {
  184. }
  185. /**
  186. * {@inheritDoc}
  187. */
  188. public function getLabel($label, $context = '', $type = '')
  189. {
  190. $label = $this->labelStrategy->getLabel($label, $context, $type);
  191. $this->addMessage($label, $this->domain);
  192. return $label;
  193. }
  194. }