Skip to content

Commit 83a38b5

Browse files
committed
Add expression download to return a ressource from a path or url
1 parent d4cb395 commit 83a38b5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/ExpressionLanguage/Download.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 {
17+
$pattern = <<<'PHP'
18+
(function () use ($input) {
19+
$resource = fopen($value, 'r');
20+
if ($resource === false) {
21+
throw new \RuntimeException('Could not open file.');
22+
}
23+
if(!is_readable($resource)) {
24+
throw new \RuntimeException('Could not read the file.');
25+
}
26+
return $ressource;
27+
})()
28+
PHP;
29+
30+
return sprintf($pattern, $value);
31+
},
32+
function (string $value) {
33+
$resource = fopen($value, 'r');
34+
if ($resource === false) {
35+
throw new \RuntimeException('Could not open file.');
36+
}
37+
return $resource;
38+
},
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)