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 da3ba77
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ExpressionLanguage/Download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 $context = ''): string {
$pattern = <<<'PHP'
(function () use($input) {
$resource = \fopen(%s %s, 'r');
if ($resource === false) {
throw new \RuntimeException('Could not open file.');
}
return $resource;
})()
PHP;

return sprintf($pattern, $value, $context);
},
function (string $value, string $context = '') {
$resource = fopen($context . $value, 'r');
if ($resource === false) {
throw new \RuntimeException('Could not open file.');
}
return $resource;
},
);
}
}

0 comments on commit da3ba77

Please sign in to comment.