Skip to content

Commit 9679a27

Browse files
committed
Add expression download to return a ressource from a path or url
1 parent 74e939b commit 9679a27

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/ExpressionLanguage/Download.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Satellite\ExpressionLanguage;
6+
7+
use PhpSpec\Locator\Resource;
8+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
9+
10+
final class Download extends ExpressionFunction
11+
{
12+
public function __construct(string $name)
13+
{
14+
parent::__construct(
15+
$name,
16+
function (string $value, string $context = ''): string {
17+
$pattern = <<<'PHP'
18+
(function () use($input) {
19+
$resource = \fopen(%s %s, 'r');
20+
if ($resource === false) {
21+
throw new \RuntimeException('Could not open file.');
22+
}
23+
return $resource;
24+
})()
25+
PHP;
26+
27+
return sprintf($pattern, $value, $context);
28+
},
29+
function (string $value, string $context = '') {
30+
$resource = fopen($context . $value, 'r');
31+
if ($resource === false) {
32+
throw new \RuntimeException('Could not open file.');
33+
}
34+
return $resource;
35+
},
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)