Created
March 1, 2019 10:18
-
-
Save kolomiec-valeriy/c425e190b8f42e8440240f78e2db9109 to your computer and use it in GitHub Desktop.
Export books with chapters to json files in Zip apchive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Entity\BibleBook; | |
use Symfony\Component\Filesystem\Filesystem; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\Routing\Annotation\Route; | |
class ExportBooksController extends Controller | |
{ | |
/** | |
* @Route("/test") | |
*/ | |
public function test(Filesystem $filesystem) | |
{ | |
$cacheDir = $this->getParameter('kernel.cache_dir'); | |
$books = $this->getDoctrine()->getManager()->getRepository(BibleBook::class)->findAll(); | |
$files = []; | |
foreach ($books as $key => $book) { | |
$bookName = $cacheDir . '/books/book_'. ($key + 1) . '.json'; | |
$json = $this->json( | |
$book, | |
200, | |
[], | |
['single' => true] | |
); | |
$filesystem->dumpFile($bookName, $json->getContent()); | |
$files[] = $bookName; | |
} | |
$zip = new \ZipArchive(); | |
$zipName = $cacheDir . "/books/Books-".(new \DateTime())->format("d-m-Y").'.zip'; | |
$zip->open($zipName, \ZipArchive::CREATE); | |
foreach ($files as $file) { | |
$zip->addFromString(basename($file), file_get_contents($file)); | |
} | |
$zip->close(); | |
return $this->file($zipName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment