AdminExtractor.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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->getAdminGroups() as $name => $group) {
  94. $this->trans($name, array(), $group['label_catalogue']);
  95. }
  96. foreach ($this->adminPool->getAdminServiceIds() as $id) {
  97. $admin = $this->getAdmin($id);
  98. $label = $admin->getLabel();
  99. if (!empty($label)) {
  100. $this->trans($label, array(), $admin->getTranslationDomain());
  101. }
  102. $this->translator = $admin->getTranslator();
  103. $this->labelStrategy = $admin->getLabelTranslatorStrategy();
  104. $this->domain = $admin->getTranslationDomain();
  105. $admin->setTranslator($this);
  106. $admin->setSecurityHandler($this);
  107. $admin->setLabelTranslatorStrategy($this);
  108. // foreach ($admin->getChildren() as $child) {
  109. // $child->setTranslator($this);
  110. // }
  111. // call the different public method
  112. $methods = array(
  113. 'getShow',
  114. 'getDatagrid',
  115. 'getList',
  116. 'getForm',
  117. );
  118. $actions = array(
  119. 'list',
  120. 'edit',
  121. 'create',
  122. 'update',
  123. 'batch',
  124. 'delete',
  125. );
  126. if ($this->logger) {
  127. $this->logger->info(sprintf('Retrieving message from admin:%s - class: %s', $admin->getCode(), get_class($admin)));
  128. }
  129. foreach ($methods as $method) {
  130. try {
  131. $admin->$method();
  132. } catch (\Exception $e) {
  133. if ($this->logger) {
  134. $this->logger->error(sprintf('ERROR : admin:%s - Raise an exception : %s', $admin->getCode(), $e->getMessage()));
  135. }
  136. throw $e;
  137. }
  138. }
  139. foreach ($actions as $action) {
  140. try {
  141. $this->breadcrumbsBuilder->getBreadcrumbs($admin, $action);
  142. } catch (\Exception $e) {
  143. if ($this->logger) {
  144. $this->logger->error(
  145. sprintf(
  146. 'ERROR : admin:%s - Raises an exception : %s',
  147. $admin->getCode(),
  148. $e->getMessage()
  149. ),
  150. array('exception' => $e)
  151. );
  152. }
  153. throw $e;
  154. }
  155. }
  156. }
  157. $catalogue = $this->catalogue;
  158. $this->catalogue = false;
  159. return $catalogue;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  165. {
  166. $this->addMessage($id, $domain);
  167. return $id;
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  173. {
  174. $this->addMessage($id, $domain);
  175. return $id;
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function setLocale($locale)
  181. {
  182. $this->translator->setLocale($locale);
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function getLocale()
  188. {
  189. return $this->translator->getLocale();
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  195. {
  196. return true;
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function buildSecurityInformation(AdminInterface $admin)
  202. {
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function createObjectSecurity(AdminInterface $admin, $object)
  208. {
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function deleteObjectSecurity(AdminInterface $admin, $object)
  214. {
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function getBaseRole(AdminInterface $admin)
  220. {
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function getLabel($label, $context = '', $type = '')
  226. {
  227. $label = $this->labelStrategy->getLabel($label, $context, $type);
  228. $this->addMessage($label, $this->domain);
  229. return $label;
  230. }
  231. /**
  232. * @param string $id
  233. *
  234. * @return AdminInterface
  235. */
  236. private function getAdmin($id)
  237. {
  238. return $this->adminPool->getContainer()->get($id);
  239. }
  240. /**
  241. * @param string $id
  242. * @param string $domain
  243. */
  244. private function addMessage($id, $domain)
  245. {
  246. $message = new Message($id, $domain);
  247. // $this->logger->debug(sprintf('extract: %s - domain:%s', $id, $domain));
  248. $trace = debug_backtrace(false);
  249. if (isset($trace[1]['file'])) {
  250. $message->addSource(new FileSource($trace[1]['file']));
  251. }
  252. $this->catalogue->add($message);
  253. }
  254. }