partkeepr

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

commit 1eade4e1cca9c8b39ff3b0adef2dc7c3c33e6228
parent e1a247c7bf80ba6757e1ed3f4fc337386a0503c7
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 19 Dec 2015 17:50:52 +0100

Externalized footprint setup to an own service, added import footprints command

Diffstat:
Asrc/PartKeepr/SetupBundle/Command/ImportFootprintCommand.php | 25+++++++++++++++++++++++++
Msrc/PartKeepr/SetupBundle/Controller/FootprintSetupController.php | 115+++----------------------------------------------------------------------------
Asrc/PartKeepr/SetupBundle/DependencyInjection/PartKeeprSetupExtension.php | 24++++++++++++++++++++++++
Asrc/PartKeepr/SetupBundle/Resources/config/services.xml | 15+++++++++++++++
Asrc/PartKeepr/SetupBundle/Services/FootprintSetupService.php | 170+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 237 insertions(+), 112 deletions(-)

diff --git a/src/PartKeepr/SetupBundle/Command/ImportFootprintCommand.php b/src/PartKeepr/SetupBundle/Command/ImportFootprintCommand.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 ImportFootprintCommand extends ContainerAwareCommand +{ + public function configure() + { + parent::configure(); + $this->setName('partkeepr:setup:import-footprints'); + $this->setDescription("Imports the default PartKeepr footprints"); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $return = $this->getContainer()->get("partkeepr.setup.footprint_service")->importFootprints(); + + $output->writeln(sprintf("%d footprints imported, %d existing footprints skipped", $return["imported"], + $return["skipped"])); + } +} diff --git a/src/PartKeepr/SetupBundle/Controller/FootprintSetupController.php b/src/PartKeepr/SetupBundle/Controller/FootprintSetupController.php @@ -1,21 +1,13 @@ <?php namespace PartKeepr\SetupBundle\Controller; -use PartKeepr\FootprintBundle\Entity\Footprint; -use PartKeepr\FootprintBundle\Entity\FootprintCategory; -use PartKeepr\FootprintBundle\Entity\FootprintImage; -use Symfony\Component\HttpFoundation\File\File; 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 FootprintSetupController extends SetupController { - const FOOTPRINT_PATH = "@PartKeeprSetupBundle/Resources/setup-data/footprints/"; - const FOOTPRINT_DATA = "footprints.yml"; - /** * @Route("/setup/_int_create_footprints") */ @@ -27,26 +19,8 @@ class FootprintSetupController extends SetupController "message" => "Default footprints successfully created", ); - $path = $this->get("kernel")->locateResource(self::FOOTPRINT_PATH.self::FOOTPRINT_DATA); - - $skipped = 0; - try { - $yaml = new Parser(); - $data = $yaml->parse(file_get_contents($path)); - - $entityManager = $this->get("doctrine.orm.default_entity_manager"); - - foreach ($data as $footprintName => $footprintData) { - if ($this->footprintExists($footprintName)) { - $skipped++; - continue; - } - - $this->createFootprint($footprintName, $footprintData); - } - - $entityManager->flush(); + $this->get("partkeepr.setup.footprint_service")->importFootprints(); } catch (\Exception $e) { $response["success"] = false; $response["message"] = "Footprint creation error"; @@ -58,6 +32,8 @@ class FootprintSetupController extends SetupController /** * @Route("/setup/createFootprints") + * + * @param Request $request */ public function createFootprintsAction(Request $request) { @@ -66,89 +42,4 @@ class FootprintSetupController extends SetupController return new Response($response->getContent()); } - protected function createFootprint($footprintName, $footprintData) - { - $fileService = $this->get("partkeepr_uploadedfile_service"); - - $footprintCategoryService = $this->get("partkeepr.footprint.category_service"); - $footprintCategoryRootNode = $footprintCategoryService->getRootNode(); - - $footprint = new Footprint(); - $footprint->setName($footprintName); - - if (array_key_exists("description", $footprintData)) { - $footprint->setDescription($footprintData["description"]); - } - - if (array_key_exists("category", $footprintData)) { - $footprintCategory = $this->addFootprintCategoryPath(explode("/", $footprintData["category"]), - $footprintCategoryRootNode); - $footprint->setCategory($footprintCategory); - } - - if (array_key_exists("image", $footprintData)) { - $footprintImage = new FootprintImage(); - - - $file = $this->get("kernel")->locateResource(self::FOOTPRINT_PATH.$footprintData["image"]); - $fileService->replaceFromFilesystem($footprintImage, new File($file)); - - $footprint->setImage($footprintImage); - } - - $this->get("doctrine.orm.default_entity_manager")->persist($footprint); - } - - /** - * Creates a node structure for the given path - * - * @param $path array The components of the path - * @param $parentNode FootprintCategory The parent node - * - * @return FootprintCategory - */ - protected function addFootprintCategoryPath(Array $path, FootprintCategory $parentNode) - { - if (count($path) == 0) { - return $parentNode; - } - $name = array_shift($path); - - $category = null; - - foreach ($parentNode->getChildren() as $child) { - if ($child->getName() == $name) { - $category = $child; - } - } - - if ($category === null) { - $category = new FootprintCategory(); - $category->setParent($parentNode); - $category->setName($name); - $parentNode->getChildren()->add($category); - - $this->get("doctrine.orm.default_entity_manager")->persist($category); - } - - return $this->addFootprintCategoryPath($path, $category); - } - - /** - * Checks if the specified footprint exists - * - * @param string $name The footprint name - */ - protected function footprintExists($name) - { - $dql = "SELECT COUNT(fp) FROM PartKeepr\FootprintBundle\Entity\Footprint fp WHERE fp.name = :name"; - $query = $this->get("doctrine.orm.default_entity_manager")->createQuery($dql); - $query->setParameter("name", $name); - - if ($query->getSingleScalarResult() == 0) { - return false; - } else { - return true; - } - } } diff --git a/src/PartKeepr/SetupBundle/DependencyInjection/PartKeeprSetupExtension.php b/src/PartKeepr/SetupBundle/DependencyInjection/PartKeeprSetupExtension.php @@ -0,0 +1,24 @@ +<?php +namespace PartKeepr\SetupBundle\DependencyInjection; + +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; + +/** + * This is the class that loads and manages your bundle configuration + * + * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} + */ +class PartKeeprSetupExtension extends Extension +{ + /** + * {@inheritdoc} + */ + public function load(array $configs, ContainerBuilder $container) + { + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.xml'); + } +} diff --git a/src/PartKeepr/SetupBundle/Resources/config/services.xml b/src/PartKeepr/SetupBundle/Resources/config/services.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" ?> + +<container xmlns="http://symfony.com/schema/dic/services" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + <services> + <service id="partkeepr.setup.footprint_service" class="PartKeepr\SetupBundle\Services\FootprintSetupService"> + <argument type="service" id="doctrine.orm.default_entity_manager" /> + <argument type="service" id="partkeepr.footprint.category_service" /> + <argument type="service" id="partkeepr_uploadedfile_service"/> + <argument type="service" id="kernel"/> + </service> + </services> +</container> diff --git a/src/PartKeepr/SetupBundle/Services/FootprintSetupService.php b/src/PartKeepr/SetupBundle/Services/FootprintSetupService.php @@ -0,0 +1,170 @@ +<?php +namespace PartKeepr\SetupBundle\Services; + + +use Doctrine\ORM\EntityManager; +use PartKeepr\CategoryBundle\Services\CategoryService; +use PartKeepr\FootprintBundle\Entity\Footprint; +use PartKeepr\FootprintBundle\Entity\FootprintCategory; +use PartKeepr\FootprintBundle\Entity\FootprintImage; +use PartKeepr\UploadedFileBundle\Services\UploadedFileService; +use Symfony\Component\HttpFoundation\File\File; +use Symfony\Component\HttpKernel\KernelInterface; +use Symfony\Component\Yaml\Parser; + +class FootprintSetupService +{ + const FOOTPRINT_PATH = "@PartKeeprSetupBundle/Resources/setup-data/footprints/"; + const FOOTPRINT_DATA = "footprints.yml"; + + /** + * @var EntityManager + */ + private $entityManager; + + /** + * @var CategoryService + */ + private $footprintCategoryService; + + /** + * @var UploadedFileService + */ + private $uploadedFileService; + + /** + * @var KernelInterface + */ + private $kernel; + + public function __construct( + EntityManager $entityManager, + CategoryService $categoryService, + UploadedFileService $uploadedFileService, + KernelInterface $kernel + ) { + $this->entityManager = $entityManager; + $this->footprintCategoryService = $categoryService; + $this->uploadedFileService = $uploadedFileService; + $this->kernel = $kernel; + } + + /** + * Imports the existing footprints. Skips existing footprints + * + * @return array An array with the keys "skipped" and "imported" which contain the number of footprints skipped and imported + */ + public function importFootprints() + { + $path = $this->kernel->locateResource(self::FOOTPRINT_PATH.self::FOOTPRINT_DATA); + + $skipped = 0; + $count = 0; + + $yaml = new Parser(); + $data = $yaml->parse(file_get_contents($path)); + + foreach ($data as $footprintName => $footprintData) { + if ($this->footprintExists($footprintName)) { + $skipped++; + continue; + } + + $this->createFootprint($footprintName, $footprintData); + $count++; + } + + $this->entityManager->flush(); + + return array("skipped" => $skipped, "imported" => $count); + } + + protected function createFootprint($footprintName, $footprintData) + { + + /** + * @var FootprintCategory $footprintCategoryRootNode + */ + $footprintCategoryRootNode = $this->footprintCategoryService->getRootNode(); + + $footprint = new Footprint(); + $footprint->setName($footprintName); + + if (array_key_exists("description", $footprintData)) { + $footprint->setDescription($footprintData["description"]); + } + + if (array_key_exists("category", $footprintData)) { + $footprintCategory = $this->addFootprintCategoryPath(explode("/", $footprintData["category"]), + $footprintCategoryRootNode); + $footprint->setCategory($footprintCategory); + } + + if (array_key_exists("image", $footprintData)) { + $footprintImage = new FootprintImage(); + + + $file = $this->kernel->locateResource(self::FOOTPRINT_PATH.$footprintData["image"]); + $this->uploadedFileService->replaceFromFilesystem($footprintImage, new File($file)); + + $footprint->setImage($footprintImage); + } + + $this->entityManager->persist($footprint); + } + + /** + * Creates a node structure for the given path + * + * @param $path array The components of the path + * @param $parentNode FootprintCategory The parent node + * + * @return FootprintCategory + */ + protected function addFootprintCategoryPath(Array $path, FootprintCategory $parentNode) + { + if (count($path) == 0) { + return $parentNode; + } + $name = array_shift($path); + + $category = null; + + foreach ($parentNode->getChildren() as $child) { + if ($child->getName() == $name) { + $category = $child; + } + } + + if ($category === null) { + $category = new FootprintCategory(); + $category->setParent($parentNode); + $category->setName($name); + $parentNode->getChildren()->add($category); + + $this->entityManager->persist($category); + } + + return $this->addFootprintCategoryPath($path, $category); + } + + /** + * Checks if the specified footprint exists + * + * @param string $name The footprint name + * + * @return boolean true if the footprints exists, false otherwise + */ + protected function footprintExists($name) + { + $dql = "SELECT COUNT(fp) FROM PartKeepr\FootprintBundle\Entity\Footprint fp WHERE fp.name = :name"; + $query = $this->entityManager->createQuery($dql); + $query->setParameter("name", $name); + + if ($query->getSingleScalarResult() == 0) { + return false; + } else { + return true; + } + } +}