1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-16 04:10:09 -07:00

Move loaders to own namespace

This commit is contained in:
David Hübner 2017-01-17 00:30:39 +01:00
parent b06c8b1bcb
commit dcb475407b
6 changed files with 93 additions and 57 deletions

View File

@ -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

View File

@ -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'));
}
}

View File

@ -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());
}

View File

@ -1,38 +1,39 @@
<?php
namespace AppBundle\Service;
namespace AppBundle\Loader;
use AppBundle\Entity\Model;
use Doctrine\ORM\EntityManager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Adapter\NullAdapter;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem;
use Oneup\FlysystemBundle\OneupFlysystemBundle;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;
class LDrawLoader
class LDrawLoader extends Loader
{
/**
* @var EntityManager
*/
private $em;
/**
* @var string LDView binary file path
*/
private $ldview;
/**
* @var string LDraw library root path
* @var $ldraw Filesystem
*/
private $ldraw;
/**
* @var string
* @var \League\Flysystem\Filesystem
*/
private $dataPath;
public function __construct($em, $ldview, $dataPath)
{
$this->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
}
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace AppBundle\Loader;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
class Loader
{
/**
* @var EntityManager
*/
protected $em;
protected $output;
public function setOutput(OutputInterface $output) {
$this->output = $output;
$this->output->setDecorated(true);
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace AppBundle\Service;
namespace AppBundle\Loader;
use AppBundle\Api\Manager\RebrickableManager;
use AppBundle\Entity\Category;
@ -12,13 +12,8 @@ use AppBundle\Entity\Part_BuildingKit;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Helper\ProgressBar;
class RebrickableLoader
class RebrickableLoader extends Loader
{
/**
* @var EntityManager
*/
private $em;
/**
* @var RebrickableManager
*/
@ -29,11 +24,14 @@ class RebrickableLoader
*/
public function __construct($em, $rebrickableManager)
{
/**
* @var $em EntityManager
* */
$this->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();