mirror of
https://github.com/ToxicCrack/PrintABrick.git
synced 2025-05-18 21:20:09 -07:00
Persist Models
This commit is contained in:
parent
27210e650c
commit
4138fefded
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace AppBundle\Loader;
|
namespace AppBundle\Loader;
|
||||||
|
|
||||||
|
use AppBundle\Entity\Category;
|
||||||
use AppBundle\Entity\Model;
|
use AppBundle\Entity\Model;
|
||||||
use League\Flysystem\Adapter\Local;
|
use League\Flysystem\Adapter\Local;
|
||||||
use League\Flysystem\Filesystem;
|
use League\Flysystem\Filesystem;
|
||||||
@ -31,6 +32,9 @@ class LDrawLoader extends Loader
|
|||||||
|
|
||||||
public function __construct($em, $ldview, $dataPath)
|
public function __construct($em, $ldview, $dataPath)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* @var $em EntityManager
|
||||||
|
* */
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->ldview = $ldview;
|
$this->ldview = $ldview;
|
||||||
$this->dataPath = $dataPath;
|
$this->dataPath = $dataPath;
|
||||||
@ -38,6 +42,7 @@ class LDrawLoader extends Loader
|
|||||||
|
|
||||||
public function loadModels($LDrawLibrary)
|
public function loadModels($LDrawLibrary)
|
||||||
{
|
{
|
||||||
|
//TODO Refactor, use flysystem
|
||||||
$adapter = new Local(getcwd().DIRECTORY_SEPARATOR.$LDrawLibrary);
|
$adapter = new Local(getcwd().DIRECTORY_SEPARATOR.$LDrawLibrary);
|
||||||
$this->ldraw = new Filesystem($adapter);
|
$this->ldraw = new Filesystem($adapter);
|
||||||
// $files = $this->ldraw->get('parts')->getContents();
|
// $files = $this->ldraw->get('parts')->getContents();
|
||||||
@ -51,51 +56,83 @@ class LDrawLoader extends Loader
|
|||||||
$progressBar->setFormat('%message:6s% %current%/%max% [%bar%]%percent:3s%% (%elapsed:6s%/%estimated:-6s%)');
|
$progressBar->setFormat('%message:6s% %current%/%max% [%bar%]%percent:3s%% (%elapsed:6s%/%estimated:-6s%)');
|
||||||
$progressBar->start();
|
$progressBar->start();
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
$this->loadModelHeader($file);
|
$model = $this->loadPartHeader($file);
|
||||||
$this->createStlFile($file);
|
$model->setFile($this->createStlFile($file)->getPath());
|
||||||
|
|
||||||
|
$this->em->persist($model);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
$progressBar->advance();
|
$progressBar->advance();
|
||||||
}
|
}
|
||||||
$progressBar->finish();
|
$progressBar->finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function loadModelHeader(SplFileInfo $fileInfo)
|
/**
|
||||||
|
* @param SplFileInfo $file
|
||||||
|
*
|
||||||
|
* @return Model
|
||||||
|
*/
|
||||||
|
private function loadPartHeader($file)
|
||||||
{
|
{
|
||||||
$handle = fopen($fileInfo->getRealPath(), 'r');
|
$handle = fopen($file->getRealPath(), 'r');
|
||||||
if ($handle) {
|
if ($handle) {
|
||||||
$firstLine = fgets($handle);
|
$firstLine = false;
|
||||||
$description = trim(substr($firstLine, 2));
|
|
||||||
$model = new Model();
|
$model = new Model();
|
||||||
$model->setFile($fileInfo->getFilename());
|
|
||||||
$p['category'] = explode(' ', trim($description))[0];
|
|
||||||
|
|
||||||
//TODO handle ~Moved to
|
// read lines while line starts with 0 or is empty
|
||||||
|
while (($line = trim(fgets($handle))) !== false && ($line ? $line[0] == '0' : true)) {
|
||||||
|
if ($line !== '') {
|
||||||
|
$line = preg_replace('/^0 /', '', $line);
|
||||||
|
|
||||||
while (!feof($handle)) {
|
// 0 <PartDescription>
|
||||||
$line = trim(fgets($handle));
|
if (!$firstLine) { //TODO handle ~Moved to
|
||||||
if ($line && ($line[0] == '1')) {
|
$category = explode(' ', trim($line))[0];
|
||||||
break;
|
$firstLine = true;
|
||||||
} elseif ($line && ($line[0] == '0' && strpos($line, '!CATEGORY '))) {
|
|
||||||
$p['category'] = trim(explode('!CATEGORY ', $line)[1]);
|
|
||||||
} elseif ($line && ($line[0] == '0' && strpos($line, '!KEYWORDS '))) {
|
|
||||||
$keywords = explode(',', explode('!KEYWORDS ', $line)[1]);
|
|
||||||
foreach ($keywords as $k) {
|
|
||||||
$p['keywords'][] = trim($k);
|
|
||||||
}
|
}
|
||||||
} elseif ($line && ($line[0] == '0' && strpos($line, 'Name: '))) {
|
// 0 !CATEGORY <CategoryName>
|
||||||
$model->setNumber(trim(explode('.dat', explode('Name: ', $line)[1])[0]));
|
elseif (strpos($line, '!CATEGORY ') === 0) {
|
||||||
} elseif ($line && ($line[0] == '0' && strpos($line, 'Author: '))) {
|
$category = trim(preg_replace('/^!CATEGORY /', '', $line));
|
||||||
$model->setAuthor(explode('Author: ', $line)[1]);
|
}
|
||||||
|
// 0 !KEYWORDS <first keyword>, <second keyword>, ..., <last keyword>
|
||||||
|
elseif (strpos($line, '!KEYWORDS ') === 0) {
|
||||||
|
$keywords = explode(', ', preg_replace('/^!KEYWORDS /', '', $line));
|
||||||
|
}
|
||||||
|
// 0 Name: <Filename>.dat
|
||||||
|
elseif (strpos($line, 'Name: ') === 0) {
|
||||||
|
$model->setNumber(preg_replace('/(^Name: )(.*)(.dat)/', '$2', $line));
|
||||||
|
}
|
||||||
|
// 0 Author: <Realname> [<Username>]
|
||||||
|
elseif (strpos($line, 'Author: ') === 0) {
|
||||||
|
$model->setAuthor(preg_replace('/^Author: /', '', $line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// $this->em->persist($model);
|
}
|
||||||
// $this->em->flush();
|
|
||||||
|
$cat = $this->em->getRepository('AppBundle:Category')->findOneBy(['name' => $category]);
|
||||||
|
if ($cat == null) {
|
||||||
|
$cat = new Category();
|
||||||
|
$cat->setName($category);
|
||||||
|
$this->em->persist($cat);
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
$model->setCategory($cat);
|
||||||
|
$cat->addModel($model);
|
||||||
} else {
|
} else {
|
||||||
throw new LogicException('loadHeader error'); //TODO
|
throw new LogicException('loadHeader error'); //TODO
|
||||||
}
|
}
|
||||||
fclose($handle);
|
fclose($handle);
|
||||||
|
|
||||||
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createStlFile(SplFileInfo $file)
|
/**
|
||||||
|
* @param SplFileInfo $file
|
||||||
|
*
|
||||||
|
* @return \League\Flysystem\File
|
||||||
|
*/
|
||||||
|
private function createStlFile($file)
|
||||||
{
|
{
|
||||||
$builder = new ProcessBuilder();
|
$builder = new ProcessBuilder();
|
||||||
$process = $builder
|
$process = $builder
|
||||||
@ -112,8 +149,12 @@ class LDrawLoader extends Loader
|
|||||||
|
|
||||||
$process->run();
|
$process->run();
|
||||||
|
|
||||||
if (!$process->isSuccessful() || !$this->dataPath->has(str_replace('.dat', '.stl', $file->getFilename()))) {
|
$stlFilename = str_replace('.dat', '.stl', $file->getFilename());
|
||||||
|
|
||||||
|
if (!$process->isSuccessful() || !$this->dataPath->has($stlFilename)) {
|
||||||
throw new ProcessFailedException($process); //TODO
|
throw new ProcessFailedException($process); //TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->dataPath->get($stlFilename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ use AppBundle\Entity\Part_BuildingKit;
|
|||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
use Symfony\Component\Console\Helper\ProgressBar;
|
use Symfony\Component\Console\Helper\ProgressBar;
|
||||||
|
|
||||||
|
//TODO Refactor
|
||||||
class RebrickableLoader extends Loader
|
class RebrickableLoader extends Loader
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -46,7 +47,7 @@ class RebrickableLoader extends Loader
|
|||||||
$header = fgetcsv($handle, 200, ',');
|
$header = fgetcsv($handle, 200, ',');
|
||||||
|
|
||||||
// create a new progress bar (50 units)
|
// create a new progress bar (50 units)
|
||||||
$progress = new ProgressBar($this->output, intval(exec("wc -l '$setPieces'")));
|
$progress = new ProgressBar($this->output, intval(exec("wc -l '$setPieces'"))); //TODO replace wc-l
|
||||||
$progress->setFormat('very_verbose');
|
$progress->setFormat('very_verbose');
|
||||||
$progress->setBarWidth(50);
|
$progress->setBarWidth(50);
|
||||||
$progress->start();
|
$progress->start();
|
||||||
@ -101,7 +102,7 @@ class RebrickableLoader extends Loader
|
|||||||
$header = fgetcsv($handle, 500, ',');
|
$header = fgetcsv($handle, 500, ',');
|
||||||
|
|
||||||
// create a new progress bar (50 units)
|
// create a new progress bar (50 units)
|
||||||
$progress = new ProgressBar($this->output, intval(exec("wc -l '$sets'")));
|
$progress = new ProgressBar($this->output, intval(exec("wc -l '$sets'"))); //TODO replace wc-l
|
||||||
$progress->setFormat('very_verbose');
|
$progress->setFormat('very_verbose');
|
||||||
$progress->setBarWidth(50);
|
$progress->setBarWidth(50);
|
||||||
$progress->start();
|
$progress->start();
|
||||||
@ -152,7 +153,7 @@ class RebrickableLoader extends Loader
|
|||||||
public function loadParts()
|
public function loadParts()
|
||||||
{
|
{
|
||||||
$pieces = tempnam(sys_get_temp_dir(), 'printabrick.');
|
$pieces = tempnam(sys_get_temp_dir(), 'printabrick.');
|
||||||
file_put_contents($pieces, fopen('compress.zlib://http://rebrickable.com/files/pieces.csv.gz', 'r'));
|
file_put_contents($pieces, fopen('compress.zlib://http://rebrickable.com/files/pieces.csv.gz', 'r')); //TODO replace wc-l
|
||||||
|
|
||||||
$categoryRepository = $this->em->getRepository('AppBundle:Category');
|
$categoryRepository = $this->em->getRepository('AppBundle:Category');
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user