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

Move Rebrickable loader

This commit is contained in:
David Hübner 2017-01-14 16:03:21 +01:00
parent 065045dce7
commit 99972a20bc
3 changed files with 12 additions and 71 deletions

View File

@ -16,9 +16,10 @@ services:
app.collection_service:
class: AppBundle\Service\CollectionService
arguments: ['@doctrine.orm.entity_manager', '@manager.brickset','@manager.rebrickable']
app.model_loader_service:
class: AppBundle\Service\ModelLoaderService
arguments: ['@doctrine.orm.entity_manager','%kernel.root_dir%/../var/data/LDrawLibrary', '@manager.rebrickable']
loader.rebrickable:
class: AppBundle\Service\RebrickableLoader
arguments: ['@doctrine.orm.entity_manager', '@manager.rebrickable']
app.form.filter_set:
class: AppBundle\Form\FilterSetType

View File

@ -11,7 +11,7 @@ class LoadRebrickableCommand extends ContainerAwareCommand
protected function configure()
{
$this
->setName('app:loadRebrickable')
->setName('app:load:rebrickable')
->setDescription('Loads Rebrickable csv data')
->setHelp('This command allows you to..')
;
@ -19,16 +19,16 @@ class LoadRebrickableCommand extends ContainerAwareCommand
protected function execute(InputInterface $input, OutputInterface $output)
{
$modelLoader = $this->getContainer()->get('app.model_loader_service');
$rebrickableLoader = $this->getContainer()->get('loader.rebrickable');
try {
$modelLoader->loadColors();
$rebrickableLoader->loadColors();
$modelLoader->loadParts($output);
$rebrickableLoader->loadParts($output);
$modelLoader->loadBuildingKits($output);
$rebrickableLoader->loadBuildingKits($output);
$modelLoader->loadPartBuildingKits($output);
$rebrickableLoader->loadPartBuildingKits($output);
} catch (\Exception $e) {
printf($e->getMessage());
}

View File

@ -6,21 +6,14 @@ use AppBundle\Api\Manager\RebrickableManager;
use AppBundle\Entity\Category;
use AppBundle\Entity\Color;
use AppBundle\Entity\Keyword;
use AppBundle\Entity\Model;
use AppBundle\Entity\BuildingKit;
use AppBundle\Entity\Part;
use AppBundle\Entity\Part_BuildingKit;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class ModelLoaderService
class RebrickableLoader
{
private $STLlib;
/**
* @var EntityManager
*/
@ -34,65 +27,12 @@ class ModelLoaderService
/**
* ModelLoaderService constructor.
*/
public function __construct($em, $STLlib, $rebrickableManager)
public function __construct($em, $rebrickableManager)
{
$this->STLlib = $STLlib;
$this->em = $em;
$this->rebrickableManager = $rebrickableManager;
}
// LDraw
public function loadModels($LDrawLibrary)
{
$finder = new Finder();
$files = $finder->files()->name('*.dat')->depth('== 0')->in(getcwd().'/'.$LDrawLibrary.'/parts');
foreach ($files as $file) {
$this->loadModelHeader($file);
}
}
private function loadModelHeader(SplFileInfo $fileInfo)
{
$handle = fopen($fileInfo->getRealPath(), 'r');
if ($handle) {
$firstLine = fgets($handle);
$description = trim(substr($firstLine, 2));
$model = new Model();
$model->setFile($fileInfo->getFilename());
$p['category'] = explode(' ', trim($description))[0];
//TODO handle ~Moved to
while (!feof($handle)) {
$line = trim(fgets($handle));
if ($line && ($line[0] == '1')) {
break;
} 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: '))) {
$model->setNumber(trim(explode('.dat', explode('Name: ', $line)[1])[0]));
} elseif ($line && ($line[0] == '0' && strpos($line, 'Author: '))) {
$model->setAuthor(explode('Author: ', $line)[1]);
}
}
$this->em->persist($model);
$this->em->flush();
} else {
throw new LogicException('loadHeader error'); //TODO
}
fclose($handle);
}
// Rebrickable
public function loadPartBuildingKits($output)
{
$partRepository = $this->em->getRepository('AppBundle:Part');