From dcb475407b990d26d56678692da7c41318fa2b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbner?= Date: Tue, 17 Jan 2017 00:30:39 +0100 Subject: [PATCH] Move loaders to own namespace --- app/config/services.yml | 6 +- src/AppBundle/Command/LoadLDrawCommand.php | 6 +- .../Command/LoadRebrickableCommand.php | 8 +- .../{Service => Loader}/LDrawLoader.php | 83 ++++++++++--------- src/AppBundle/Loader/Loader.php | 23 +++++ .../{Service => Loader}/RebrickableLoader.php | 24 +++--- 6 files changed, 93 insertions(+), 57 deletions(-) rename src/AppBundle/{Service => Loader}/LDrawLoader.php (56%) create mode 100644 src/AppBundle/Loader/Loader.php rename src/AppBundle/{Service => Loader}/RebrickableLoader.php (93%) diff --git a/app/config/services.yml b/app/config/services.yml index db4c4dc..028f1c7 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -18,11 +18,11 @@ services: arguments: ['@doctrine.orm.entity_manager', '@manager.brickset','@manager.rebrickable'] loader.rebrickable: - class: AppBundle\Service\RebrickableLoader + class: AppBundle\Loader\RebrickableLoader arguments: ['@doctrine.orm.entity_manager', '@manager.rebrickable'] loader.ldraw: - class: AppBundle\Service\LDrawLoader - arguments: ['@doctrine.orm.entity_manager', '%kernel.root_dir%/../bin/ldview', '%kernel.root_dir%/../var/data/ldraw'] + class: AppBundle\Loader\LDrawLoader + arguments: ['@doctrine.orm.entity_manager', '%kernel.root_dir%/../bin/ldview', '@oneup_flysystem.ldraw_filesystem'] app.form.filter_set: class: AppBundle\Form\FilterSetType diff --git a/src/AppBundle/Command/LoadLDrawCommand.php b/src/AppBundle/Command/LoadLDrawCommand.php index 13642f6..193d0ed 100644 --- a/src/AppBundle/Command/LoadLDrawCommand.php +++ b/src/AppBundle/Command/LoadLDrawCommand.php @@ -22,6 +22,10 @@ class LoadLDrawCommand extends ContainerAwareCommand protected function execute(InputInterface $input, OutputInterface $output) { - $this->getContainer()->get('loader.ldraw')->loadModels($input->getArgument('ldraw')); + $ldrawLoader = $this->getContainer()->get('loader.ldraw'); + + $ldrawLoader->setOutput($output); + + $ldrawLoader->loadModels($input->getArgument('ldraw')); } } diff --git a/src/AppBundle/Command/LoadRebrickableCommand.php b/src/AppBundle/Command/LoadRebrickableCommand.php index 2209135..f4c01dc 100644 --- a/src/AppBundle/Command/LoadRebrickableCommand.php +++ b/src/AppBundle/Command/LoadRebrickableCommand.php @@ -21,14 +21,16 @@ class LoadRebrickableCommand extends ContainerAwareCommand { $rebrickableLoader = $this->getContainer()->get('loader.rebrickable'); + $rebrickableLoader->setOutput($output); + try { $rebrickableLoader->loadColors(); - $rebrickableLoader->loadParts($output); + $rebrickableLoader->loadParts(); - $rebrickableLoader->loadBuildingKits($output); + $rebrickableLoader->loadBuildingKits(); - $rebrickableLoader->loadPartBuildingKits($output); + $rebrickableLoader->loadPartBuildingKits(); } catch (\Exception $e) { printf($e->getMessage()); } diff --git a/src/AppBundle/Service/LDrawLoader.php b/src/AppBundle/Loader/LDrawLoader.php similarity index 56% rename from src/AppBundle/Service/LDrawLoader.php rename to src/AppBundle/Loader/LDrawLoader.php index 9f5a45c..ffdbb5e 100644 --- a/src/AppBundle/Service/LDrawLoader.php +++ b/src/AppBundle/Loader/LDrawLoader.php @@ -1,38 +1,39 @@ em = $em; @@ -40,17 +41,26 @@ class LDrawLoader $this->dataPath = $dataPath; } - // LDraw public function loadModels($LDrawLibrary) { + $adapter = new Local(getcwd().DIRECTORY_SEPARATOR.$LDrawLibrary); + $this->ldraw = new Filesystem($adapter); +// $files = $this->ldraw->get('parts')->getContents(); + $finder = new Finder(); - $files = $finder->files()->name('*.dat')->depth('== 0')->in(getcwd().DIRECTORY_SEPARATOR.$LDrawLibrary.'/parts'); - - $this->ldraw = getcwd().DIRECTORY_SEPARATOR.$LDrawLibrary; + $files = $finder->files()->name('*.dat')->depth('== 0')->in(getcwd().DIRECTORY_SEPARATOR.$LDrawLibrary.DIRECTORY_SEPARATOR.'parts'); + $progressBar = new ProgressBar($this->output, $files->count()); + $progressBar->setFormat('very_verbose'); + $progressBar->setMessage('Loading LDraw library models'); + $progressBar->setFormat('%message:6s% %current%/%max% [%bar%]%percent:3s%% (%elapsed:6s%/%estimated:-6s%)'); + $progressBar->start(); foreach ($files as $file) { $this->loadModelHeader($file); + $this->createStlFile($file); + $progressBar->advance(); } + $progressBar->finish(); } private function loadModelHeader(SplFileInfo $fileInfo) @@ -82,29 +92,6 @@ class LDrawLoader $model->setAuthor(explode('Author: ', $line)[1]); } } - - $builder = new ProcessBuilder(); - $process = $builder - ->setPrefix($this->ldview) - ->setArguments([ - $fileInfo->getRealPath(), - '-ExportFiles=1', - '-LDrawDir='.$this->ldraw, - '-ExportSuffix=.stl', - '-ExportsDir='.$this->dataPath, - ]) - ->getProcess(); - - - $process->run(); - - - if (!$process->isSuccessful()) { - throw new ProcessFailedException($process); - } else { - var_dump($process->getOutput()); - } - // $this->em->persist($model); // $this->em->flush(); } else { @@ -112,4 +99,26 @@ class LDrawLoader } fclose($handle); } + + private function createStlFile(SplFileInfo $file) + { + $builder = new ProcessBuilder(); + $process = $builder + ->setPrefix($this->ldview) + ->setArguments([ +// $this->ldraw->getAdapter()->getPathPrefix().$file['path'], + $file->getRealPath(), + '-ExportFiles=1', + '-LDrawDir='.$this->ldraw->getAdapter()->getPathPrefix(), + '-ExportSuffix=.stl', + '-ExportsDir='.$this->dataPath->getAdapter()->getPathPrefix(), + ]) + ->getProcess(); + + $process->run(); + + if (!$process->isSuccessful() || !$this->dataPath->has(str_replace('.dat','.stl',$file->getFilename()))) { + throw new ProcessFailedException($process); //TODO + } + } } diff --git a/src/AppBundle/Loader/Loader.php b/src/AppBundle/Loader/Loader.php new file mode 100644 index 0000000..269193d --- /dev/null +++ b/src/AppBundle/Loader/Loader.php @@ -0,0 +1,23 @@ +output = $output; + $this->output->setDecorated(true); + } +} \ No newline at end of file diff --git a/src/AppBundle/Service/RebrickableLoader.php b/src/AppBundle/Loader/RebrickableLoader.php similarity index 93% rename from src/AppBundle/Service/RebrickableLoader.php rename to src/AppBundle/Loader/RebrickableLoader.php index 4e9855c..428272a 100644 --- a/src/AppBundle/Service/RebrickableLoader.php +++ b/src/AppBundle/Loader/RebrickableLoader.php @@ -1,6 +1,6 @@ em = $em; $this->rebrickableManager = $rebrickableManager; } - public function loadPartBuildingKits($output) + public function loadPartBuildingKits() { $partRepository = $this->em->getRepository('AppBundle:Part'); $buldingKitRepository = $this->em->getRepository('AppBundle:BuildingKit'); @@ -48,7 +46,7 @@ class RebrickableLoader $header = fgetcsv($handle, 200, ','); // create a new progress bar (50 units) - $progress = new ProgressBar($output, intval(exec("wc -l '$setPieces'"))); + $progress = new ProgressBar($this->output, intval(exec("wc -l '$setPieces'"))); $progress->setFormat('very_verbose'); $progress->setBarWidth(50); $progress->start(); @@ -90,7 +88,7 @@ class RebrickableLoader unlink($setPieces); } - public function loadBuildingKits($output) + public function loadBuildingKits() { $keywordRepository = $this->em->getRepository('AppBundle:Keyword'); @@ -103,7 +101,7 @@ class RebrickableLoader $header = fgetcsv($handle, 500, ','); // create a new progress bar (50 units) - $progress = new ProgressBar($output, intval(exec("wc -l '$sets'"))); + $progress = new ProgressBar($this->output, intval(exec("wc -l '$sets'"))); $progress->setFormat('very_verbose'); $progress->setBarWidth(50); $progress->start(); @@ -151,7 +149,7 @@ class RebrickableLoader unlink($sets); } - public function loadParts($output) + public function loadParts() { $pieces = tempnam(sys_get_temp_dir(), 'printabrick.'); file_put_contents($pieces, fopen('compress.zlib://http://rebrickable.com/files/pieces.csv.gz', 'r')); @@ -163,7 +161,7 @@ class RebrickableLoader if (($handle = fopen($pieces, 'r')) !== false) { // create a new progress bar (50 units) - $progress = new ProgressBar($output, intval(exec("wc -l '$pieces'"))); + $progress = new ProgressBar($this->output, intval(exec("wc -l '$pieces'"))); $progress->setFormat('very_verbose'); $progress->setBarWidth(50); $progress->start();