partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 76375f372487b41cc8eac9320b1104d5e014f8df
parent d3825a3d9bce225f429a7556c37362643f32e937
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 19 Dec 2015 18:27:12 +0100

Externalized unit setup, added cli command to import units

Diffstat:
Asrc/PartKeepr/SetupBundle/Command/ImportUnitsCommand.php | 25+++++++++++++++++++++++++
Msrc/PartKeepr/SetupBundle/Controller/UnitSetupController.php | 68++++----------------------------------------------------------------
Msrc/PartKeepr/SetupBundle/Resources/config/services.xml | 5+++++
Asrc/PartKeepr/SetupBundle/Services/UnitSetupService.php | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 148 insertions(+), 64 deletions(-)

diff --git a/src/PartKeepr/SetupBundle/Command/ImportUnitsCommand.php b/src/PartKeepr/SetupBundle/Command/ImportUnitsCommand.php @@ -0,0 +1,25 @@ +<?php +namespace PartKeepr\SetupBundle\Command; + +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + + +class ImportUnitsCommand extends ContainerAwareCommand +{ + public function configure() + { + parent::configure(); + $this->setName('partkeepr:setup:import-units'); + $this->setDescription("Imports the default PartKeepr units"); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $return = $this->getContainer()->get("partkeepr.setup.unit_service")->importUnits(); + + $output->writeln(sprintf("%d units imported, %d existing units skipped", $return["imported"], + $return["skipped"])); + } +} diff --git a/src/PartKeepr/SetupBundle/Controller/UnitSetupController.php b/src/PartKeepr/SetupBundle/Controller/UnitSetupController.php @@ -3,19 +3,13 @@ namespace PartKeepr\SetupBundle\Controller; -use PartKeepr\SiPrefixBundle\Entity\SiPrefix; -use PartKeepr\UnitBundle\Entity\Unit; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -use Symfony\Component\Yaml\Parser; class UnitSetupController extends SetupController { - const UNIT_PATH = "@PartKeeprSetupBundle/Resources/setup-data/"; - const UNIT_DATA = "units.yml"; - /** * @Route("/setup/_int_create_units") */ @@ -27,41 +21,8 @@ class UnitSetupController extends SetupController "message" => "Default units successfully created/updated", ); - $path = $this->get("kernel")->locateResource(self::UNIT_PATH.self::UNIT_DATA); - try { - $yaml = new Parser(); - $data = $yaml->parse(file_get_contents($path)); - - $entityManager = $this->get("doctrine.orm.default_entity_manager"); - - foreach ($data as $unitName => $unitData) { - $unit = $this->getUnit($unitName); - - if ($unit === null) { - $unit = new Unit(); - $unit->setName($unitName); - $unit->setSymbol($unitData["symbol"]); - - if (array_key_exists("prefixes", $unitData)) { - if (!is_array($unitData["prefixes"])) { - throw new \Exception($unitName." doesn't contain a prefix list, or the prefix list is not an array."); - } - - foreach ($unitData["prefixes"] as $name) { - $prefix = $this->getSiPrefix($name); - if ($prefix === null) { - throw new \Exception("Unable to find SI Prefix ".$name); - } - - $unit->getPrefixes()->add($prefix); - } - } - $entityManager->persist($unit); - $entityManager->flush(); - } - } - + $this->get("partkeepr.setup.unit_service")->importUnits(); } catch (\Exception $e) { $response["success"] = false; $response["message"] = "Unit creation error"; @@ -73,6 +34,9 @@ class UnitSetupController extends SetupController /** * @Route("/setup/createUnits") + * @param Request $request + * + * @return Response */ public function createUnitAction(Request $request) { @@ -80,28 +44,4 @@ class UnitSetupController extends SetupController return new Response($response->getContent()); } - - /** - * Checks if the specified SI Prefix - * - * @param string $name The footprint name - */ - protected function getUnit($name) - { - $repository = $this->get("doctrine.orm.default_entity_manager")->getRepository("PartKeeprUnitBundle:Unit"); - - return $repository->findOneBy(array("name" => $name)); - } - - /** - * Finds an SI Prefix by name - * - * @param string $name The SI Prefix name - * @return SiPrefix|null - */ - protected function getSiPrefix($name) - { - $repository = $this->get("doctrine.orm.default_entity_manager")->getRepository("PartKeeprSiPrefixBundle:SiPrefix"); - return $repository->findOneBy(array("prefix" => $name)); - } } diff --git a/src/PartKeepr/SetupBundle/Resources/config/services.xml b/src/PartKeepr/SetupBundle/Resources/config/services.xml @@ -28,5 +28,10 @@ <argument id="kernel" type="service"/> </service> + <service id="partkeepr.setup.unit_service" class="PartKeepr\SetupBundle\Services\UnitSetupService"> + <argument id="doctrine.orm.default_entity_manager" type="service"/> + <argument id="kernel" type="service"/> + </service> + </services> </container> diff --git a/src/PartKeepr/SetupBundle/Services/UnitSetupService.php b/src/PartKeepr/SetupBundle/Services/UnitSetupService.php @@ -0,0 +1,114 @@ +<?php + + +namespace PartKeepr\SetupBundle\Services; + +use Doctrine\ORM\EntityManager; +use PartKeepr\SiPrefixBundle\Entity\SiPrefix; +use PartKeepr\UnitBundle\Entity\Unit; +use Symfony\Component\HttpKernel\KernelInterface; +use Symfony\Component\Yaml\Parser; + +class UnitSetupService +{ + const UNIT_PATH = "@PartKeeprSetupBundle/Resources/setup-data/"; + const UNIT_DATA = "units.yml"; + + /** + * @var EntityManager + */ + private $entityManager; + + /** + * @var KernelInterface + */ + private $kernel; + + /** + * UnitSetupService constructor. + * + * @param EntityManager $entityManager + * @param KernelInterface $kernel + */ + public function __construct(EntityManager $entityManager, KernelInterface $kernel) + { + $this->entityManager = $entityManager; + $this->kernel = $kernel; + } + + /** + * Imports units. + * + * @return array An array with the keys "skipped" and "imported" which contain the number of units skipped and imported + * @throws \Exception If an error occured + */ + public function importUnits() + { + $path = $this->kernel->locateResource(self::UNIT_PATH.self::UNIT_DATA); + + $yaml = new Parser(); + $data = $yaml->parse(file_get_contents($path)); + + $count = 0; + $skipped = 0; + + foreach ($data as $unitName => $unitData) { + $unit = $this->getUnit($unitName); + + if ($unit === null) { + $unit = new Unit(); + $unit->setName($unitName); + $unit->setSymbol($unitData["symbol"]); + + if (array_key_exists("prefixes", $unitData)) { + if (!is_array($unitData["prefixes"])) { + throw new \Exception($unitName." doesn't contain a prefix list, or the prefix list is not an array."); + } + + foreach ($unitData["prefixes"] as $name) { + $prefix = $this->getSiPrefix($name); + if ($prefix === null) { + throw new \Exception("Unable to find SI Prefix ".$name); + } + + $unit->getPrefixes()->add($prefix); + } + } + $this->entityManager->persist($unit); + $this->entityManager->flush(); + $count++; + } else { + $skipped++; + } + } + + return array("imported" => $count, "skipped" => $skipped); + } + + /** + * Checks if the specified SI Prefix + * + * @param string $name The footprint name + * @return Unit|null + */ + protected function getUnit($name) + { + $repository = $this->entityManager->getRepository("PartKeeprUnitBundle:Unit"); + + return $repository->findOneBy(array("name" => $name)); + } + + /** + * Finds an SI Prefix by name + * + * @param string $name The SI Prefix name + * + * @return SiPrefix|null + */ + protected function getSiPrefix($name) + { + $repository = $this->entityManager->getRepository("PartKeeprSiPrefixBundle:SiPrefix"); + + return $repository->findOneBy(array("prefix" => $name)); + } +}