/** * @Route("/setup/_int_create_si_prefixes") */ public function intCreateSiPrefixes() { $response = array("success" => true, "errors" => [], "message" => "SI Prefixes successfully created/updated"); $path = $this->get("kernel")->locateResource(self::SIPREFIX_PATH . self::SIPREFIX_DATA); try { $yaml = new Parser(); $data = $yaml->parse(file_get_contents($path)); $entityManager = $this->get("doctrine.orm.default_entity_manager"); foreach ($data as $prefixName => $prefixData) { $prefix = $this->getSiPrefix($prefixName); if ($prefix === null) { $prefix = new SiPrefix(); $prefix->setPrefix($prefixName); $entityManager->persist($prefix); } $prefix->setExponent($prefixData["exponent"]); $prefix->setSymbol($prefixData["symbol"]); $prefix->setBase($prefixData["base"]); } $entityManager->flush(); } catch (\Exception $e) { $response["success"] = false; $response["message"] = "SI Prefix creation error"; $response["errors"] = [$e->getMessage()]; } return new JsonResponse($response); }
/** * Imports or updates the existing si prefixes. * * @return array An array with the keys "skipped" and "imported" which contain the number of si prefixes skipped and imported */ public function importSiPrefixes() { $path = $this->kernel->locateResource(self::SIPREFIX_PATH . self::SIPREFIX_DATA); $yaml = new Parser(); $data = $yaml->parse(file_get_contents($path)); $count = 0; $updated = 0; foreach ($data as $prefixName => $prefixData) { $prefix = $this->getSiPrefix($prefixName); if ($prefix === null) { $prefix = new SiPrefix(); $prefix->setPrefix($prefixName); $this->entityManager->persist($prefix); $count++; } $prefix->setExponent($prefixData["exponent"]); $prefix->setSymbol($prefixData["symbol"]); $prefix->setBase($prefixData["base"]); $updated++; } $this->entityManager->flush(); return array("updated" => $updated - $count, "imported" => $count); }