Skip to content

Commit f703ace

Browse files
committed
♻ add separate methods for loading php assets, deprecate load() method
1 parent 2bf3d1c commit f703ace

File tree

6 files changed

+79
-13
lines changed

6 files changed

+79
-13
lines changed

src/Roots/Acorn/Assets/Asset/PhpAsset.php

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,77 @@ class PhpAsset extends Asset
99
/**
1010
* Get the returned value of the asset
1111
*
12+
* @deprecated since 2.1.2. Use require(), requireOnce(), include() or includeOnce() instead.
13+
*
14+
* @param bool $require
15+
* @param bool $once
1216
* @return mixed
1317
*/
1418
public function load($require = false, $once = false)
1519
{
16-
if (! $this->exists()) {
17-
throw new FileNotFoundException("Asset [{$this->path()}] not found.");
18-
}
19-
2020
if ($require) {
2121
return $once
22-
? require_once $this->path()
23-
: require $this->path();
22+
? $this->requireOnce()
23+
: $this->require();
2424
}
2525

2626
return $once
27-
? include_once $this->path()
28-
: include $this->path();
27+
? $this->includeOnce()
28+
: $this->include();
29+
}
30+
31+
/**
32+
* Get the returned value of the asset
33+
*
34+
* @return mixed
35+
*/
36+
public function requireOnce()
37+
{
38+
$this->assertExists();
39+
return require_once $this->path();
40+
}
41+
42+
/**
43+
* Get the returned value of the asset
44+
*
45+
* @return mixed
46+
*/
47+
public function require()
48+
{
49+
$this->assertExists();
50+
return require $this->path();
51+
}
52+
53+
/**
54+
* Get the returned value of the asset
55+
*
56+
* @return mixed
57+
*/
58+
public function includeOnce()
59+
{
60+
$this->assertExists();
61+
return include_once $this->path();
62+
}
63+
64+
/**
65+
* Get the returned value of the asset
66+
*
67+
* @return mixed
68+
*/
69+
public function include()
70+
{
71+
$this->assertExists();
72+
return include $this->path();
73+
}
74+
75+
/**
76+
* Assert that the asset exists.
77+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
78+
*/
79+
protected function assertExists()
80+
{
81+
if (! $this->exists()) {
82+
throw new FileNotFoundException("Asset [{$this->path()}] not found.");
83+
}
2984
}
3085
}

tests/Assets/AssetTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,30 @@
5555
});
5656

5757
it('can include a php asset', function () {
58-
assertMatchesSnapshot($this->assets->asset('bnif.php')->load());
58+
assertMatchesSnapshot($this->assets->asset('bnif.php')->include());
5959
});
6060

6161
it('can include_once a php asset', function () {
62-
assertMatchesSnapshot($this->assets->asset('bnif.php')->load(false, true));
62+
assertMatchesSnapshot($this->assets->asset('bnif.php')->includeOnce());
6363
});
6464

6565
it('can require a php asset', function () {
66-
assertMatchesSnapshot($this->assets->asset('bnif.php')->load(true));
66+
assertMatchesSnapshot($this->assets->asset('bnif.php')->require());
6767
});
6868

6969
it('can require_once a php asset', function () {
70+
assertMatchesSnapshot($this->assets->asset('bnif.php')->requireOnce());
71+
});
72+
73+
it('can load a php asset', function () {
74+
assertMatchesSnapshot($this->assets->asset('bnif.php')->load(false, false));
75+
assertMatchesSnapshot($this->assets->asset('bnif.php')->load(false, true));
76+
assertMatchesSnapshot($this->assets->asset('bnif.php')->load(true, false));
7077
assertMatchesSnapshot($this->assets->asset('bnif.php')->load(true, true));
7178
});
7279

73-
it('can fail to load a php asset', function () {
74-
(new PhpAsset(temp('does/not/exist.php'), 'https://kjo.kjo/'))->load();
80+
it('can fail to include a php asset', function () {
81+
(new PhpAsset(temp('does/not/exist.php'), 'https://kjo.kjo/'))->include();
7582
})->throws(FileNotFoundException::class);
7683

7784
it('can get a relative path', function () {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bnif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bnif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

0 commit comments

Comments
 (0)