1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-17 04:40:08 -07:00

Refactor data loaders

This commit is contained in:
David Hübner 2017-03-27 18:25:49 +02:00
parent 81094348bf
commit b6295d34ea
3 changed files with 58 additions and 81 deletions

View File

@ -6,11 +6,12 @@ use AppBundle\Entity\LDraw\Category;
use AppBundle\Entity\LDraw\Keyword; use AppBundle\Entity\LDraw\Keyword;
use AppBundle\Entity\LDraw\Model; use AppBundle\Entity\LDraw\Model;
use AppBundle\Entity\LDraw\Part; use AppBundle\Entity\LDraw\Part;
use AppBundle\Entity\LDraw\Part_Relation;
use AppBundle\Entity\LDraw\Type; use AppBundle\Entity\LDraw\Type;
use AppBundle\Service\LDrawService;
use AppBundle\Service\LDViewService; use AppBundle\Service\LDViewService;
use League\Flysystem\Adapter\Local; use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem; use League\Flysystem\Filesystem;
//use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Asset\Exception\LogicException; use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBar;
@ -32,13 +33,17 @@ class LDrawLoader extends Loader
*/ */
private $LDViewService; private $LDViewService;
/** @var LDrawService */
private $ldrawService;
/** /**
* @param array $ldraw_url * @param array $ldraw_url
*/ */
public function setArguments(LDViewService $LDViewService, $ldraw_url) public function setArguments(LDViewService $LDViewService, $ldraw_url, LDrawService $ldrawService)
{ {
$this->LDViewService = $LDViewService; $this->LDViewService = $LDViewService;
$this->ldraw_url = $ldraw_url; $this->ldraw_url = $ldraw_url;
$this->ldrawService = $ldrawService;
} }
/** /**
@ -48,19 +53,21 @@ class LDrawLoader extends Loader
*/ */
public function downloadLibrary() public function downloadLibrary()
{ {
$this->output->writeln('Downloading LDraw library form ldraw.org');
$temp = $this->downloadFile($this->ldraw_url); $temp = $this->downloadFile($this->ldraw_url);
// Create unique temporary directory
$temp_dir = tempnam(sys_get_temp_dir(), 'printabrick.'); $temp_dir = tempnam(sys_get_temp_dir(), 'printabrick.');
if (file_exists($temp_dir)) {
unlink($temp_dir);
}
mkdir($temp_dir); mkdir($temp_dir);
// Unzip downloaded library zip file to temporary directory
$zip = new \ZipArchive(); $zip = new \ZipArchive();
if ($zip->open($temp) != 'true') { if ($zip->open($temp) != 'true') {
echo 'Error :- Unable to open the Zip File'; throw new LogicException('Error :- Unable to open the Zip File');
} }
$zip->extractTo($temp_dir); $zip->extractTo($temp_dir);
$zip->close(); $zip->close();
// Unlink downloaded zip file
unlink($temp); unlink($temp);
return $temp_dir; return $temp_dir;
@ -91,7 +98,7 @@ class LDrawLoader extends Loader
{ {
if (($model = $this->em->getRepository(Model::class)->find($header['id'])) == null) { if (($model = $this->em->getRepository(Model::class)->find($header['id'])) == null) {
$model = new Model(); $model = new Model();
$model->setId($header['id']); $model->setNumber($header['id']);
} }
$model->setAuthor($header['author']); $model->setAuthor($header['author']);
@ -110,47 +117,29 @@ class LDrawLoader extends Loader
// TODO refactor // TODO refactor
public function loadParts() public function loadParts()
{ {
$partManager = $this->ldrawService->getPartManager();
$relationManager = $this->ldrawService->getPartRelationManager();
$files = $this->ldraw->get('parts')->getContents(); $files = $this->ldraw->get('parts')->getContents();
$this->em->getConnection()->getConfiguration()->setSQLLogger(null); $this->initProgressBar(count($files));
$progressBar = new ProgressBar($this->output, count($files));
$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) { foreach ($files as $file) {
if ($file['type'] == 'file' && $file['extension'] == 'dat') { if ($file['type'] == 'file' && $file['extension'] == 'dat') {
$header = $this->getPartHeader($file); $header = $this->getPartHeader($file);
if ($this->isPartIncluded($header)) { if ($this->isPartIncluded($header)) {
if (null == ($part = $this->em->getRepository(Part::class)->find($header['id']))) { $part = $partManager->create($header['id']);
$part = new Part();
$part->setId($header['id']);
}
$part->setName($header['name']); $part->setName($header['name']);
$part
if (($category = $this->em->getRepository(Category::class)->findOneBy(['name' => $header['category']])) == null) { ->setCategory($this->ldrawService->getCategoryManager()->createCategory($header['category']))
$category = new Category(); ->setType($this->ldrawService->getTypeManager()->create($header['type']));
$category->setName($header['category']);
}
$part->setCategory($category);
if (($type = $this->em->getRepository(Type::class)->findOneBy(['name' => $header['type']])) == null) {
$type = new Type();
$type->setName($header['type']);
}
$part->setType($type);
if (isset($header['keywords'])) { if (isset($header['keywords'])) {
foreach ($header['keywords'] as $kword) { foreach ($header['keywords'] as $keyword) {
$kword = trim($kword); $keyword = stripslashes(strtolower(trim($keyword)));
if (($keyword = $this->em->getRepository(Keyword::class)->findOneBy(['name' => $kword])) == null) { $part->addKeyword($this->ldrawService->getKeywordManager()->createKeyword($keyword));
$keyword = new Keyword();
$keyword->setName($kword);
}
$part->addKeyword($keyword);
} }
} }
@ -168,14 +157,9 @@ class LDrawLoader extends Loader
foreach ($header['subparts'] as $referenceId) { foreach ($header['subparts'] as $referenceId) {
if ($referenceId != $this->getPrintedParentId($header['id'])) { if ($referenceId != $this->getPrintedParentId($header['id'])) {
if ($this->getModel($referenceId) && $this->isPartIncluded($this->getPartHeader($this->getModel($referenceId)->getMetadata()))) { if ($this->getModel($referenceId) && $this->isPartIncluded($this->getPartHeader($this->getModel($referenceId)->getMetadata()))) {
if (($referencedPart = $this->em->getRepository(Part::class)->find($referenceId)) == null) { $referencedPart = $this->ldrawService->getPartManager()->create($referenceId);
$referencedPart = new Part();
$referencedPart->setId($referenceId);
$this->em->persist($referencedPart); if ($relationType == 'Alias') {
}
if($relationType == 'Alias') {
$parent = $referencedPart; $parent = $referencedPart;
$child = $part; $child = $part;
} else { } else {
@ -183,17 +167,10 @@ class LDrawLoader extends Loader
$child = $referencedPart; $child = $referencedPart;
} }
if (($alias = $this->em->getRepository(Part_Relation::class)->find(['parent' => $parent, 'child' => $child, 'type' => $relationType])) == null) { $partRelation = $relationManager->create($parent, $child, $relationType);
$alias = new Part_Relation(); $partRelation->setCount($partRelation->getCount() + 1);
$alias->setParent($parent);
$alias->setChild($child);
$alias->setCount(0);
$alias->setType($relationType);
}
$alias->setCount($alias->getCount() + 1); $relationManager->getRepository()->save($partRelation);
$this->em->persist($alias);
} }
} }
} }
@ -203,20 +180,18 @@ class LDrawLoader extends Loader
$part->setModel($this->loadModel($file, $header)); $part->setModel($this->loadModel($file, $header));
} }
try { // try {
$this->LDViewService->datToPng($file, $this->ldraw); // $this->LDViewService->datToPng($file, $this->ldraw);
} catch (\Exception $e) { // } catch (\Exception $e) {
dump($e->getMessage()); // dump($e->getMessage());
} // }
$this->em->persist($part); $partManager->getRepository()->save($part);
$this->em->flush();
$this->em->clear();
} }
} }
$progressBar->advance(); $this->progressBar->advance();
} }
$progressBar->finish(); $this->progressBar->finish();
} }
/** /**
@ -276,10 +251,8 @@ class LDrawLoader extends Loader
return $id; return $id;
} }
/** /**
* Check if part is shortcut part of stricker and part * Check if part is shortcut part of stricker and part.
* *
* part name in format: * part name in format:
* nnnDnn, nnnnDnn, nnnnnDnn (a = alpha, n= numeric, x = alphanumeric) * nnnDnn, nnnnDnn, nnnnnDnn (a = alpha, n= numeric, x = alphanumeric)
@ -317,10 +290,12 @@ class LDrawLoader extends Loader
return null; return null;
} }
private function getModel($id) { private function getModel($id)
{
if ($this->ldraw->has('parts/'.$id.'.dat')) { if ($this->ldraw->has('parts/'.$id.'.dat')) {
return $this->ldraw->get('parts/'.$id.'.dat'); return $this->ldraw->get('parts/'.$id.'.dat');
} }
return null; return null;
} }
@ -397,8 +372,9 @@ class LDrawLoader extends Loader
} }
// 0 Name: <Filename>.dat // 0 Name: <Filename>.dat
elseif (strpos($line, 'Name: ') === 0) { elseif (strpos($line, 'Name: ') === 0) {
if(!isset($header['id'])) if (!isset($header['id'])) {
$header['id'] = preg_replace('/(^Name: )(.*)(.dat)/', '$2', $line); $header['id'] = preg_replace('/(^Name: )(.*)(.dat)/', '$2', $line);
}
} }
// 0 Author: <Realname> [<Username>] // 0 Author: <Realname> [<Username>]
elseif (strpos($line, 'Author: ') === 0) { elseif (strpos($line, 'Author: ') === 0) {

View File

@ -33,6 +33,7 @@ abstract class Loader
public function __construct(EntityManager $em) public function __construct(EntityManager $em)
{ {
$this->em = $em; $this->em = $em;
$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
} }
public function setOutput(OutputInterface $output) public function setOutput(OutputInterface $output)
@ -41,13 +42,19 @@ abstract class Loader
$this->output->setDecorated(true); $this->output->setDecorated(true);
} }
protected function initProgressBar($total)
{
$this->progressBar = new ProgressBar($this->output, $total);
$this->progressBar->setFormat('very_verbose');
$this->progressBar->setFormat('%current%/%max% [%bar%]%percent:3s%% (%elapsed:6s%/%estimated:-6s%)'.PHP_EOL);
$this->progressBar->start();
}
protected function progressCallback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) protected function progressCallback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max)
{ {
switch ($notification_code) { switch ($notification_code) {
case STREAM_NOTIFY_FILE_SIZE_IS: case STREAM_NOTIFY_FILE_SIZE_IS:
$this->progressBar = new ProgressBar($this->output); $this->initProgressBar($bytes_max);
$this->progressBar->setBarWidth(100);
$this->progressBar->start($bytes_max);
break; break;
case STREAM_NOTIFY_PROGRESS: case STREAM_NOTIFY_PROGRESS:
$this->progressBar->setProgress($bytes_transferred); $this->progressBar->setProgress($bytes_transferred);
@ -61,6 +68,7 @@ abstract class Loader
protected function downloadFile($url) protected function downloadFile($url)
{ {
$this->output->writeln('Downloading file from: <info>'.$url.'</info>');
$temp = tempnam(sys_get_temp_dir(), 'printabrick.'); $temp = tempnam(sys_get_temp_dir(), 'printabrick.');
$ctx = stream_context_create([], [ $ctx = stream_context_create([], [

View File

@ -2,25 +2,18 @@
namespace AppBundle\Loader; namespace AppBundle\Loader;
use AppBundle\Api\Manager\RebrickableManager;
use AppBundle\Entity\Rebrickable\Set; use AppBundle\Entity\Rebrickable\Set;
//TODO Refactor //TODO Refactor
class RebrickableLoader extends Loader class RebrickableLoader extends Loader
{ {
/**
* @var RebrickableManager
*/
private $rebrickableManager;
private $rebrickable_url; private $rebrickable_url;
/** /**
* ModelLoaderService constructor. * ModelLoaderService constructor.
*/ */
public function setArguments($rebrickableManager, $rebrickable_url) public function setArguments($rebrickable_url)
{ {
$this->rebrickableManager = $rebrickableManager;
$this->rebrickable_url = $rebrickable_url; $this->rebrickable_url = $rebrickable_url;
} }