Source of file ImportListCommand.php
Size: 4,852 Bytes - Last Modified: 2023-11-16T22:56:03+01:00
/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/list-bundle/src/Command/ImportListCommand.php
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 | <?phpnamespace TIC\ListBundle\Command; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Exception\NotSupported; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Yaml\Yaml; use TIC\CoreBundle\Base\TICCommand as BaseCommand; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use TIC\ListBundle\Entity\ListInfo; use TIC\ListBundle\Entity\ListItem; /** * Commande d'importation de liste administrable (à partir de fichiers YAML ou JSON). */class ImportListCommand extends BaseCommand { use \TIC\DormBundle\Traits\ManagerTrait; /** * {@inheritdoc} */protected function configure(): void { parent::configure(); $this ->setDescription("Commande d'importation de liste administrable (à partir de fichiers YAML ou JSON).") ->addArgument('path', InputArgument::REQUIRED, "Fichier ou dossier à charger") ->addOption('empty', null, InputOption::VALUE_NONE, "Vide les éléments d'une liste existante") ->addOption('update', null, InputOption::VALUE_NONE, "Modification des éléments déjà existant") ; } /** * {@inheritdoc} * @throws NotSupported */protected function execute(InputInterface $input, OutputInterface $output): int { $source = $input->getArgument('path'); if (! file_exists($source)) { $this->io->error("File or folder '$source' does not exists!"); exit; } // traitement des fichiers INI indiqués foreach ($this->getSources($source) as $path => $format) { // lecture des données à importer depuis le fichier switch ($format) { case 'json': $data = json_decode(file_get_contents($path), true); break; case 'yaml': $data = Yaml::parse(file_get_contents($path)); break; default : $data = null; } if (! is_array($data)) { $this->io->error("File '$path' does not contain list informations!"); continue; } // analyse du contenu des données importées if (isset($data['ref'])) $ref = $data['ref']; $nbItems = isset($data['items']) ? count($data['items']) : null; $this->io->section("Importing list: $ref (" . (($nbItems === null) ? 'no' : $nbItems) . " elements)"); // récupération ou création de la liste $list = $this->getRepo('ListInfo')->find($ref); if ($list) { $this->io->note("List '$ref' already exists."); } else { $list = new ListInfo(0, $ref); $this->io->success("List '$ref' created."); } // si la liste existe et contient déjà des noeuds... if ($nbItems !== null && $list->countItems()) { // suppression des nœuds existant (avec l'option "empty") if ($input->getOption('empty')) { $this->io->warning("List '$ref' is not empty: removing existing items!"); foreach ($list->getItems() as $item) $list->removeItem($item); } // ou échec de l'import (sauf avec l'option "update") elseif (! $input->getOption('update')) { $this->io->error("List '$ref' contains items: use 'empty' or 'update' option!"); exit; } } // ajout/mise à jour des infos et éléments depuis les données importées $list->import($data); // fin de la boucle en mode test (pas de commit) if ($this->test) { $this->io->note("TEST mode: no commit."); # dump($list);continue; } // enregistrement dans la base de données $this->em->persist($list); $this->em->flush(); $this->io->success("Import done."); } return true; } /** * */protected function getSources($path) { $this->io->section("Scanning source: $path"); $files = array(); if (is_dir($path)) foreach (scandir($path) as $name) { $file = $path . '/' . $name; if (! is_file($file)) continue; if (preg_match('/^.+\.(ya?ml|json)$/i', $name, $m)) $files[ $file ] = $m[1]; } elseif (is_file($path)) { $name = basename( $path ); if (preg_match('/^.+\.(ya?ml|json)$/', $name, $m)) $files[ $path ] = $m[1]; } if ($this->out->isVeryVerbose()) $this->io->listing(array_keys($files)); return $files; } } |