From 88423887aed0ab809f3e3504ccddca3708e0e83e Mon Sep 17 00:00:00 2001 From: Amos Folz <33728190+amosfolz@users.noreply.github.com> Date: Tue, 4 Jun 2019 23:47:53 +0000 Subject: [PATCH 1/3] Begin restructure --- .../src/Bakery/LocaleMissingKeysCommand.php | 116 +++++++++++------- .../src/Bakery/LocaleMissingValuesCommand.php | 6 +- 2 files changed, 77 insertions(+), 45 deletions(-) diff --git a/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php b/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php index d8173a9a3..48fef9471 100644 --- a/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php +++ b/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php @@ -12,85 +12,117 @@ use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use UserFrosting\System\Bakery\BaseCommand; class LocaleMissingKeysCommand extends BaseCommand { - protected $locales = [ - 'zh_CN', - 'es_ES', - 'ar', - 'pt_PT', - 'ru_RU', - 'de_DE', - 'fr_FR', - 'tr', - 'it_IT', - 'th_TH', - 'fa', - 'el', - ]; + protected $missing = []; protected function configure() { - // the name of the command (the part after "php bakery") - $this->setName('localeUtil:missing-keys'); + $this->setName('locale:missing-keys') + ->addOption('locale', 'l', InputOption::VALUE_REQUIRED, 'The locale to compare against.', 'en_US'); - // the short description shown while running "php bakery list" - $this->setDescription('Identifies missing locale keys by comparing en_US with other locale files.'); + $this->setDescription('Identify missing locale keys through comparison.'); } protected function execute(InputInterface $input, OutputInterface $output) { - $localesArray = $this->locales; + // The locale that other locales are compared to. Defaults to en_US if not set. + $baseLocale = $input->getOption('locale'); - $enUS = $this->getEnglishUS(); + $baseLocaleFileNames = $this->getBaseFileNames($baseLocale); + // print_r($baseLocaleFileNames); + $localesAvailable = $this->getAvailableLocales(); - foreach ($enUS as $filename => $path) { - $enUSFile = $this->fetchArray($path); + foreach ($localesAvailable as $altLocale) { + // print_r($altLocale); + $test[$altLocale] = $this->compareFiles($baseLocale, $altLocale, $baseLocaleFileNames); + } + //print_r($test); - foreach ($localesArray as $index => $localeDirectory) { - $altLocale = $this->fetchArray($this->ci->locator->getResource("locale://{$localeDirectory}/{$filename}")); + $difference[$localeDirectory][$path] = array_diff_key($baseFile, $altFile); - if (!empty(array_diff_key($enUSFile, $altLocale))) { - $difference[$localeDirectory][$path] = array_diff_key($enUSFile, $altLocale); + $table = new Table($output); - $table = new Table($output); + foreach ($difference[$localeDirectory][$path] as $k => $v) { + // print_r(gettype($k) . "\r\n"); + // print_r($v . "\r\n"); + $table->setHeaders([$localeDirectory, key($difference[$localeDirectory])]); + $table->addRow([$k]); + } + $table->render(); + } - foreach ($difference[$localeDirectory][$path] as $k => $v) { - $table->setHeaders([$localeDirectory, key($difference[$localeDirectory])]); - $table->addRow([$k]); + public function getDifference($array1, $array2) + { + foreach ($array1 as $key => $value) { + if (is_array($value)) { + if (!isset($array2[$key])) { + $difference[$key] = $key; + } elseif (!is_array($array2[$key])) { + $difference[$key] = $key; + } else { + $new_diff = $this->getDifference($value, $array2[$key]); + if ($new_diff != false) { + $difference[$key] = $new_diff; } - $table->render(); } + } elseif (!isset($array2[$key])) { + $difference[$key] = $key; } } + + return !isset($difference) ? 0 : $difference; } /** - * Access file contents through inclusion. + * Returns filenames and paths for a locale type. * - * @param string $path The path of file to be included. + * @param string The locale to get filenames and paths for. This should be a locale as listed in config['site']['locales']['available'] + * @return array Locale filenames and paths. */ - private function fetchArray($path) + private function compareFiles($baseLocale, $altLocale, $filenames) { - return include "$path"; + // print_r($filenames); + foreach ($filenames as $file) { + // print_r($file); + $base = $this->getFile($this->ci->locator->getResource("locale://{$baseLocale}/{$file}")); + + $alt = $this->getFile($this->ci->locator->getResource("locale://{$altLocale}/{$file}")); + + $difference[$file] = $this->getDifference($base, $alt); + } + + return $difference; } /** - * Returns filenames and paths for en_US locale files. + * Access file contents through inclusion. * - * @return array en_US locale filenames and paths. + * @param string $path The path of file to be included. */ - private function getEnglishUS() + private function getFile($path) { - $en = $this->ci->locator->listResources('locale://en_US'); + return include "$path"; + } - foreach ($en as $filename => $path) { - $enUS[$path->getBasename()] = $path->getAbsolutePath(); + private function getBaseFileNames($locale) + { + $file = ($this->ci->locator->listResources("locale://{$locale}", true)); + // print_r($file); + foreach ($file as $filename => $path) { + print_r($path->getLocatorBasePath()); + $files[$path->getAbsolutePath()] = $path->getBaseName(); } - return $enUS; + return $files; + } + + private function getAvailableLocales() + { + return array_keys($this->ci->config['site']['locales']['available']); } } diff --git a/app/sprinkles/core/src/Bakery/LocaleMissingValuesCommand.php b/app/sprinkles/core/src/Bakery/LocaleMissingValuesCommand.php index d3a559afb..d8177ef68 100644 --- a/app/sprinkles/core/src/Bakery/LocaleMissingValuesCommand.php +++ b/app/sprinkles/core/src/Bakery/LocaleMissingValuesCommand.php @@ -15,7 +15,7 @@ use Symfony\Component\Console\Output\OutputInterface; use UserFrosting\System\Bakery\BaseCommand; -class LocalMissingValuesCommand extends BaseCommand +class LocaleMissingValuesCommand extends BaseCommand { protected $locales = [ 'en_US', @@ -35,9 +35,9 @@ class LocalMissingValuesCommand extends BaseCommand protected function configure() { - $this->setName('localeUtil:missing-values'); + $this->setName('locale:missing-values'); // A missing value is equal to an empty string. - $this->setDescription('Identifies missing locale values by comparing en_US with other locale files.'); + $this->setDescription('Identify missing locale values through comparison.'); } protected function execute(InputInterface $input, OutputInterface $output) From f9f717068e75d91a5c7ddf3343df3539b03b1ab7 Mon Sep 17 00:00:00 2001 From: Amos Folz Date: Wed, 5 Jun 2019 03:40:09 +0000 Subject: [PATCH 2/3] Malou is not going to like this... --- .../src/Bakery/LocaleMissingKeysCommand.php | 63 +++++++++++++------ 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php b/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php index 48fef9471..014f30776 100644 --- a/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php +++ b/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php @@ -19,6 +19,7 @@ class LocaleMissingKeysCommand extends BaseCommand { protected $missing = []; + protected $table = []; protected function configure() { @@ -30,6 +31,8 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { + $this->$table = new Table($output); + // The locale that other locales are compared to. Defaults to en_US if not set. $baseLocale = $input->getOption('locale'); @@ -37,40 +40,61 @@ protected function execute(InputInterface $input, OutputInterface $output) // print_r($baseLocaleFileNames); $localesAvailable = $this->getAvailableLocales(); - foreach ($localesAvailable as $altLocale) { + foreach ($localesAvailable as $key => $altLocale) { // print_r($altLocale); - $test[$altLocale] = $this->compareFiles($baseLocale, $altLocale, $baseLocaleFileNames); + // $difference['base_locale'] = $baseLocale; + $difference[] = $this->compareFiles($baseLocale, $altLocale, $baseLocaleFileNames); } - //print_r($test); + //print_r($difference); - $difference[$localeDirectory][$path] = array_diff_key($baseFile, $altFile); + $this->$table->setHeaders([$baseLocale, null]); - $table = new Table($output); + return $this->buildTable($difference); + } - foreach ($difference[$localeDirectory][$path] as $k => $v) { - // print_r(gettype($k) . "\r\n"); - // print_r($v . "\r\n"); - $table->setHeaders([$localeDirectory, key($difference[$localeDirectory])]); - $table->addRow([$k]); + public function buildTable($difference) + { + // print_r($difference); + + foreach ($difference as $key => $value) { + { + foreach ($value as $k => $v) { + if (!is_array($v) && $v != '0') { + $this->$table->addRow([$v]); + } else { + foreach ($v as $a => $b) { + if (!is_array($b) && $b != '0') { + $this->$table->addRow([$k, $b]); + } + } + } + } + } } - $table->render(); + + return $this->$table->render(); } public function getDifference($array1, $array2) { foreach ($array1 as $key => $value) { if (is_array($value)) { + // print_r("1:\r\n" . $value); if (!isset($array2[$key])) { $difference[$key] = $key; + // print_r('Number 1 triggered:' . $key . "\r\n"); } elseif (!is_array($array2[$key])) { $difference[$key] = $key; + // print_r('Number 2 triggered:' . $key . "\r\n"); } else { $new_diff = $this->getDifference($value, $array2[$key]); if ($new_diff != false) { + // print_r('Number 3 triggered:' . $key . "\r\n"); $difference[$key] = $new_diff; } } } elseif (!isset($array2[$key])) { + // print_r('Number 4 triggered:' . $key . "\r\n"); $difference[$key] = $key; } } @@ -86,14 +110,16 @@ public function getDifference($array1, $array2) */ private function compareFiles($baseLocale, $altLocale, $filenames) { - // print_r($filenames); - foreach ($filenames as $file) { - // print_r($file); - $base = $this->getFile($this->ci->locator->getResource("locale://{$baseLocale}/{$file}")); + // print_r($filenames); + foreach ($filenames as $sprinkle => $files) { + foreach ($files as $key => $file) { + // print_r($sprinkle . "\r\n"); + $base = $this->getFile($this->ci->locator->getResource("locale://{$baseLocale}/{$file}")); - $alt = $this->getFile($this->ci->locator->getResource("locale://{$altLocale}/{$file}")); + $alt = $this->getFile($this->ci->locator->getResource("locale://{$altLocale}/{$file}")); - $difference[$file] = $this->getDifference($base, $alt); + $difference[$sprinkle . '/locale' . '/' . $altLocale . '/' . $file] = $this->getDifference($base, $alt); + } } return $difference; @@ -114,8 +140,7 @@ private function getBaseFileNames($locale) $file = ($this->ci->locator->listResources("locale://{$locale}", true)); // print_r($file); foreach ($file as $filename => $path) { - print_r($path->getLocatorBasePath()); - $files[$path->getAbsolutePath()] = $path->getBaseName(); + $files[$path->getLocation()->getName()][] = $path->getBaseName(); } return $files; From 5ed640dcfe950311a340815dfe66beb51526c0ba Mon Sep 17 00:00:00 2001 From: Amos Folz <33728190+amosfolz@users.noreply.github.com> Date: Wed, 5 Jun 2019 15:17:17 +0000 Subject: [PATCH 3/3] try to separate out LocaleMissingKeysCommand --- .../src/Bakery/LocaleMissingKeysCommand.php | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php b/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php index 014f30776..f7f9deae5 100644 --- a/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php +++ b/app/sprinkles/core/src/Bakery/LocaleMissingKeysCommand.php @@ -11,6 +11,8 @@ namespace UserFrosting\Sprinkle\Core\Bakery; use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Helper\TableCell; +use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -19,12 +21,16 @@ class LocaleMissingKeysCommand extends BaseCommand { protected $missing = []; + protected $table = []; + protected $auxLocale; + protected function configure() { $this->setName('locale:missing-keys') - ->addOption('locale', 'l', InputOption::VALUE_REQUIRED, 'The locale to compare against.', 'en_US'); + ->addOption('base', 'b', InputOption::VALUE_REQUIRED, 'The base locale to compare against.', 'en_US') + ->addOption('compare', 'c', InputOption::VALUE_REQUIRED, 'A optional second locale to compare against', null); $this->setDescription('Identify missing locale keys through comparison.'); } @@ -34,28 +40,27 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->$table = new Table($output); // The locale that other locales are compared to. Defaults to en_US if not set. - $baseLocale = $input->getOption('locale'); + $baseLocale = $input->getOption('base'); + + $this->auxLocale = $input->getOption('compare'); + //print_r($this->auxLocale = $input->getOption('compare')); $baseLocaleFileNames = $this->getBaseFileNames($baseLocale); - // print_r($baseLocaleFileNames); + $localesAvailable = $this->getAvailableLocales(); foreach ($localesAvailable as $key => $altLocale) { - // print_r($altLocale); - // $difference['base_locale'] = $baseLocale; $difference[] = $this->compareFiles($baseLocale, $altLocale, $baseLocaleFileNames); } - //print_r($difference); - $this->$table->setHeaders([$baseLocale, null]); + $this->$table->setHeaders([new TableCell('COMPARING AGAINST: ' . $baseLocale, ['colspan' => 2])]); + $this->$table->addRows([['FILE PATH', 'MISSING KEY'], new TableSeparator()]); return $this->buildTable($difference); } public function buildTable($difference) { - // print_r($difference); - foreach ($difference as $key => $value) { { foreach ($value as $k => $v) { @@ -79,22 +84,17 @@ public function getDifference($array1, $array2) { foreach ($array1 as $key => $value) { if (is_array($value)) { - // print_r("1:\r\n" . $value); if (!isset($array2[$key])) { $difference[$key] = $key; - // print_r('Number 1 triggered:' . $key . "\r\n"); } elseif (!is_array($array2[$key])) { $difference[$key] = $key; - // print_r('Number 2 triggered:' . $key . "\r\n"); } else { $new_diff = $this->getDifference($value, $array2[$key]); if ($new_diff != false) { - // print_r('Number 3 triggered:' . $key . "\r\n"); $difference[$key] = $new_diff; } } } elseif (!isset($array2[$key])) { - // print_r('Number 4 triggered:' . $key . "\r\n"); $difference[$key] = $key; } } @@ -106,18 +106,14 @@ public function getDifference($array1, $array2) * Returns filenames and paths for a locale type. * * @param string The locale to get filenames and paths for. This should be a locale as listed in config['site']['locales']['available'] - * @return array Locale filenames and paths. + * @return array */ private function compareFiles($baseLocale, $altLocale, $filenames) { - // print_r($filenames); foreach ($filenames as $sprinkle => $files) { foreach ($files as $key => $file) { - // print_r($sprinkle . "\r\n"); $base = $this->getFile($this->ci->locator->getResource("locale://{$baseLocale}/{$file}")); - $alt = $this->getFile($this->ci->locator->getResource("locale://{$altLocale}/{$file}")); - $difference[$sprinkle . '/locale' . '/' . $altLocale . '/' . $file] = $this->getDifference($base, $alt); } } @@ -135,10 +131,15 @@ private function getFile($path) return include "$path"; } + /** + * Gets all locale files for a specific locale. + * + * @param string $locale The locale to get files for. + * @return array Locale files per sprinkle. + */ private function getBaseFileNames($locale) { $file = ($this->ci->locator->listResources("locale://{$locale}", true)); - // print_r($file); foreach ($file as $filename => $path) { $files[$path->getLocation()->getName()][] = $path->getBaseName(); } @@ -148,6 +149,10 @@ private function getBaseFileNames($locale) private function getAvailableLocales() { - return array_keys($this->ci->config['site']['locales']['available']); + if ($this->auxLocale) { + return [$this->auxLocale]; + } else { + return array_keys($this->ci->config['site']['locales']['available']); + } } }