Skip to content

Commit 3131ba4

Browse files
committed
Quick resource from factory
1 parent 94f1b27 commit 3131ba4

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/Resources/ResourceFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,15 @@ public static function create(Resource $resource)
119119
$response->setContent($resource->getContent());
120120
return $response;
121121
}
122+
123+
public static function fromFile($fullPath)
124+
{
125+
$resource = self::getExtensionResource(pathinfo($fullPath, PATHINFO_EXTENSION));
126+
if($resource instanceof AbstractResource)
127+
{
128+
$resource->setFilePath($fullPath);
129+
$resource->setContent(file_get_contents($fullPath));
130+
}
131+
return self::create($resource);
132+
}
122133
}

tests/Resources/ResourceFactoryTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Packaged\Dispatch\Tests\Resources;
44

55
use Packaged\Dispatch\Resources\CssResource;
6+
use Packaged\Dispatch\Resources\Image\IconResource;
67
use Packaged\Dispatch\Resources\ResourceFactory;
78
use Packaged\Dispatch\Resources\UnknownResource;
89
use Packaged\Dispatch\Resources\ZipResource;
10+
use Packaged\Helpers\Path;
911
use PHPUnit\Framework\TestCase;
1012
use Symfony\Component\HttpFoundation\Response;
1113

@@ -40,4 +42,13 @@ public function testCreate()
4042
$this->assertInstanceOf(Response::class, $response);
4143
$this->assertEquals($resource->getContentType(), $response->headers->get('Content-Type'));
4244
}
45+
46+
public function testFavicon()
47+
{
48+
$path = Path::system(dirname(__DIR__), '_root', 'public', 'favicon.ico');
49+
$full = ResourceFactory::create(IconResource::fromFilePath($path));
50+
$fromFile = ResourceFactory::fromFile($path);
51+
$this->assertEquals($full, $fromFile);
52+
$this->assertEquals('"40ed4027e1e5f15c19a2fb287bcc3724"', $fromFile->getEtag());
53+
}
4354
}

tests/_root/public/favicon.ico

5.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)