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:
parent
81094348bf
commit
b6295d34ea
@ -6,11 +6,12 @@ use AppBundle\Entity\LDraw\Category;
|
||||
use AppBundle\Entity\LDraw\Keyword;
|
||||
use AppBundle\Entity\LDraw\Model;
|
||||
use AppBundle\Entity\LDraw\Part;
|
||||
use AppBundle\Entity\LDraw\Part_Relation;
|
||||
use AppBundle\Entity\LDraw\Type;
|
||||
use AppBundle\Service\LDrawService;
|
||||
use AppBundle\Service\LDViewService;
|
||||
use League\Flysystem\Adapter\Local;
|
||||
use League\Flysystem\Filesystem;
|
||||
//use Symfony\Component\Asset\Exception\LogicException;
|
||||
use Symfony\Component\Asset\Exception\LogicException;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
|
||||
@ -32,13 +33,17 @@ class LDrawLoader extends Loader
|
||||
*/
|
||||
private $LDViewService;
|
||||
|
||||
/** @var LDrawService */
|
||||
private $ldrawService;
|
||||
|
||||
/**
|
||||
* @param array $ldraw_url
|
||||
*/
|
||||
public function setArguments(LDViewService $LDViewService, $ldraw_url)
|
||||
public function setArguments(LDViewService $LDViewService, $ldraw_url, LDrawService $ldrawService)
|
||||
{
|
||||
$this->LDViewService = $LDViewService;
|
||||
$this->ldraw_url = $ldraw_url;
|
||||
$this->ldrawService = $ldrawService;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,19 +53,21 @@ class LDrawLoader extends Loader
|
||||
*/
|
||||
public function downloadLibrary()
|
||||
{
|
||||
$this->output->writeln('Downloading LDraw library form ldraw.org');
|
||||
$temp = $this->downloadFile($this->ldraw_url);
|
||||
|
||||
// Create unique temporary directory
|
||||
$temp_dir = tempnam(sys_get_temp_dir(), 'printabrick.');
|
||||
if (file_exists($temp_dir)) {
|
||||
unlink($temp_dir);
|
||||
}
|
||||
mkdir($temp_dir);
|
||||
|
||||
// Unzip downloaded library zip file to temporary directory
|
||||
$zip = new \ZipArchive();
|
||||
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->close();
|
||||
|
||||
// Unlink downloaded zip file
|
||||
unlink($temp);
|
||||
|
||||
return $temp_dir;
|
||||
@ -91,7 +98,7 @@ class LDrawLoader extends Loader
|
||||
{
|
||||
if (($model = $this->em->getRepository(Model::class)->find($header['id'])) == null) {
|
||||
$model = new Model();
|
||||
$model->setId($header['id']);
|
||||
$model->setNumber($header['id']);
|
||||
}
|
||||
|
||||
$model->setAuthor($header['author']);
|
||||
@ -110,47 +117,29 @@ class LDrawLoader extends Loader
|
||||
// TODO refactor
|
||||
public function loadParts()
|
||||
{
|
||||
$partManager = $this->ldrawService->getPartManager();
|
||||
$relationManager = $this->ldrawService->getPartRelationManager();
|
||||
|
||||
$files = $this->ldraw->get('parts')->getContents();
|
||||
|
||||
$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
|
||||
|
||||
$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();
|
||||
$this->initProgressBar(count($files));
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file['type'] == 'file' && $file['extension'] == 'dat') {
|
||||
$header = $this->getPartHeader($file);
|
||||
|
||||
if ($this->isPartIncluded($header)) {
|
||||
if (null == ($part = $this->em->getRepository(Part::class)->find($header['id']))) {
|
||||
$part = new Part();
|
||||
$part->setId($header['id']);
|
||||
}
|
||||
$part = $partManager->create($header['id']);
|
||||
|
||||
$part->setName($header['name']);
|
||||
|
||||
if (($category = $this->em->getRepository(Category::class)->findOneBy(['name' => $header['category']])) == null) {
|
||||
$category = new Category();
|
||||
$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);
|
||||
$part
|
||||
->setCategory($this->ldrawService->getCategoryManager()->createCategory($header['category']))
|
||||
->setType($this->ldrawService->getTypeManager()->create($header['type']));
|
||||
|
||||
if (isset($header['keywords'])) {
|
||||
foreach ($header['keywords'] as $kword) {
|
||||
$kword = trim($kword);
|
||||
if (($keyword = $this->em->getRepository(Keyword::class)->findOneBy(['name' => $kword])) == null) {
|
||||
$keyword = new Keyword();
|
||||
$keyword->setName($kword);
|
||||
}
|
||||
$part->addKeyword($keyword);
|
||||
foreach ($header['keywords'] as $keyword) {
|
||||
$keyword = stripslashes(strtolower(trim($keyword)));
|
||||
$part->addKeyword($this->ldrawService->getKeywordManager()->createKeyword($keyword));
|
||||
}
|
||||
}
|
||||
|
||||
@ -168,12 +157,7 @@ class LDrawLoader extends Loader
|
||||
foreach ($header['subparts'] as $referenceId) {
|
||||
if ($referenceId != $this->getPrintedParentId($header['id'])) {
|
||||
if ($this->getModel($referenceId) && $this->isPartIncluded($this->getPartHeader($this->getModel($referenceId)->getMetadata()))) {
|
||||
if (($referencedPart = $this->em->getRepository(Part::class)->find($referenceId)) == null) {
|
||||
$referencedPart = new Part();
|
||||
$referencedPart->setId($referenceId);
|
||||
|
||||
$this->em->persist($referencedPart);
|
||||
}
|
||||
$referencedPart = $this->ldrawService->getPartManager()->create($referenceId);
|
||||
|
||||
if ($relationType == 'Alias') {
|
||||
$parent = $referencedPart;
|
||||
@ -183,17 +167,10 @@ class LDrawLoader extends Loader
|
||||
$child = $referencedPart;
|
||||
}
|
||||
|
||||
if (($alias = $this->em->getRepository(Part_Relation::class)->find(['parent' => $parent, 'child' => $child, 'type' => $relationType])) == null) {
|
||||
$alias = new Part_Relation();
|
||||
$alias->setParent($parent);
|
||||
$alias->setChild($child);
|
||||
$alias->setCount(0);
|
||||
$alias->setType($relationType);
|
||||
}
|
||||
$partRelation = $relationManager->create($parent, $child, $relationType);
|
||||
$partRelation->setCount($partRelation->getCount() + 1);
|
||||
|
||||
$alias->setCount($alias->getCount() + 1);
|
||||
|
||||
$this->em->persist($alias);
|
||||
$relationManager->getRepository()->save($partRelation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -203,20 +180,18 @@ class LDrawLoader extends Loader
|
||||
$part->setModel($this->loadModel($file, $header));
|
||||
}
|
||||
|
||||
try {
|
||||
$this->LDViewService->datToPng($file, $this->ldraw);
|
||||
} catch (\Exception $e) {
|
||||
dump($e->getMessage());
|
||||
}
|
||||
// try {
|
||||
// $this->LDViewService->datToPng($file, $this->ldraw);
|
||||
// } catch (\Exception $e) {
|
||||
// dump($e->getMessage());
|
||||
// }
|
||||
|
||||
$this->em->persist($part);
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
$partManager->getRepository()->save($part);
|
||||
}
|
||||
}
|
||||
$progressBar->advance();
|
||||
$this->progressBar->advance();
|
||||
}
|
||||
$progressBar->finish();
|
||||
$this->progressBar->finish();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -276,10 +251,8 @@ class LDrawLoader extends Loader
|
||||
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:
|
||||
* nnnDnn, nnnnDnn, nnnnnDnn (a = alpha, n= numeric, x = alphanumeric)
|
||||
@ -317,10 +290,12 @@ class LDrawLoader extends Loader
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getModel($id) {
|
||||
private function getModel($id)
|
||||
{
|
||||
if ($this->ldraw->has('parts/'.$id.'.dat')) {
|
||||
return $this->ldraw->get('parts/'.$id.'.dat');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -397,9 +372,10 @@ class LDrawLoader extends Loader
|
||||
}
|
||||
// 0 Name: <Filename>.dat
|
||||
elseif (strpos($line, 'Name: ') === 0) {
|
||||
if(!isset($header['id']))
|
||||
if (!isset($header['id'])) {
|
||||
$header['id'] = preg_replace('/(^Name: )(.*)(.dat)/', '$2', $line);
|
||||
}
|
||||
}
|
||||
// 0 Author: <Realname> [<Username>]
|
||||
elseif (strpos($line, 'Author: ') === 0) {
|
||||
$header['author'] = preg_replace('/^Author: /', '', $line);
|
||||
|
@ -33,6 +33,7 @@ abstract class Loader
|
||||
public function __construct(EntityManager $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
|
||||
}
|
||||
|
||||
public function setOutput(OutputInterface $output)
|
||||
@ -41,13 +42,19 @@ abstract class Loader
|
||||
$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)
|
||||
{
|
||||
switch ($notification_code) {
|
||||
case STREAM_NOTIFY_FILE_SIZE_IS:
|
||||
$this->progressBar = new ProgressBar($this->output);
|
||||
$this->progressBar->setBarWidth(100);
|
||||
$this->progressBar->start($bytes_max);
|
||||
$this->initProgressBar($bytes_max);
|
||||
break;
|
||||
case STREAM_NOTIFY_PROGRESS:
|
||||
$this->progressBar->setProgress($bytes_transferred);
|
||||
@ -61,6 +68,7 @@ abstract class Loader
|
||||
|
||||
protected function downloadFile($url)
|
||||
{
|
||||
$this->output->writeln('Downloading file from: <info>'.$url.'</info>');
|
||||
$temp = tempnam(sys_get_temp_dir(), 'printabrick.');
|
||||
|
||||
$ctx = stream_context_create([], [
|
||||
|
@ -2,25 +2,18 @@
|
||||
|
||||
namespace AppBundle\Loader;
|
||||
|
||||
use AppBundle\Api\Manager\RebrickableManager;
|
||||
use AppBundle\Entity\Rebrickable\Set;
|
||||
|
||||
//TODO Refactor
|
||||
class RebrickableLoader extends Loader
|
||||
{
|
||||
/**
|
||||
* @var RebrickableManager
|
||||
*/
|
||||
private $rebrickableManager;
|
||||
|
||||
private $rebrickable_url;
|
||||
|
||||
/**
|
||||
* ModelLoaderService constructor.
|
||||
*/
|
||||
public function setArguments($rebrickableManager, $rebrickable_url)
|
||||
public function setArguments($rebrickable_url)
|
||||
{
|
||||
$this->rebrickableManager = $rebrickableManager;
|
||||
$this->rebrickable_url = $rebrickable_url;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user