AdminExtractor.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 Psr\Log\LoggerInterface;
  16. use Sonata\AdminBundle\Admin\AdminInterface;
  17. use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
  18. use Sonata\AdminBundle\Admin\Pool;
  19. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  20. use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
  21. use Symfony\Component\Translation\TranslatorInterface;
  22. class AdminExtractor implements ExtractorInterface, TranslatorInterface, SecurityHandlerInterface, LabelTranslatorStrategyInterface
  23. {
  24. /**
  25. * @var LoggerInterface
  26. */
  27. private $logger;
  28. /**
  29. * @var Pool
  30. */
  31. private $adminPool;
  32. /**
  33. * @var string|bool
  34. */
  35. private $catalogue;
  36. /**
  37. * @var string|bool
  38. */
  39. private $translator;
  40. /**
  41. * @var string|bool
  42. */
  43. private $labelStrategy;
  44. /**
  45. * @var string|bool
  46. */
  47. private $domain;
  48. /**
  49. * @var BreadcrumbsBuilderInterface
  50. */
  51. private $breadcrumbsBuilder;
  52. /**
  53. * @param Pool $adminPool
  54. * @param LoggerInterface $logger
  55. */
  56. public function __construct(Pool $adminPool, LoggerInterface $logger = null)
  57. {
  58. $this->logger = $logger;
  59. $this->adminPool = $adminPool;
  60. // state variable
  61. $this->catalogue = false;
  62. $this->translator = false;
  63. $this->labelStrategy = false;
  64. $this->domain = false;
  65. }
  66. /**
  67. * @param LoggerInterface $logger
  68. */
  69. public function setLogger(LoggerInterface $logger)
  70. {
  71. $this->logger = $logger;
  72. }
  73. /**
  74. * NEXT_MAJOR : use a constructor argument instead.
  75. */
  76. final public function setBreadcrumbsBuilder(BreadcrumbsBuilderInterface $breadcrumbsBuilder)
  77. {
  78. $this->breadcrumbsBuilder = $breadcrumbsBuilder;
  79. }
  80. /**
  81. * Extract messages to MessageCatalogue.
  82. *
  83. * @return MessageCatalogue
  84. *
  85. * @throws \Exception|\RuntimeException
  86. */
  87. public function extract()
  88. {
  89. if ($this->catalogue) {
  90. throw new \RuntimeException('Invalid state');
  91. }
  92. $this->catalogue = new MessageCatalogue();
  93. foreach ($this->adminPool->getAdminServiceIds() as $id) {
  94. $admin = $this->getAdmin($id);
  95. $this->translator = $admin->getTranslator();
  96. $this->labelStrategy = $admin->getLabelTranslatorStrategy();
  97. $this->domain = $admin->getTranslationDomain();
  98. $admin->setTranslator($this);
  99. $admin->setSecurityHandler($this);
  100. $admin->setLabelTranslatorStrategy($this);
  101. // foreach ($admin->getChildren() as $child) {
  102. // $child->setTranslator($this);
  103. // }
  104. // call the different public method
  105. $methods = array(
  106. 'getShow',
  107. 'getDatagrid',
  108. 'getList',
  109. 'getForm',
  110. );
  111. $actions = array(
  112. 'list',
  113. 'edit',
  114. 'create',
  115. 'update',
  116. 'batch',
  117. 'delete',
  118. );
  119. if ($this->logger) {
  120. $this->logger->info(sprintf('Retrieving message from admin:%s - class: %s', $admin->getCode(), get_class($admin)));
  121. }
  122. foreach ($methods as $method) {
  123. try {
  124. $admin->$method();
  125. } catch (\Exception $e) {
  126. if ($this->logger) {
  127. $this->logger->error(sprintf('ERROR : admin:%s - Raise an exception : %s', $admin->getCode(), $e->getMessage()));
  128. }
  129. throw $e;
  130. }
  131. }
  132. foreach ($actions as $action) {
  133. try {
  134. $this->breadcrumbsBuilder->getBreadcrumbs($admin, $action);
  135. } catch (\Exception $e) {
  136. if ($this->logger) {
  137. $this->logger->error(
  138. sprintf(
  139. 'ERROR : admin:%s - Raises an exception : %s',
  140. $admin->getCode(),
  141. $e->getMessage()
  142. ),
  143. array('exception' => $e)
  144. );
  145. }
  146. throw $e;
  147. }
  148. }
  149. }
  150. $catalogue = $this->catalogue;
  151. $this->catalogue = false;
  152. return $catalogue;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  158. {
  159. $this->addMessage($id, $domain);
  160. return $id;
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  166. {
  167. $this->addMessage($id, $domain);
  168. return $id;
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function setLocale($locale)
  174. {
  175. $this->translator->setLocale($locale);
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function getLocale()
  181. {
  182. return $this->translator->getLocale();
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  188. {
  189. return true;
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function buildSecurityInformation(AdminInterface $admin)
  195. {
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function createObjectSecurity(AdminInterface $admin, $object)
  201. {
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function deleteObjectSecurity(AdminInterface $admin, $object)
  207. {
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getBaseRole(AdminInterface $admin)
  213. {
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function getLabel($label, $context = '', $type = '')
  219. {
  220. $label = $this->labelStrategy->getLabel($label, $context, $type);
  221. $this->addMessage($label, $this->domain);
  222. return $label;
  223. }
  224. /**
  225. * @param string $id
  226. *
  227. * @return AdminInterface
  228. */
  229. private function getAdmin($id)
  230. {
  231. return $this->adminPool->getContainer()->get($id);
  232. }
  233. /**
  234. * @param string $id
  235. * @param string $domain
  236. */
  237. private function addMessage($id, $domain)
  238. {
  239. $message = new Message($id, $domain);
  240. // $this->logger->debug(sprintf('extract: %s - domain:%s', $id, $domain));
  241. $trace = debug_backtrace(false);
  242. if (isset($trace[1]['file'])) {
  243. $message->addSource(new FileSource($trace[1]['file']));
  244. }
  245. $this->catalogue->add($message);
  246. }
  247. }