/**
  * {@inheritdoc}
  */
 public function beforeCompile(CompilationContextInterface $context)
 {
     // The language data is contained in the locales bundle in ICU <= 4.2
     if (IcuVersion::compare($context->getIcuVersion(), '4.2', '<=', 1)) {
         return $context->getSourceDir() . '/locales';
     }
     return $context->getSourceDir() . '/lang';
 }
 /**
  * {@inheritdoc}
  */
 public function afterCompile(CompilationContextInterface $context)
 {
     // \ResourceBundle does not like locale names with uppercase chars, so rename
     // the resource file
     // See: http://bugs.php.net/bug.php?id=54025
     $fileName = $context->getBinaryDir() . '/curr/supplementalData.res';
     $fileNameLower = $context->getBinaryDir() . '/curr/supplementaldata.res';
     $context->getFilesystem()->rename($fileName, $fileNameLower);
 }
 private function scanLocales(CompilationContextInterface $context)
 {
     $tempDir = sys_get_temp_dir() . '/icu-data-locales-source';
     $context->getFilesystem()->remove($tempDir);
     $context->getFilesystem()->mkdir($tempDir);
     // Temporarily generate the resource bundles
     $context->getCompiler()->compile($context->getSourceDir() . '/locales', $tempDir);
     // Discover the list of supported locales, which are the names of the resource
     // bundles in the "locales" directory
     $locales = glob($tempDir . '/*.res');
     // Remove file extension and sort
     array_walk($locales, function (&$locale) {
         $locale = basename($locale, '.res');
     });
     sort($locales);
     // Delete unneeded locales
     foreach ($locales as $key => $locale) {
         // Delete all aliases from the list
         // i.e., "az_AZ" is an alias for "az_Latn_AZ"
         $content = file_get_contents($context->getSourceDir() . '/locales/' . $locale . '.txt');
         // The key "%%ALIAS" is not accessible through the \ResourceBundle class,
         // so look in the original .txt file instead
         if (strpos($content, '%%ALIAS') !== false) {
             unset($locales[$key]);
         }
         // Delete locales that have no content (i.e. only "Version" key)
         try {
             $bundle = new \ResourceBundle($locale, $tempDir);
         } catch (\Exception $e) {
             // HHVM compatibility: constructor throws on invalid resource
             $bundle = null;
         }
         if (null === $bundle) {
             throw new RuntimeException('The resource bundle for locale ' . $locale . ' could not be loaded from directory ' . $tempDir);
         }
         // There seems to be no other way for identifying all keys in this specific
         // resource bundle
         if (array_keys(iterator_to_array($bundle)) === array('Version')) {
             unset($locales[$key]);
         }
     }
     $context->getFilesystem()->remove($tempDir);
     return $locales;
 }
 /**
  * Runs the compilation with the given compilation context.
  *
  * @param CompilationContextInterface $context The context storing information
  *                                             needed to run the compilation.
  *
  * @throws RuntimeException If any of the files to be compiled by the loaded
  *                          compilation rules does not exist.
  */
 public function compileBundles(CompilationContextInterface $context)
 {
     $filesystem = $context->getFilesystem();
     $compiler = $context->getCompiler();
     $filesystem->remove($context->getBinaryDir());
     $filesystem->mkdir($context->getBinaryDir());
     foreach ($this->rules as $rule) {
         $filesystem->mkdir($context->getBinaryDir() . '/' . $rule->getBundleName());
         $resources = (array) $rule->beforeCompile($context);
         foreach ($resources as $resource) {
             if (!file_exists($resource)) {
                 throw new RuntimeException(sprintf('The file "%s" to be compiled by %s does not exist.', $resource, get_class($rule)));
             }
             $compiler->compile($resource, $context->getBinaryDir() . '/' . $rule->getBundleName());
         }
         $rule->afterCompile($context);
     }
 }