Skip to content

Commit 4ce09d1

Browse files
committed
4268: Updated fixture loader to allow loading from local files
1 parent 6ecf3b4 commit 4ce09d1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ ElasticSearch.
5757
> [!TIP]
5858
> Use `task fixtures:load` to load all fixtures into Elasticsearch.
5959
60+
<!-- Ignore MD028/no-blanks-blockquote Blank line inside blockquote (cf.
61+
https://github.com/DavidAnson/markdownlint/issues/263) -->
62+
6063
> [!CAUTION]
6164
> If the `task fixtures:load` command (or any `bin/console app:fixtures:load` incantation) fails with an error like
6265
>

src/Fixtures/FixtureLoader.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Elastic\Elasticsearch\Exception\MissingParameterException;
99
use Elastic\Elasticsearch\Exception\ServerResponseException;
1010
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\HttpKernel\Exception\HttpException;
12+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1113
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
1214
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
1315
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
@@ -58,14 +60,28 @@ public function process(string $indexName, string $url): void
5860
* @throws RedirectionExceptionInterface
5961
* @throws ServerExceptionInterface
6062
* @throws TransportExceptionInterface
61-
* @throws \HttpException
63+
* @throws HttpException
6264
*/
6365
private function download(string $url): array
6466
{
67+
// Load from local file if using "file" URL scheme.
68+
if (preg_match('~^file://(?<path>/.+)$~', $url, $matches)) {
69+
$path = $matches['path'];
70+
if (!is_readable($path)) {
71+
throw new NotFoundHttpException('Unable to download fixture data');
72+
}
73+
$data = json_decode(file_get_contents($path), true);
74+
if (empty($data)) {
75+
throw new NotFoundHttpException('Unable to download fixture data');
76+
}
77+
78+
return $data;
79+
}
80+
6581
$response = $this->httpClient->request('GET', $url);
6682

6783
if (Response::HTTP_OK !== $response->getStatusCode()) {
68-
throw new \HttpException('Unable to download fixture data');
84+
throw new NotFoundHttpException('Unable to download fixture data');
6985
}
7086

7187
return $response->toArray();

0 commit comments

Comments
 (0)