From bf1a18e7538131ecd6382e077b30aa675f8cead0 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Mon, 24 Nov 2014 16:45:41 +0800 Subject: [PATCH] I think uploading packs is more or less working. I nutted out a few bugs in a lot of places. I suspect there are many more though. --- Controllers/PackTestController.php | 2 +- Controllers/SimfileController.php | 22 ++++++--- DataAccess/DataMapper/DataMapper.php | 56 ++++++++++++++++++---- DataAccess/DataMapper/Helpers/VOMapsHelper.php | 5 +- Domain/Entities/AbstractEntity.php | 1 + Domain/Entities/StepMania/ISimfile.php | 4 +- Domain/Entities/StepMania/ISimfileBuilder.php | 5 +- Domain/Entities/StepMania/PackBuilder.php | 4 +- Domain/Entities/StepMania/PackFactory.php | 1 + .../Entities/StepMania/PackStepByStepBuilder.php | 4 +- Domain/Entities/StepMania/Simfile.php | 14 +++++- Domain/Entities/StepMania/SimfileBuilder.php | 12 ++++- Domain/Entities/StepMania/SimfileFactory.php | 3 ++ .../StepMania/SimfileStepByStepBuilder.php | 15 ++++-- Domain/VOs/StepMania/StepChart.php | 2 +- Services/BannerExtracter.php | 2 +- Services/SimfileParser.php | 2 +- Services/Uploads/UploadManager.php | 5 +- Services/ZipParser.php | 19 +++++--- config/DataMaps.php | 1 + 20 files changed, 133 insertions(+), 46 deletions(-) diff --git a/Controllers/PackTestController.php b/Controllers/PackTestController.php index 656f9f9..060514a 100644 --- a/Controllers/PackTestController.php +++ b/Controllers/PackTestController.php @@ -17,7 +17,7 @@ class PackTestController implements IDivineController } public function indexAction() { - $pack = $this->_packRepository->findById(1); + $pack = $this->_packRepository->findById(10); echo '
';
         print_r($pack);
diff --git a/Controllers/SimfileController.php b/Controllers/SimfileController.php
index 00dc15c..defa951 100644
--- a/Controllers/SimfileController.php
+++ b/Controllers/SimfileController.php
@@ -10,6 +10,8 @@ use Services\IZipParser;
 use DataAccess\StepMania\ISimfileRepository;
 use DataAccess\StepMania\IPackRepository;
 use DataAccess\IFileRepository;
+use Domain\Entities\StepMania\ISimfile;
+use Domain\Entities\StepMania\IPack;
 
 class SimfileController implements IDivineController
 {
@@ -103,20 +105,26 @@ class SimfileController implements IDivineController
             $zipParser->parse($file);
             
             //save the actual zip in the db
-            //$this->_fileRepository->save($file);  
-            foreach($zipParser->simfiles() as $simfile)
-            {
-                $this->_fileRepository->save($simfile->getBanner());
-                $this->_fileRepository->save($simfile->getSimfile());
-                $this->_simfileRepository->save($simfile);
-            }
+            $this->_fileRepository->save($file);  
             
             if($zipParser->isPack())
             {
+                //XXX: Tricky! pack() uses packbuilder and so returns a new pack each time.
+                //I tried to be clever and call pack() multiple times thinking I was getting the same
+                //object. Should I cache it in zipparser?
                 $pack = $zipParser->pack();
                 $this->_fileRepository->save($pack->getBanner());
                 $this->_packRepository->save($pack);
             }
+            
+            foreach($zipParser->simfiles() as $simfile)
+            {   
+                $banner = $simfile->getBanner() ? $this->_fileRepository->save($simfile->getBanner()) : null;
+                $simfileZip = $simfile->getSimfile() ? $this->_fileRepository->save($simfile->getSimfile()) : null;
+
+                if(isset($pack)) $simfile->addToPack($pack);
+                $this->_simfileRepository->save($simfile);
+            }
         }
     }
 }
diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php
index 24b7994..24a7d5a 100644
--- a/DataAccess/DataMapper/DataMapper.php
+++ b/DataAccess/DataMapper/DataMapper.php
@@ -23,7 +23,7 @@ class DataMapper implements IDataMapper
     public function map($entityName, IQueryBuilder $queryBuilder)
     {
         $queryString = $queryBuilder->buildQuery();
-        
+
         $statement = $this->_db->prepare(sprintf($queryString,
             $this->_maps[$entityName]['table']
         ));
@@ -56,6 +56,12 @@ class DataMapper implements IDataMapper
     public function save(IDivineEntity $entity)
     {
         $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);
+        $mergeMap = array();
+        
+        echo 'pre flattened: 
'; + echo '
';
+        print_r($queries);
+        echo '
'; $flattened = array(); $flattened_tables = array(); @@ -63,26 +69,49 @@ class DataMapper implements IDataMapper { $this_table = $query['table']; $this_columns = $query['columns']; - + $flatten = true; for($i = $index+1; $i $queries[$i]['columns'], 'table' => $queries[$i]['table'], 'prepared' => $prepared, 'id' => $id); + $flatten = false; + } } } if(!in_array($this_table, $flattened_tables)) { - $flattened_tables[] = $this_table; + if($flatten) $flattened_tables[] = $this_table; $prepared = isset($query['prepared']) ? $query['prepared'] : null; $id = isset($query['id']) ? $query['id'] : null; $flattened[] = array('columns' => $this_columns, 'table' => $this_table, 'prepared' => $prepared, 'id' => $id); } } - $queries = array(); + echo 'flattened:
'; + echo '
';
+        print_r($flattened);
+        echo '
'; + $queries = array(); + foreach($flattened as $info) { if(isset($info['id'])) @@ -99,7 +128,7 @@ class DataMapper implements IDataMapper $queries[] = $query; } - + // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE) // { $idMap = []; @@ -123,8 +152,15 @@ class DataMapper implements IDataMapper { $statement = $this->_db->prepare($query); $statement->execute(); - $refIndex = $index+1; - $idMap['INDEX_REF_' . $refIndex] = $this->_db->lastInsertId(); + //$refIndex = $index+1; This was being used as the index for idMap below. I have nfi why I was adding 1. + $idMap['INDEX_REF_' . $index] = $this->_db->lastInsertId(); + + foreach($mergeMap as $oldIndex => $mergedIndex) { + if($mergedIndex == $index) { + $idMap['INDEX_REF_' . $oldIndex] = $idMap['INDEX_REF_' . $index]; + } + } + unset($queries[$index]); } else { //update query so that other references are resolved. @@ -140,8 +176,8 @@ class DataMapper implements IDataMapper $statement->execute(); } //} - - $entity->setId(end($idMap)); + + if(!$entity->getId()) $entity->setId(end($idMap)); return $entity; } diff --git a/DataAccess/DataMapper/Helpers/VOMapsHelper.php b/DataAccess/DataMapper/Helpers/VOMapsHelper.php index 497d5fe..9a1f2cc 100644 --- a/DataAccess/DataMapper/Helpers/VOMapsHelper.php +++ b/DataAccess/DataMapper/Helpers/VOMapsHelper.php @@ -48,10 +48,11 @@ class VOMapsHelper { $className = $maps[$this->_voName]['class']; $table = $maps[$this->_voName]['table']; - + // If the table we already have contains the id of a row we need in // another table - if(isset($row[$this->_tableName . '_id'])) { + //if(isset($row[$this->_tableName . '_id'])) { + if(array_key_exists($this->_tableName . '_id', $row)) { //this is a better choice as somtimes the array key is set, but is equal to null, and isset doesn't like that $join_id = $row[$this->_tableName . '_id']; $statement = $db->prepare(sprintf('SELECT * from %s WHERE id=%u', $table, diff --git a/Domain/Entities/AbstractEntity.php b/Domain/Entities/AbstractEntity.php index 7d36796..c847865 100644 --- a/Domain/Entities/AbstractEntity.php +++ b/Domain/Entities/AbstractEntity.php @@ -2,6 +2,7 @@ namespace Domain\Entities; +use Exception; use Domain\Entities\IDivineEntity; abstract class AbstractEntity implements IDivineEntity diff --git a/Domain/Entities/StepMania/ISimfile.php b/Domain/Entities/StepMania/ISimfile.php index 40e179f..655ad6b 100644 --- a/Domain/Entities/StepMania/ISimfile.php +++ b/Domain/Entities/StepMania/ISimfile.php @@ -3,6 +3,7 @@ namespace Domain\Entities\StepMania; use Domain\VOs\StepMania\IStepChart; +use Domain\Entities\StepMania\IPack; use Domain\Entities\IDivineEntity; interface ISimfile extends IDivineEntity @@ -17,7 +18,8 @@ interface ISimfile extends IDivineEntity public function hasBgChanges(); public function getBanner(); public function getSimfile(); - + public function addToPack(IPack $pack); public function addStepChart(IStepChart $stepChart); public function getSteps(); + public function getPackId(); } \ No newline at end of file diff --git a/Domain/Entities/StepMania/ISimfileBuilder.php b/Domain/Entities/StepMania/ISimfileBuilder.php index 3f76097..ecbe677 100644 --- a/Domain/Entities/StepMania/ISimfileBuilder.php +++ b/Domain/Entities/StepMania/ISimfileBuilder.php @@ -17,8 +17,9 @@ interface ISimfileBuilder public function With_Stops($const); public function With_FgChanges($const); public function With_BgChanges($const); - public function With_Banner(IFile $banner); - public function With_Simfile(Ifile $simfile); + public function With_Banner(IFile $banner = null); + public function With_Simfile(Ifile $simfile = null); + public function With_PackId($packId = null); public function With_Steps(array $steps); public function build(); } \ No newline at end of file diff --git a/Domain/Entities/StepMania/PackBuilder.php b/Domain/Entities/StepMania/PackBuilder.php index 3a52a36..3abb934 100644 --- a/Domain/Entities/StepMania/PackBuilder.php +++ b/Domain/Entities/StepMania/PackBuilder.php @@ -56,6 +56,8 @@ class PackBuilder implements IPackBuilder { return $this->_packFactory->createInstance($this->_title, $this->_uploader, - $this->_simfiles); + $this->_simfiles, + $this->_banner, + $this->_file); } } \ No newline at end of file diff --git a/Domain/Entities/StepMania/PackFactory.php b/Domain/Entities/StepMania/PackFactory.php index b59c49a..dcaf260 100644 --- a/Domain/Entities/StepMania/PackFactory.php +++ b/Domain/Entities/StepMania/PackFactory.php @@ -2,6 +2,7 @@ namespace Domain\Entities\StepMania; +use Domain\Entities\IFile; use Domain\Entities\StepMania\Pack; use Domain\Entities\IUser; diff --git a/Domain/Entities/StepMania/PackStepByStepBuilder.php b/Domain/Entities/StepMania/PackStepByStepBuilder.php index cc53507..3c02ed1 100644 --- a/Domain/Entities/StepMania/PackStepByStepBuilder.php +++ b/Domain/Entities/StepMania/PackStepByStepBuilder.php @@ -71,7 +71,7 @@ class PackStepByStepBuilder_With_Simfiles extends AbstractPackStepByStepBuilder { public function With_Banner(IFile $banner) { - $this->_packBuilder->With_File($banner); + $this->_packBuilder->With_Banner($banner); return $this; } @@ -83,7 +83,7 @@ class PackStepByStepBuilder_With_Simfiles extends AbstractPackStepByStepBuilder public function build() { - return $this->_simfileBuilder + return $this->_packBuilder ->build(); } } diff --git a/Domain/Entities/StepMania/Simfile.php b/Domain/Entities/StepMania/Simfile.php index 243f02f..7071e0b 100644 --- a/Domain/Entities/StepMania/Simfile.php +++ b/Domain/Entities/StepMania/Simfile.php @@ -5,9 +5,9 @@ namespace Domain\Entities\StepMania; use Domain\VOs\StepMania\IArtist; use Domain\VOs\StepMania\IBPM; use Domain\VOs\StepMania\IStepChart; -use Domain\ConstantsAndTypes\SIMFILE_CONSTANT; use Domain\Exception\InvalidStepChartException; use Domain\Entities\StepMania\ISimfile; +use Domain\Entities\StepMania\IPack; use Domain\Entities\IUser; use Domain\Entities\IFile; use Domain\Entities\AbstractEntity; @@ -25,6 +25,7 @@ class Simfile extends AbstractEntity implements ISimfile private $_banner; private $_simfile; private $_steps; + private $_packId; public function __construct( $title, @@ -37,6 +38,7 @@ class Simfile extends AbstractEntity implements ISimfile $bgChanges, IFile $banner = null, IFile $simfile = null, + $packId = null, array $steps ) { $this->_title = $title; @@ -49,6 +51,7 @@ class Simfile extends AbstractEntity implements ISimfile $this->_bgChanges = $bgChanges; $this->_banner = $banner; $this->_simfile = $simfile; + $this->_packId = $packId; foreach($steps as $stepChart) { if(!$stepChart instanceof IStepChart) { @@ -117,4 +120,13 @@ class Simfile extends AbstractEntity implements ISimfile { return $this->_steps; } + + public function addToPack(IPack $pack) { + $this->_packId = $pack->getId(); + } + + public function getPackId() + { + return $this->_packId; + } } diff --git a/Domain/Entities/StepMania/SimfileBuilder.php b/Domain/Entities/StepMania/SimfileBuilder.php index c8afcd2..6c87aa9 100644 --- a/Domain/Entities/StepMania/SimfileBuilder.php +++ b/Domain/Entities/StepMania/SimfileBuilder.php @@ -22,6 +22,7 @@ class SimfileBuilder implements ISimfileBuilder private $_bgChanges; private $_banner; private $_simfile; + private $_packId; private $_steps; //override parent @@ -70,16 +71,22 @@ class SimfileBuilder implements ISimfileBuilder return $this; } - public function With_Banner(IFile $banner) { + public function With_Banner(IFile $banner = null) { $this->_banner = $banner; return $this; } - public function With_Simfile(IFile $simfile) { + public function With_Simfile(IFile $simfile = null) { $this->_simfile = $simfile; return $this; } + public function With_PackId($packId = null) + { + $this->_packId = $packId; + return $this; + } + public function With_Steps(array $steps) { $this->_steps = $steps; return $this; @@ -97,6 +104,7 @@ class SimfileBuilder implements ISimfileBuilder $this->_bgChanges, $this->_banner, $this->_simfile, + $this->_packId, $this->_steps); } } diff --git a/Domain/Entities/StepMania/SimfileFactory.php b/Domain/Entities/StepMania/SimfileFactory.php index 33c939a..eda0ea3 100644 --- a/Domain/Entities/StepMania/SimfileFactory.php +++ b/Domain/Entities/StepMania/SimfileFactory.php @@ -21,6 +21,7 @@ interface ISimfileFactory $bgChanges, IFile $banner, IFile $simfile, + $packId = null, array $steps ); } @@ -38,6 +39,7 @@ class SimfileFactory implements ISimfileFactory $bgChanges, IFile $banner = null, IFile $simfile = null, + $packId = null, array $steps ) { return new Simfile( @@ -51,6 +53,7 @@ class SimfileFactory implements ISimfileFactory $bgChanges, $banner, $simfile, + $packId, $steps ); } diff --git a/Domain/Entities/StepMania/SimfileStepByStepBuilder.php b/Domain/Entities/StepMania/SimfileStepByStepBuilder.php index d0b9971..2408d2f 100644 --- a/Domain/Entities/StepMania/SimfileStepByStepBuilder.php +++ b/Domain/Entities/StepMania/SimfileStepByStepBuilder.php @@ -55,8 +55,9 @@ interface ISimfileStepByStepBuilder_With_BgChanges interface ISimfileStepByStepBuilder_With_Steps { - public function With_Banner(IFile $banner); - public function With_Simfile(IFile $simfile); + public function With_Banner(IFile $banner = null); + public function With_Simfile(IFile $simfile = null); + public function With_PackId($packId = null); public function build(); } @@ -155,18 +156,24 @@ class SimfileStepByStepBuilder_With_BgChanges extends AbstractSimfileStepByStepB class SimfileStepByStepBuilder_With_Steps extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_Steps { - public function With_Banner(IFile $banner) + public function With_Banner(IFile $banner = null) { $this->_simfileBuilder->With_Banner($banner); return new SimfileStepByStepBuilder_With_Steps($this->_simfileBuilder); //TODO: Pretty sure return $this will be OK } - public function With_Simfile(IFile $simfile) + public function With_Simfile(IFile $simfile = null) { $this->_simfileBuilder->With_Simfile($simfile); return new SimfileStepByStepBuilder_With_Steps($this->_simfileBuilder); } + public function With_PackId($packId = null) + { + $this->_simfileBuilder->With_PackId($packId); + return new SimfileStepByStepBuilder_With_Steps($this->_simfileBuilder); + } + public function build() { return $this->_simfileBuilder diff --git a/Domain/VOs/StepMania/StepChart.php b/Domain/VOs/StepMania/StepChart.php index 8951b11..a13dea4 100644 --- a/Domain/VOs/StepMania/StepChart.php +++ b/Domain/VOs/StepMania/StepChart.php @@ -19,7 +19,7 @@ class StepChart implements IStepChart function __construct( DanceMode $mode, Difficulty $difficulty, - StepArtist $artist, + StepArtist $artist = null, $rating ) { $this->mode = $mode; diff --git a/Services/BannerExtracter.php b/Services/BannerExtracter.php index 1fc49f3..2de532d 100644 --- a/Services/BannerExtracter.php +++ b/Services/BannerExtracter.php @@ -84,7 +84,7 @@ class BannerExtracter implements IBannerExtracter } if(!isset($result) || !$result) return null; - + $finfo = new finfo(FILEINFO_MIME); $mimetype = $finfo->file('../files/banners/' . $this->_destinationFileName); $size = filesize('../files/banners/' . $this->_destinationFileName); diff --git a/Services/SimfileParser.php b/Services/SimfileParser.php index bc75ba0..b5c0c64 100644 --- a/Services/SimfileParser.php +++ b/Services/SimfileParser.php @@ -125,7 +125,7 @@ class SimfileParser implements ISimfileParser return new \Domain\VOs\StepMania\StepChart( new \Domain\VOs\StepMania\DanceMode($stepData[0]), new \Domain\VOs\StepMania\Difficulty($stepData[2]), - new \Domain\VOs\StepMania\StepArtist($stepData[1]), + empty($stepData[1]) ? null : new \Domain\VOs\StepMania\StepArtist($stepData[1]), $stepData[3] ); } diff --git a/Services/Uploads/UploadManager.php b/Services/Uploads/UploadManager.php index 7f381c1..8530c2b 100644 --- a/Services/Uploads/UploadManager.php +++ b/Services/Uploads/UploadManager.php @@ -8,7 +8,6 @@ use Services\Uploads\IFile; //This was a bit silly, I have a File object to use with the service but there is also a File entity. //It's confusing but if you pay attention it should be OK. use Domain\Entities\IFileStepByStepBuilder; -use DataAccess\IFileRepository; use Exception; class UploadManager implements IUploadManager{ @@ -18,12 +17,10 @@ class UploadManager implements IUploadManager{ private $_basePath; private $_destination; private $_fileBuilder; - private $_fileRepository; - public function __construct(IFileFactory $fileFactory, IFileStepByStepBuilder $builder, IFileRepository $fileRepository) { + public function __construct(IFileFactory $fileFactory, IFileStepByStepBuilder $builder) { $this->_fileFactory = $fileFactory; $this->_fileBuilder = $builder; - $this->_fileRepository = $fileRepository; if($_FILES) { foreach($_FILES as $file) diff --git a/Services/ZipParser.php b/Services/ZipParser.php index 822c0fd..d0418a7 100644 --- a/Services/ZipParser.php +++ b/Services/ZipParser.php @@ -54,12 +54,13 @@ class ZipParser implements IZipParser { $packname = $this->packNameFromFiles(); $banner = $this->_bannerExtracter->extractPackBanner('../files/StepMania/' . $this->_file->getHash() . '.zip', $packname); - + /* @var $builder \Domain\Entities\StepMania\PackStepByStepBuilder */ $builder = $this->_packBuilder; return $builder->With_Title($packname) ->With_Uploader($this->_userSession->getCurrentUser()) - ->With_Simfiles($this->_smFiles) + //->With_Simfiles($this->_smFiles) + ->With_Simfiles(array()) ->With_Banner($banner) ->With_File($this->_file) ->build(); @@ -89,9 +90,15 @@ class ZipParser implements IZipParser if(pathinfo($stat['name'], PATHINFO_EXTENSION) == 'sm') { $smData = file_get_contents('zip://../files/StepMania/' . $this->_file->getHash() . '.zip#' . $stat['name']); - $this->_smFiles[$stat['name']] = $this->SmDataToSmClass($smData); + $this->_smFiles[$stat['name']] = $smData; } } + + //XXX: Hack. SmDataToSmClass needs to know whether we are dealing with a pack + //or single, but to do that we simply check the number of found sm files. To overcome this + //first populate the smFiles array with the raw sm data, then apply SmDataToSmClass on each + //array element. This way the check is accurate and the array gets populated as expected. + $this->_smFiles = array_map(array($this, 'SmDataToSmClass'), $this->_smFiles); } private function packNameFromFiles() @@ -115,9 +122,9 @@ class ZipParser implements IZipParser { $parser = $this->_smParser; $parser->parse($smData); - $banner = $this->_bannerExtracter->extractSongBanner('../files/StepMania/' . $this->_file->getHash() . '.zip', $parser->banner()); - + $file = $this->isPack() ? null : $this->_file; + return $this->_smBuilder->With_Title($parser->title()) ->With_Artist($parser->artist()) ->With_Uploader($this->_userSession->getCurrentUser()) //obj @@ -127,7 +134,7 @@ class ZipParser implements IZipParser ->With_FgChanges($parser->fgChanges()) ->With_BgChanges($parser->bgChanges()) ->With_Steps($parser->steps()) - ->With_Simfile($this->_file) + ->With_Simfile($file) ->With_Banner($banner) ->build(); } diff --git a/config/DataMaps.php b/config/DataMaps.php index 1d6a2e7..a647632 100644 --- a/config/DataMaps.php +++ b/config/DataMaps.php @@ -22,6 +22,7 @@ return [ 'bgChanges' => DataAccess\Int('bg_changes', 'hasBgChanges'), 'banner' => DataAccess\Entity('File', 'getBanner', 'banner_file'), 'simfile' => DataAccess\Entity('File', 'getSimfile', 'simfile_file'), + 'packId' => DataAccess\Int('pack_id', 'getPackId'), 'steps' => DataAccess\VOArray('StepChart', 'getSteps') ] ], -- 2.11.0