@@ -310,18 +310,10 @@ public function getModuleVersion($moduleName)
310310 return !empty ($ moduleInfo ['setup_version ' ]) ? $ moduleInfo ['setup_version ' ] : '??? ' ;
311311 }
312312
313- protected function getWrapperFilename ($ ajax = false )
313+ protected function getWrapperBaseFilename ($ ajax = false )
314314 {
315315 $ sessionId = $ this ->session ->getSessionId ();
316-
317- $ fileName = 'qdb_register_ ' .$ sessionId .'.json ' ;
318- if ($ ajax ) {
319- $ fileName = $ this ->_getRequest ()->isAjax ()
320- ? 'qdb_ajax_register_ ' .$ sessionId .'_ ' .time ().'.json '
321- : 'qdb_unknown_register_ ' .$ sessionId .'_ ' .time ().'.json ' ;
322- }
323-
324- return $ this ->getQdbTempDir () . $ fileName ;
316+ return 'qdb_register_ ' . (!$ ajax ? 'std ' : 'xhr ' ) . '_ ' . $ sessionId ;
325317 }
326318
327319
@@ -335,34 +327,70 @@ protected function getQdbTempDir()
335327
336328 public function getWrapperContent ($ ajax = false )
337329 {
338- //Clean old files
339- /** @var \SplFileInfo $fileInfo */
330+ //Clean old files
331+ // /** @var \SplFileInfo $fileInfo */
332+ // foreach (new \DirectoryIterator($this->getQdbTempDir()) as $fileInfo) {
333+ // if($fileInfo->isFile() && time() - $fileInfo->getMTime() > 20) {
334+ // //TODO: unlink only files starting with 'qdb_register_' . $sessionId
335+ // unlink($fileInfo->getPathname());
336+ // }
337+ // }
338+
339+ $ wrapperFiles = [];
340+ $ filename = $ this ->getWrapperBaseFilename ($ ajax );
340341 foreach (new \DirectoryIterator ($ this ->getQdbTempDir ()) as $ fileInfo ) {
341- if ($ fileInfo ->isFile () && time () - $ fileInfo ->getMTime () > 20 ) {
342- unlink ( $ fileInfo ->getPathname () );
342+ if ($ fileInfo ->isFile () && strpos ( $ fileInfo ->getFilename (), $ filename )=== 0 ) {
343+ $ wrapperFiles [] = $ fileInfo ->getPathname ();
343344 }
344345 }
345346
346- $ filename = $ this ->getWrapperFilename ($ ajax );
347- if (!file_exists ($ filename )) {
348- throw new LocalizedException (__ ('No file for wrapper ' ));
347+ if (empty ($ wrapperFiles )) {
348+ throw new LocalizedException (__ ('No files for wrapper ' ));
349+ }
350+
351+ $ serializer = new \Magento \Framework \Serialize \Serializer \Json ();
352+
353+ $ content = [];
354+ foreach ($ wrapperFiles as $ wrapperContent ) {
355+ $ jsonContent = file_get_contents ($ wrapperContent );
356+ if ($ jsonContent ) {
357+ foreach ($ serializer ->unserialize ($ jsonContent ) as $ contentKey => $ contentValue ) {
358+ $ content [$ contentKey ] = empty ($ content [$ contentKey ]) ? $ contentValue : array_merge ($ content [$ contentKey ], $ contentValue );
359+ }
360+ }
361+ //TODO: remove foreach
362+ break ;
349363 }
350364
351- $ content = file_get_contents ($ filename );
352365 if (empty ($ content )) {
353366 throw new LocalizedException (__ ('No data registered ' ));
354367 }
355368
369+ /** @var \SplFileInfo $fileInfo */
370+ foreach (new \DirectoryIterator ($ this ->getQdbTempDir ()) as $ fileInfo ) {
371+ if ($ fileInfo ->isFile ()) {
372+ //TODO: unlink only files starting with 'qdb_register_' . $sessionId
373+ unlink ($ fileInfo ->getPathname ());
374+ }
375+ }
376+
356377 return $ content ;
357378 }
358379
359380
360381 public function setWrapperContent ($ content , $ ajax = false )
361382 {
362- file_put_contents ($ this ->getWrapperFilename ($ ajax ), $ content );
383+ $ filename = $ this ->getWrapperBaseFilename ($ ajax );
384+ if ($ ajax ) {
385+ $ filename .= time ();
386+ }
387+ file_put_contents ($ this ->getQdbTempDir () . $ filename . '.json ' , $ content );
363388 }
364389
365390 /**
391+ * TODO: To removed
392+ * Asymmetric behavior frontend/admin is no more necessary
393+ *
366394 * @return bool
367395 * @throws LocalizedException
368396 */
0 commit comments