AdminExtractor.php 6.3 KB

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