835433c501772550728789d5639e0ac19ec6a365
[yaffs-website] / vendor / drupal / console / src / Utils / TranslatorManager.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\TranslatorManager.
6  */
7
8 namespace Drupal\Console\Utils;
9
10 use Drupal\Console\Core\Utils\TranslatorManager as TranslatorManagerBase;
11 use Symfony\Component\Yaml\Exception\ParseException;
12 use Symfony\Component\Finder\Finder;
13
14 /**
15  * Class TranslatorManager
16  *
17  * @package Drupal\Console\Utils
18  */
19 class TranslatorManager extends TranslatorManagerBase
20 {
21     protected $extensions = [];
22
23     /**
24      * @param $extensionPath
25      */
26     private function addResourceTranslationsByExtensionPath($extensionPath)
27     {
28         $languageDirectory = sprintf(
29             '%s/console/translations/%s',
30             $extensionPath,
31             $this->language
32         );
33
34         if (!is_dir($languageDirectory)) {
35             return;
36         }
37         $finder = new Finder();
38         $finder->files()
39             ->name('*.yml')
40             ->in($languageDirectory);
41         foreach ($finder as $file) {
42             $resource = $languageDirectory . '/' . $file->getBasename();
43             $filename = $file->getBasename('.yml');
44             $key = 'commands.' . $filename;
45             try {
46                 $this->loadTranslationByFile($resource, $key);
47             } catch (ParseException $e) {
48                 echo $key . '.yml ' . $e->getMessage();
49             }
50         }
51     }
52
53     /**
54      * @param $module
55      */
56     private function addResourceTranslationsByModule($module)
57     {
58         if (!\Drupal::moduleHandler()->moduleExists($module)) {
59             return;
60         }
61         $extensionPath = \Drupal::moduleHandler()->getModule($module)->getPath();
62         $this->addResourceTranslationsByExtensionPath(
63             $extensionPath
64         );
65     }
66
67     /**
68      * @param $theme
69      */
70     private function addResourceTranslationsByTheme($theme)
71     {
72         $extensionPath = \Drupal::service('theme_handler')->getTheme($theme)->getPath();
73         $this->addResourceTranslationsByExtensionPath(
74             $extensionPath
75         );
76     }
77
78     /**
79      * @param $library
80      */
81     private function addResourceTranslationsByLibrary($library)
82     {
83         /** @var \Drupal\Console\Core\Utils\DrupalFinder $drupalFinder */
84         $drupalFinder = \Drupal::service('console.drupal_finder');
85         $path =  $drupalFinder->getComposerRoot() . '/vendor/' . $library;
86         $this->addResourceTranslationsByExtensionPath(
87             $path
88         );
89     }
90
91     /**
92      * @param $extension
93      * @param $type
94      */
95     public function addResourceTranslationsByExtension($extension, $type)
96     {
97         if (array_search($extension, $this->extensions) !== false) {
98             return;
99         }
100
101         $this->extensions[] = $extension;
102         if ($type == 'module') {
103             $this->addResourceTranslationsByModule($extension);
104             return;
105         }
106         if ($type == 'theme') {
107             $this->addResourceTranslationsByTheme($extension);
108             return;
109         }
110         if ($type == 'library') {
111             $this->addResourceTranslationsByLibrary($extension);
112             return;
113         }
114     }
115 }