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:
parent
065045dce7
commit
99972a20bc
@ -16,9 +16,10 @@ services:
|
|||||||
app.collection_service:
|
app.collection_service:
|
||||||
class: AppBundle\Service\CollectionService
|
class: AppBundle\Service\CollectionService
|
||||||
arguments: ['@doctrine.orm.entity_manager', '@manager.brickset','@manager.rebrickable']
|
arguments: ['@doctrine.orm.entity_manager', '@manager.brickset','@manager.rebrickable']
|
||||||
app.model_loader_service:
|
|
||||||
class: AppBundle\Service\ModelLoaderService
|
loader.rebrickable:
|
||||||
arguments: ['@doctrine.orm.entity_manager','%kernel.root_dir%/../var/data/LDrawLibrary', '@manager.rebrickable']
|
class: AppBundle\Service\RebrickableLoader
|
||||||
|
arguments: ['@doctrine.orm.entity_manager', '@manager.rebrickable']
|
||||||
|
|
||||||
app.form.filter_set:
|
app.form.filter_set:
|
||||||
class: AppBundle\Form\FilterSetType
|
class: AppBundle\Form\FilterSetType
|
||||||
|
@ -11,7 +11,7 @@ class LoadRebrickableCommand extends ContainerAwareCommand
|
|||||||
protected function configure()
|
protected function configure()
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->setName('app:loadRebrickable')
|
->setName('app:load:rebrickable')
|
||||||
->setDescription('Loads Rebrickable csv data')
|
->setDescription('Loads Rebrickable csv data')
|
||||||
->setHelp('This command allows you to..')
|
->setHelp('This command allows you to..')
|
||||||
;
|
;
|
||||||
@ -19,16 +19,16 @@ class LoadRebrickableCommand extends ContainerAwareCommand
|
|||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$modelLoader = $this->getContainer()->get('app.model_loader_service');
|
$rebrickableLoader = $this->getContainer()->get('loader.rebrickable');
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (\Exception $e) {
|
||||||
printf($e->getMessage());
|
printf($e->getMessage());
|
||||||
}
|
}
|
||||||
|
@ -6,21 +6,14 @@ use AppBundle\Api\Manager\RebrickableManager;
|
|||||||
use AppBundle\Entity\Category;
|
use AppBundle\Entity\Category;
|
||||||
use AppBundle\Entity\Color;
|
use AppBundle\Entity\Color;
|
||||||
use AppBundle\Entity\Keyword;
|
use AppBundle\Entity\Keyword;
|
||||||
use AppBundle\Entity\Model;
|
|
||||||
use AppBundle\Entity\BuildingKit;
|
use AppBundle\Entity\BuildingKit;
|
||||||
use AppBundle\Entity\Part;
|
use AppBundle\Entity\Part;
|
||||||
use AppBundle\Entity\Part_BuildingKit;
|
use AppBundle\Entity\Part_BuildingKit;
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
use Symfony\Component\Asset\Exception\LogicException;
|
|
||||||
use Symfony\Component\Console\Helper\ProgressBar;
|
use Symfony\Component\Console\Helper\ProgressBar;
|
||||||
use Symfony\Component\Finder\Finder;
|
|
||||||
use Symfony\Component\Finder\SplFileInfo;
|
|
||||||
|
|
||||||
|
class RebrickableLoader
|
||||||
class ModelLoaderService
|
|
||||||
{
|
{
|
||||||
private $STLlib;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var EntityManager
|
* @var EntityManager
|
||||||
*/
|
*/
|
||||||
@ -34,65 +27,12 @@ class ModelLoaderService
|
|||||||
/**
|
/**
|
||||||
* ModelLoaderService constructor.
|
* ModelLoaderService constructor.
|
||||||
*/
|
*/
|
||||||
public function __construct($em, $STLlib, $rebrickableManager)
|
public function __construct($em, $rebrickableManager)
|
||||||
{
|
{
|
||||||
$this->STLlib = $STLlib;
|
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->rebrickableManager = $rebrickableManager;
|
$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)
|
public function loadPartBuildingKits($output)
|
||||||
{
|
{
|
||||||
$partRepository = $this->em->getRepository('AppBundle:Part');
|
$partRepository = $this->em->getRepository('AppBundle:Part');
|
Loading…
x
Reference in New Issue
Block a user