| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHP Translation package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) PHP Translation team <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Translation\SymfonyStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Translation\MessageCatalogue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Translation\MessageCatalogueInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Translation\Reader\TranslationReaderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Translation\Writer\TranslationWriterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Translation\Common\Model\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Translation\Common\Model\MessageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Translation\Common\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Translation\Common\TransferableStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * This storage uses Symfony's writer and loader. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author Tobias Nyholm <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | final class FileStorage implements Storage, TransferableStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var TranslationWriterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var TranslationReaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $reader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var array directory path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private $dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @var array with option to the dumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @var MessageCatalogue[] Fetched catalogues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private $catalogues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 9 |  |     public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, array $dir, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 9 |  |         if (empty($dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |             throw new \LogicException('Third parameter of FileStorage cannot be empty'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 8 |  |         if (!\array_key_exists('xliff_version', $options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             // Set default value for xliff version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 8 |  |             $options['xliff_version'] = '2.0'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 8 |  |         $this->writer = $writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 8 |  |         $this->reader = $reader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 8 |  |         $this->dir = $dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 8 |  |         $this->options = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |     public function get(string $locale, string $domain, string $key): ?MessageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         $catalogue = $this->getCatalogue($locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         $translation = $catalogue->get($key, $domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         return new Message($key, $domain, $locale, $translation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |     public function create(MessageInterface $m): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |         $catalogue = $this->getCatalogue($m->getLocale()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |         if (!$catalogue->defines($m->getKey(), $m->getDomain())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |             $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |             $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |     public function update(MessageInterface $m): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |         $catalogue = $this->getCatalogue($m->getLocale()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |         $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |     public function delete(string $locale, string $domain, string $key): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         $catalogue = $this->getCatalogue($locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         $messages = $catalogue->all($domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         unset($messages[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |         $catalogue->replace($messages, $domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         $this->writeCatalogue($catalogue, $locale, $domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 1 |  |     public function export(MessageCatalogueInterface $catalogue, array $options = []): void | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 1 |  |         $locale = $catalogue->getLocale(); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 1 |  |         $catalogue->addCatalogue($this->getCatalogue($locale)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |     public function import(MessageCatalogueInterface $catalogue, array $options = []): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |         $domains = $catalogue->getDomains(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |         foreach ($domains as $domain) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |             $this->writeCatalogue($catalogue, $catalogue->getLocale(), $domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 5 |  |     private function writeCatalogue(MessageCatalogueInterface $catalogue, string $locale, string $domain): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 5 |  |         $resources = $catalogue->getResources(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 5 |  |         $options = $this->options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 5 |  |         $written = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         // $intlDomainSuffix = '(\\' . MessageCatalogueInterface::INTL_DOMAIN_SUFFIX . ')'; # not available in older Symfony versions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 5 |  |         $intlDomainSuffix = '(\\'.'+intl-icu'.')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 5 |  |         $searchPatternWithIntl = '|/'.$domain.$intlDomainSuffix.'\.'.$locale.'\.([a-z]+)$|'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 5 |  |         $searchPatternWithoutIntl = str_replace($intlDomainSuffix, '', $searchPatternWithIntl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 5 |  |         foreach ($resources as $resource) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 3 |  |             $path = (string) $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 3 |  |             if (preg_match($searchPatternWithIntl, $path, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 $options['path'] = str_replace($matches[0], '', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 $this->writer->write($catalogue, $matches[2], $options); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 $written = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 3 |  |             } elseif (preg_match($searchPatternWithoutIntl, $path, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 3 |  |                 $options['path'] = str_replace($matches[0], '', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 3 |  |                 $this->writer->write($catalogue, $matches[1], $options); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 3 |  |                 $written = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 5 |  |         if ($written) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             // We have written the translation to a file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 3 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 2 |  |         $options['path'] = reset($this->dir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 2 |  |         $format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 2 |  |         $this->writer->write($catalogue, $format, $options); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 6 |  |     private function getCatalogue(string $locale): MessageCatalogue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 6 |  |         if (empty($this->catalogues[$locale])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 6 |  |             $this->loadCatalogue($locale, $this->dir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 6 |  |         return $this->catalogues[$locale]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 6 |  |     private function loadCatalogue(string $locale, array $dirs): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 6 |  |         $currentCatalogue = new MessageCatalogue($locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 6 |  |         foreach ($dirs as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 6 |  |             if (is_dir($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 5 |  |                 $this->reader->read($path, $currentCatalogue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 6 |  |         $this->catalogues[$locale] = $currentCatalogue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 6 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  | 
            
                        
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: