Skip to content

Commit 7ace5a2

Browse files
authored
Merge pull request #34 from tattersoftware/serial
Bundle Caching
2 parents 9d6d589 + fdc0e9d commit 7ace5a2

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

src/Bundle.php

+26
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,30 @@ private function toString(?bool $head = null): string
176176

177177
return implode(PHP_EOL, $lines);
178178
}
179+
180+
//--------------------------------------------------------------------
181+
// Caching
182+
//--------------------------------------------------------------------
183+
184+
/**
185+
* Prepares the bundle for caching.
186+
*
187+
* @return Asset[]
188+
*/
189+
public function __serialize(): array
190+
{
191+
return $this->getAssets();
192+
}
193+
194+
/**
195+
* Restores the bundle from cached version.
196+
*
197+
* @param Asset[] $data
198+
*/
199+
public function __unserialize(array $data): void
200+
{
201+
foreach ($data as $asset) {
202+
$this->add($asset);
203+
}
204+
}
179205
}

src/RouteBundle.php

-22
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,4 @@ public static function createFromRoute(string $uri): self
6262

6363
return $bundle;
6464
}
65-
66-
/**
67-
* Prepares the bundle for caching.
68-
*
69-
* @return Asset[]
70-
*/
71-
public function __serialize(): array
72-
{
73-
return $this->getAssets();
74-
}
75-
76-
/**
77-
* Prepares the bundle for caching.
78-
*
79-
* @param Asset[] $data
80-
*/
81-
public function __unserialize(array $data): void
82-
{
83-
foreach ($data as $asset) {
84-
$this->add($asset);
85-
}
86-
}
8765
}

tests/BundleTest.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class BundleTest extends AssetsTestCase
1212
{
1313
public function testConstructorPaths()
1414
{
15-
$bundle = new class() extends Bundle {
15+
$bundle = new class () extends Bundle {
1616
protected $paths = ['apple.css'];
1717
};
1818

@@ -25,7 +25,7 @@ public function testConstructorPaths()
2525

2626
public function testConstructorBundles()
2727
{
28-
$bundle = new class() extends Bundle {
28+
$bundle = new class () extends Bundle {
2929
protected $bundles = [FruitSalad::class];
3030
};
3131

@@ -38,7 +38,7 @@ public function testConstructorBundles()
3838

3939
public function testConstructorStrings()
4040
{
41-
$bundle = new class() extends Bundle {
41+
$bundle = new class () extends Bundle {
4242
protected $strings = ['foobar'];
4343
};
4444

@@ -51,7 +51,7 @@ public function testConstructorStrings()
5151

5252
public function testStringable()
5353
{
54-
$asset = new class() extends Bundle {
54+
$asset = new class () extends Bundle {
5555
protected $paths = ['apple.css'];
5656
};
5757

@@ -60,7 +60,7 @@ public function testStringable()
6060

6161
public function testHead()
6262
{
63-
$asset = new class() extends Bundle {
63+
$asset = new class () extends Bundle {
6464
protected $paths = [
6565
'apple.css',
6666
'banana.js',
@@ -69,4 +69,13 @@ public function testHead()
6969

7070
$this->assertSame('<link href="http://example.com/assets/apple.css" rel="stylesheet" type="text/css" />', $asset->head());
7171
}
72+
73+
public function testSerializing()
74+
{
75+
$bundle = new FruitSalad();
76+
77+
$result = unserialize(serialize($bundle));
78+
79+
$this->assertSame($bundle->body(), $result->body());
80+
}
7281
}

tests/RouteBundleTest.php

-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use Tatter\Assets\Asset;
43
use Tatter\Assets\Exceptions\AssetsException;
54
use Tatter\Assets\RouteBundle;
65
use Tests\Support\AssetsTestCase;
@@ -103,15 +102,4 @@ public function testCreateFromRouteThrowsInvalidType()
103102

104103
RouteBundle::createFromRoute('invalid');
105104
}
106-
107-
public function testSerializing()
108-
{
109-
$asset1 = new Asset('banana');
110-
$asset2 = new Asset('bread', false);
111-
$bundle = (new RouteBundle())->add($asset1)->add($asset2);
112-
113-
$result = unserialize(serialize($bundle));
114-
115-
$this->assertSame($bundle->body(), $result->body());
116-
}
117105
}

0 commit comments

Comments
 (0)