1313
1414use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
1515use Symfony \Component \Console \Input \InputArgument ;
16+ use Symfony \Component \Console \Input \InputOption ;
1617use Symfony \Component \Console \Input \InputInterface ;
1718use Symfony \Component \Console \Output \OutputInterface ;
19+ use Symfony \Component \Finder \Finder ;
1820use Translation \Bundle \Service \StorageService ;
21+ use Translation \Bundle \Model \Configuration ;
1922
2023/**
2124 * @author Tobias Nyholm <[email protected] > @@ -27,15 +30,50 @@ protected function configure()
2730 $ this
2831 ->setName ('translation:download ' )
2932 ->setDescription ('Replace local messages with messages from remote ' )
30- ->addArgument ('configuration ' , InputArgument::OPTIONAL , 'The configuration to use ' , 'default ' );
33+ ->addArgument ('configuration ' , InputArgument::OPTIONAL , 'The configuration to use ' , 'default ' )
34+ ->addOption ('cache ' , null , InputOption::VALUE_NONE , 'Clear the cache if the translations have changed ' )
35+ ;
3136 }
3237
3338 protected function execute (InputInterface $ input , OutputInterface $ output )
3439 {
3540 $ container = $ this ->getContainer ();
3641 $ configName = $ input ->getArgument ('configuration ' );
42+
3743 /** @var StorageService $storage */
3844 $ storage = $ container ->get ('php_translation.storage. ' .$ configName );
39- $ storage ->download ();
45+ /** @var Configuration $configuration */
46+ $ configuration = $ this ->getContainer ()->get ('php_translation.configuration. ' .$ configName );
47+
48+ if ($ input ->getOption ('cache ' )) {
49+ $ translationsDirectory = $ configuration ->getOutputDir ();
50+ $ md5BeforeDownload = $ this ->hashDirectory ($ translationsDirectory );
51+ $ storage ->download ();
52+ $ md5AfterDownload = $ this ->hashDirectory ($ translationsDirectory );
53+
54+ if ($ md5BeforeDownload !== $ md5AfterDownload ) {
55+ $ cacheClearer = $ this ->getContainer ()->get ('php_translation.cache_clearer ' );
56+ $ cacheClearer ->clearAndWarmUp ();
57+ }
58+ } else {
59+ $ storage ->download ();
60+ }
61+ }
62+
63+ private function hashDirectory ($ directory )
64+ {
65+ if (!is_dir ($ directory )) {
66+ return false ;
67+ }
68+
69+ $ finder = new Finder ();
70+ $ finder ->files ()->in ($ directory )->notName ('/~$/ ' )->sortByName ();
71+
72+ $ hash = hash_init ('md5 ' );
73+ foreach ($ finder as $ file ) {
74+ hash_update_file ($ hash , $ file ->getRealPath ());
75+ }
76+
77+ return hash_final ($ hash );
4078 }
4179}
0 commit comments