-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
122 additions
and
25 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\NotificationCenterBundle\BulkyItem; | ||
|
||
use Contao\CoreBundle\Filesystem\FilesystemItem; | ||
use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Mime\MimeTypeGuesserInterface; | ||
|
||
class FileItemFactory | ||
{ | ||
public function __construct(private readonly MimeTypeGuesserInterface $mimeTypeGuesser) | ||
{ | ||
} | ||
|
||
public function createFromLocalPath(string $path): FileItem | ||
{ | ||
if (!(new Filesystem())->exists($path)) { | ||
throw new \InvalidArgumentException(\sprintf('The file "%s" does not exist.', $path)); | ||
} | ||
|
||
$name = basename($path); | ||
$mimeType = (string) $this->mimeTypeGuesser->guessMimeType($path); | ||
$size = (int) filesize($path); | ||
|
||
return FileItem::fromPath($path, $name, $mimeType, $size); | ||
} | ||
|
||
public function createFromVfsFilesystemItem(FilesystemItem $file, VirtualFilesystemInterface $virtualFilesystem): FileItem | ||
{ | ||
$stream = $virtualFilesystem->readStream($file->getPath()); | ||
|
||
return FileItem::fromStream($stream, $file->getName(), $file->getMimeType(), $file->getFileSize()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BulkyItem; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Mime\MimeTypes; | ||
use Terminal42\NotificationCenterBundle\BulkyItem\FileItemFactory; | ||
use Terminal42\NotificationCenterBundle\Test\VirtualFilesystemTestTrait; | ||
|
||
class FileItemFactoryTest extends TestCase | ||
{ | ||
use VirtualFilesystemTestTrait; | ||
|
||
public function testCreateFromLocalPath(): void | ||
{ | ||
$factory = new FileItemFactory(new MimeTypes()); | ||
$item = $factory->createFromLocalPath(__DIR__.'/../Fixtures/name.jpg'); | ||
$this->assertSame('name.jpg', $item->getName()); | ||
$this->assertSame('image/jpeg', $item->getMimeType()); | ||
$this->assertSame(333, $item->getSize()); | ||
$this->assertIsResource($item->getContents()); | ||
} | ||
|
||
public function testCreateFromVfsFilesystemItem(): void | ||
{ | ||
$vfsCollection = $this->createVfsCollection(); | ||
$vfs = $vfsCollection->get('files'); | ||
$vfs->write('media/name.jpg', file_get_contents(__DIR__.'/../Fixtures/name.jpg')); | ||
|
||
$item = $vfs->get('media/name.jpg'); | ||
|
||
$factory = new FileItemFactory(new MimeTypes()); | ||
$item = $factory->createFromVfsFilesystemItem($item, $vfs); | ||
$this->assertSame('name.jpg', $item->getName()); | ||
$this->assertSame('image/jpeg', $item->getMimeType()); | ||
$this->assertSame(333, $item->getSize()); | ||
$this->assertIsResource($item->getContents()); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\NotificationCenterBundle\Test; | ||
|
||
use Contao\CoreBundle\Filesystem\Dbafs\DbafsManager; | ||
use Contao\CoreBundle\Filesystem\MountManager; | ||
use Contao\CoreBundle\Filesystem\VirtualFilesystem; | ||
use League\Flysystem\InMemory\InMemoryFilesystemAdapter; | ||
use Terminal42\NotificationCenterBundle\Test\BulkyItem\InMemoryDbafs; | ||
use Terminal42\NotificationCenterBundle\Test\BulkyItem\VirtualFilesystemCollection; | ||
|
||
trait VirtualFilesystemTestTrait | ||
{ | ||
private function createVfsCollection(): VirtualFilesystemCollection | ||
{ | ||
$mountManager = (new MountManager()) | ||
->mount(new InMemoryFilesystemAdapter(), 'files') | ||
->mount(new InMemoryFilesystemAdapter(), 'bulky_item') | ||
; | ||
|
||
$dbafsManager = new DbafsManager(); | ||
$dbafsManager->register(new InMemoryDbafs(), 'files'); | ||
$dbafsManager->register(new InMemoryDbafs(), 'bulky_item'); | ||
|
||
$vfsCollection = new VirtualFilesystemCollection(); | ||
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, 'files')); | ||
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, 'bulky_item')); | ||
$vfsCollection->add(new VirtualFilesystem($mountManager, $dbafsManager, '')); // Global one | ||
|
||
return $vfsCollection; | ||
} | ||
} |