From 794202d05de2ae626ba2d3d48f8b94aa941468b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbner?= Date: Tue, 11 Apr 2017 17:30:20 +0200 Subject: [PATCH] Move ldraw dir dependency from config to command --- app/config/config.yml | 7 +- app/config/parameters.yml.dist | 5 +- app/config/service/loader.yml | 6 +- src/AppBundle/Command/LoadModelsCommand.php | 37 +++-- src/AppBundle/Manager/BaseManager.php | 13 +- src/AppBundle/Manager/RebrickableManager.php | 12 +- ...aderService.php => ModelLoaderService.php} | 144 ++++++++---------- 7 files changed, 111 insertions(+), 113 deletions(-) rename src/AppBundle/Service/Loader/{LDrawLoaderService.php => ModelLoaderService.php} (56%) diff --git a/app/config/config.yml b/app/config/config.yml index 89fd475..58be23c 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -142,11 +142,6 @@ oneup_flysystem: media: local: directory: "%kernel.root_dir%/../var/media/" - ldraw_library: - local: - directory: "%ldraw_library%" filesystems: media: - adapter: media - ldraw_library: - adapter: ldraw_library \ No newline at end of file + adapter: media \ No newline at end of file diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index 451dd92..be71996 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -25,7 +25,4 @@ parameters: rebrickable_apikey: ~ # Absolute path to OSMesa port of LDView - ldview_bin: /usr/bin/ldview - - # Path to LDraw library - ldraw_library: ~ \ No newline at end of file + ldview_bin: /usr/bin/ldview \ No newline at end of file diff --git a/app/config/service/loader.yml b/app/config/service/loader.yml index 2fb30a7..30b4804 100644 --- a/app/config/service/loader.yml +++ b/app/config/service/loader.yml @@ -14,9 +14,9 @@ services: arguments: ['%rebrickable_url%'] parent: service.loader - service.loader.ldraw: - class: AppBundle\Service\Loader\LDrawLoaderService - arguments: ['@oneup_flysystem.ldraw_library_filesystem','@service.ldview', '@manager.ldraw', '@app.relation.mapper'] + service.loader.model: + class: AppBundle\Service\Loader\ModelLoaderService + arguments: ['@service.ldview', '@manager.ldraw', '@app.relation.mapper'] parent: service.loader service.loader.relation: diff --git a/src/AppBundle/Command/LoadModelsCommand.php b/src/AppBundle/Command/LoadModelsCommand.php index f206e47..c8ac9f1 100644 --- a/src/AppBundle/Command/LoadModelsCommand.php +++ b/src/AppBundle/Command/LoadModelsCommand.php @@ -3,6 +3,7 @@ namespace AppBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\LockableTrait; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; @@ -11,6 +12,8 @@ use Symfony\Component\Console\Output\OutputInterface; class LoadModelsCommand extends ContainerAwareCommand { + use LockableTrait; + protected function configure() { $this @@ -18,35 +21,45 @@ class LoadModelsCommand extends ContainerAwareCommand ->setDescription('Loads LDraw library models into database') ->setHelp('This command allows you to load LDraw library models into while converting .dat files to .stl') ->setDefinition( - new InputDefinition(array( - new InputOption('images', 'i'), - new InputOption('ldraw', 'l', InputOption::VALUE_REQUIRED), - new InputOption('file', 'f', InputOption::VALUE_REQUIRED), - )) + new InputDefinition([ + new InputArgument('ldraw', InputArgument::REQUIRED, 'Path to LDraw library directory'), + new InputOption('images', 'i',InputOption::VALUE_NONE, 'Do you want to generate images of models?'), + new InputOption('all','a',InputOption::VALUE_NONE, 'Do you want to load whole LDraw libary?'), + new InputOption('file','f',InputOption::VALUE_REQUIRED, 'Path to DAT file that should be loaded into database') + ]) ); } + //TODO log errors protected function execute(InputInterface $input, OutputInterface $output) { - $ldrawLoader = $this->getContainer()->get('service.loader.ldraw'); - $ldrawLoader->setOutput($output); + if (!$this->lock()) { + $output->writeln('The command is already running in another process.'); + + return 0; + } + + $ldrawLoader = $this->getContainer()->get('service.loader.model'); + $ldrawLoader->setOutput($output); + $ldrawLoader->setLDrawLibraryContext(realpath($input->getArgument('ldraw'))); - //TODO log errors try { - // TODO handle relative path to dir if (($ldrawPath = $input->getOption('file')) != null) { - $ldrawLoader->loadFileContext($ldrawPath); + $ldrawLoader->loadFileContext(dirname(realpath($ldrawPath))); $model = $ldrawLoader->loadModel($ldrawPath); if($model !== null) { $this->getContainer()->get('manager.ldraw.model')->getRepository()->save($model); } - } else { - $ldrawLoader->loadAllModels(); + } + if ($input->getOption('all')) { + $ldrawLoader->loadAllModels(); } } catch (\Exception $e) { printf($e->getMessage()); } + + $this->release(); } } diff --git a/src/AppBundle/Manager/BaseManager.php b/src/AppBundle/Manager/BaseManager.php index cfeb49c..df466fe 100644 --- a/src/AppBundle/Manager/BaseManager.php +++ b/src/AppBundle/Manager/BaseManager.php @@ -2,15 +2,26 @@ namespace AppBundle\Manager; +use AppBundle\Repository\BaseRepository; +use Doctrine\ORM\EntityManager; + class BaseManager { + /** @var EntityManager $em */ + protected $em; + protected $repository; /** - * @return mixed + * @return BaseRepository */ public function getRepository() { return $this->repository; } + + public function setEntityManager(EntityManager $entityManager) + { + $this->em = $entityManager; + } } diff --git a/src/AppBundle/Manager/RebrickableManager.php b/src/AppBundle/Manager/RebrickableManager.php index 9896d53..38e2fc6 100644 --- a/src/AppBundle/Manager/RebrickableManager.php +++ b/src/AppBundle/Manager/RebrickableManager.php @@ -1,18 +1,12 @@ em->getRepository(Theme::class)->findAll(); } } diff --git a/src/AppBundle/Service/Loader/LDrawLoaderService.php b/src/AppBundle/Service/Loader/ModelLoaderService.php similarity index 56% rename from src/AppBundle/Service/Loader/LDrawLoaderService.php rename to src/AppBundle/Service/Loader/ModelLoaderService.php index 08679fd..2d5ba87 100644 --- a/src/AppBundle/Service/Loader/LDrawLoaderService.php +++ b/src/AppBundle/Service/Loader/ModelLoaderService.php @@ -5,22 +5,23 @@ namespace AppBundle\Service\Loader; use AppBundle\Entity\LDraw\Alias; use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\LDraw\Type; +use AppBundle\Exception\ConvertingFailedException; use AppBundle\Manager\LDrawManager; use AppBundle\Service\LDViewService; use AppBundle\Utils\DatParser; use AppBundle\Utils\RelationMapper; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; - -//use Symfony\Component\Asset\Exception\LogicException; +use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; //TODO refactor -class LDrawLoaderService extends BaseLoaderService +class ModelLoaderService extends BaseLoaderService { /** * @var Filesystem */ - private $ldrawLibrary; + private $ldrawLibraryContext; /** * @var Filesystem @@ -43,57 +44,54 @@ class LDrawLoaderService extends BaseLoaderService /** @var RelationMapper */ protected $relationMapper; + /** @var Finder */ + private $finder; + /** * LDrawLoaderService constructor. - * @param Filesystem $ldrawLibraryFilesystem * @param LDViewService $LDViewService * @param LDrawManager $ldrawService * @param RelationMapper $relationMapper */ - public function __construct($ldrawLibraryFilesystem, $LDViewService, $ldrawService, $relationMapper) + public function __construct($LDViewService, $ldrawService, $relationMapper) { $this->LDViewService = $LDViewService; $this->ldrawService = $ldrawService; - $this->datParser = new DatParser(); - $this->ldrawLibrary = $ldrawLibraryFilesystem; $this->relationMapper = $relationMapper; + + $this->datParser = new DatParser(); + $this->finder = new Finder(); } - private function dumpModel($model, $level = 1) { - $level = $level + 1; - dump(str_repeat("-", 2*$level).'> '.$model->getNumber()); - foreach ($model->getSubparts() as $subpart) { - $this->dumpModel($subpart->getSubpart(), $level); - } + public function setLDrawLibraryContext($ldrawLibrary) + { + $adapter = new Local($ldrawLibrary); + $this->ldrawLibraryContext = new Filesystem($adapter); + + $this->LDViewService->setLdrawFilesystem($this->ldrawLibraryContext); + } + + public function loadFileContext($file) { + $adapter = new Local($file); + $this->fileContext = new Filesystem($adapter); } public function loadAllModels() { - $files = $this->ldrawLibrary->get('parts')->getContents(); + $files = $this->finder->in(['/home/hubnedav/Documents/ldraw'])->path('parts/')->name('*.dat')->depth(1)->files(); + $modelManager = $this->ldrawService->getModelManager(); - $this->initProgressBar(count($files)); + $this->initProgressBar($files->count()); + /** @var SplFileInfo $file */ foreach ($files as $file) { - if ($file['type'] == 'file' && $file['extension'] == 'dat') { + $this->newModels = []; - $this->newModels = []; + $model = $this->loadModel($file->getRealPath()); - $model = $this->loadModel($this->ldrawLibrary->getAdapter()->getPathPrefix().$file['path']); - - if($model !== null) { -// dump($model->getNumber()); - try { - $modelManager->getRepository()->save($model); - } catch (\Exception $exception) { - dump($exception); -// dump($model); - - $this->dumpModel($model); - - exit(); - } - } + if($model !== null) { + $modelManager->getRepository()->save($model); } $this->progressBar->advance(); @@ -116,33 +114,22 @@ class LDrawLoaderService extends BaseLoaderService $subpartManager = $this->ldrawService->getSubpartManager(); if(($model = $modelManager->findByNumber(basename($file,'.dat'))) || ($model = isset($this->newModels[basename($file,'.dat')]) ? $this->newModels[basename($file,'.dat')] : null)) { - $this->LDViewService->datToPng($file); return $model; } try { - $header = $this->datParser->parse($file); + $modelArray = $this->datParser->parse($file); } catch (\Exception $e) { - dump($e); + dump($e->getMessage()); return null; } - if ($this->isModelIncluded($header)) { + if ($this->isModelIncluded($modelArray)) { - if($this->relationMapper->find($header['id'], 'alias_model') !== $header['id']) { - $parentFile = $this->findModelFile($this->relationMapper->find($header['id'], 'alias_model')); - } else { - $parentFile = isset($header['parent']) && strpos($header['parent'], 's\\') === false ? $this->findModelFile($header['parent']) : null; - } - - if ($parentFile) { + if ($parentModelFile = $this->getParentModelFile($modelArray)) { try { - $parentModel = $this->loadModel($parentFile); - - if($parentModel) { - $alias = new Alias(); - $alias->setNumber($header['id']); - $alias->setModel($parentModel); + if(($parentModel = $this->loadModel($parentModelFile))!= null) { + $alias = $this->ldrawService->getAliasManager()->create($modelArray['id'], $parentModel); $parentModel->addAlias($alias); $this->newModels[$parentModel->getNumber()] = $parentModel; @@ -154,26 +141,23 @@ class LDrawLoaderService extends BaseLoaderService return null; } } else { - $model = $modelManager->create($header['id']); - $model->setName($header['name']); - $model->setCategory($this->ldrawService->getCategoryManager()->create($header['category'])); + $model = $modelManager->create($modelArray['id']); - if (isset($header['keywords'])) { - foreach ($header['keywords'] as $keyword) { + if (isset($modelArray['keywords'])) { + foreach ($modelArray['keywords'] as $keyword) { $keyword = stripslashes(strtolower(trim($keyword))); $model->addKeyword($this->ldrawService->getKeywordManager()->create($keyword)); } } - if (isset($header['subparts'])) { - foreach ($header['subparts'] as $subpartId => $count) { + if (isset($modelArray['subparts'])) { + foreach ($modelArray['subparts'] as $subpartId => $count) { if(strpos($subpartId, 's\\') === false) { if(($subpartFile = $this->findModelFile($subpartId)) != null) { try { - $subModel = $this->loadModel($subpartFile); - - if ($subModel) { + if ($subModel = $this->loadModel($subpartFile)) { $subpart = $subpartManager->create($model,$subModel,$count); + $model->addSubpart($subpart); } } catch (\Exception $e) { @@ -185,9 +169,14 @@ class LDrawLoaderService extends BaseLoaderService } } - $model->setAuthor($header['author']); - $model->setModified($header['modified']); - $model->setPath($this->loadStlModel($file)); + $model + ->setName($modelArray['name']) + ->setCategory($this->ldrawService->getCategoryManager()->create($modelArray['category'])) + ->setAuthor($modelArray['author']) + ->setModified($modelArray['modified']) + ->setPath($this->loadStlModel($file)); + + $this->LDViewService->datToPng($file); $this->newModels[$model->getNumber()] = $model; } @@ -196,6 +185,14 @@ class LDrawLoaderService extends BaseLoaderService return $model; } + private function getParentModelFile($modelArray) { + if($this->relationMapper->find($modelArray['id'], 'alias_model') !== $modelArray['id']) { + return $this->findModelFile($this->relationMapper->find($modelArray['id'], 'alias_model')); + } else { + return strpos($modelArray['parent'], 's\\') === false ? $this->findModelFile($modelArray['parent']) : null; + } + } + /** * Find model file on ldraw filesystem. * @@ -209,33 +206,24 @@ class LDrawLoaderService extends BaseLoaderService if($this->fileContext && $this->fileContext->has($filename)) { return $this->fileContext->getAdapter()->getPathPrefix().$filename; - } else if ($this->ldrawLibrary->has('parts/'.$filename)) { - return $this->ldrawLibrary->getAdapter()->getPathPrefix().'parts/'.$filename; + } else if ($this->ldrawLibraryContext->has('parts/'.$filename)) { + return $this->ldrawLibraryContext->getAdapter()->getPathPrefix().'parts/'.$filename; } return null; } - public function loadFileContext($file) { - $adapter = new Local(dirname(realpath($file))); - $this->fileContext = new Filesystem($adapter); - } - /** * Determine if model file should be loaded into database. * - * @param $header + * @param $modelArray * * @return bool */ - private function isModelIncluded($header) + private function isModelIncluded($modelArray) { // Do not include sticker parts and incomplete parts - if ( - $header['type'] != 'Subpart' - && $header['type'] != 'Sticker' -// && !(($header['type'] == 'Printed') && $this->findModelFile($header['parent'])) - ) { + if ( $modelArray['type'] != 'Subpart' && $modelArray['type'] != 'Sticker' ) { return true; } @@ -255,7 +243,7 @@ class LDrawLoaderService extends BaseLoaderService { try { return $this->LDViewService->datToStl($file)->getPath(); - } catch (\Exception $e) { + } catch (ConvertingFailedException $e) { throw $e; //TODO } }