Skip to content

Commit

Permalink
Add expression download to return a ressource from a path or url
Browse files Browse the repository at this point in the history
  • Loading branch information
JoMessina committed Apr 29, 2024
1 parent d4cb395 commit 83a38b5
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/ExpressionLanguage/Download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\ExpressionLanguage;

use PhpSpec\Locator\Resource;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;

final class Download extends ExpressionFunction
{
public function __construct(string $name)
{
parent::__construct(
$name,
function (string $value): string {
$pattern = <<<'PHP'
(function () use ($input) {
$resource = fopen($value, 'r');
if ($resource === false) {
throw new \RuntimeException('Could not open file.');
}
if(!is_readable($resource)) {
throw new \RuntimeException('Could not read the file.');
}
return $ressource;
})()
PHP;

return sprintf($pattern, $value);

Check failure on line 30 in src/ExpressionLanguage/Download.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to sprintf contains 0 placeholders, 1 value given.
},
function (string $value) {
$resource = fopen($value, 'r');
if ($resource === false) {
throw new \RuntimeException('Could not open file.');
}
return $resource;
},
);
}
}

0 comments on commit 83a38b5

Please sign in to comment.