Files
chill-bundles/src/Bundle/ChillMainBundle/Command/LoadAndUpdateLanguagesCommand.php
2025-10-30 01:20:52 +01:00

103 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Command;
use Chill\MainBundle\Entity\Language;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Intl\Languages;
/*
* Load or update the languages entities command
*/
#[\Symfony\Component\Console\Attribute\AsCommand(name: 'chill:main:languages:populate')]
class LoadAndUpdateLanguagesCommand
{
final public const INCLUDE_ANCIENT = 'include_ancient';
final public const INCLUDE_REGIONAL_VERSION = 'include_regional';
// Array of ancien languages (to exclude)
private array $ancientToExclude = ['ang', 'egy', 'fro', 'goh', 'grc', 'la', 'non', 'peo', 'pro', 'sga',
'dum', 'enm', 'frm', 'gmh', 'mga', 'akk', 'phn', 'zxx', 'got', 'und', ];
// The regional version of language are language with _ in the code
// This array contains regional code to not exclude
private array $regionalVersionToInclude = ['ro_MD'];
/**
* LoadCountriesCommand constructor.
*/
public function __construct(private readonly EntityManagerInterface $entityManager, private readonly ParameterBagInterface $parameterBag)
{
}
/**
* (non-PHPdoc).
*
* @see \Symfony\Component\Console\Command\Command::execute()
*/
public function __invoke(
#[\Symfony\Component\Console\Attribute\Option(name: self::INCLUDE_REGIONAL_VERSION, mode: InputOption::VALUE_NONE, description: 'Include the regional languages. The regional languages are languages with code containing')]
bool $includeRegional = false,
#[\Symfony\Component\Console\Attribute\Option(name: self::INCLUDE_ANCIENT, mode: InputOption::VALUE_NONE, description: 'Include the ancient languages that are languages with code ')]
bool $includeAncient = false): int
{
$em = $this->entityManager;
$chillAvailableLanguages = $this->parameterBag->get('chill_main.available_languages');
$languages = [];
foreach ($chillAvailableLanguages as $avLang) {
$languages[$avLang] = Languages::getNames();
}
foreach (Languages::getNames() as $code => $lang) {
$excludeCode = (
(
null === $include_regional
&& strpos($code, '_')
&& !\in_array($code, $this->regionalVersionToInclude, true)
) || (
null === $include_ancient
&& \in_array($code, $this->ancientToExclude, true)
)
);
if (true === $excludeCode) {
continue;
}
$languageDB = $em->getRepository(Language::class)->find($code);
if (null === $languageDB) {
$languageDB = new Language();
$languageDB->setId($code);
$em->persist($languageDB);
}
$avLangNames = [];
foreach ($chillAvailableLanguages as $avLang) {
$avLangNames[$avLang] = ucfirst(Languages::getName($code, $avLang));
}
$languageDB->setName($avLangNames);
}
$em->flush();
return Command::SUCCESS;
}
}