Skip to content

Commit

Permalink
[FEATURE] Allow for using another method than GET in FileUtility, res…
Browse files Browse the repository at this point in the history
…olves #29
  • Loading branch information
fsuter committed Nov 10, 2024
1 parent 9ee21c0 commit a798b26
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2024-11-10 Francois Suter (Idéative) <[email protected]>

* Allow for using another method than GET in FileUtility, resolves #29

2024-11-04 Francois Suter (Idéative) <[email protected]>

* Add events for replacing all usual hooks, resolves #31
Expand Down
13 changes: 8 additions & 5 deletions Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function __toString(): string
{
return 'FileUtility';
}

/**
* Reads data from a file pointed to by a versatile URI.
*
Expand All @@ -51,9 +52,10 @@ public function __toString(): string
*
* @param string $uri Address of the file to read
* @param array|null $headers Headers to pass on to the request
* @param string $method Method to use for fetching a remote URI, defaults to GET
* @return string|bool
*/
public function getFileContent(string $uri, ?array $headers = null): bool|string
public function getFileContent(string $uri, ?array $headers = null, string $method = 'GET'): bool|string
{
// Reset the error message
$this->setError('');
Expand Down Expand Up @@ -97,7 +99,7 @@ public function getFileContent(string $uri, ?array $headers = null): bool|string
try {
$response = $requestFactory->request(
$uri,
'GET',
$method,
is_array($headers) ? ['headers' => $headers] : []
);
$data = $response->getBody()->getContents();
Expand Down Expand Up @@ -156,12 +158,13 @@ public function getFileContent(string $uri, ?array $headers = null): bool|string
*
* @param string $uri Address of the file to read
* @param array|null $headers Headers to pass on to the request
* @param string $method Method to use for fetching a remote URI, defaults to GET
* @return string|bool
* @see getFileContent
*/
public function getFileAsTemporaryFile(string $uri, ?array $headers = null): bool|string
public function getFileAsTemporaryFile(string $uri, ?array $headers = null, string $method = 'GET'): bool|string
{
$fileContent = $this->getFileContent($uri, $headers);
$fileContent = $this->getFileContent($uri, $headers, $method);
// Exit early if file content could not be read
if ($fileContent === false) {
return false;
Expand Down Expand Up @@ -199,6 +202,6 @@ public function getError(): string
*/
public function setError(string $error): void
{
$this->error = (string)$error;
$this->error = $error;
}
}
4 changes: 2 additions & 2 deletions Documentation/Developers/Utilities/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Reading files
"""""""""""""

The :code:`\Cobweb\Svconnector\Utility\FileUtility` provides a general
method for reading the content of a file. It will transparently handle
the following syntax for pointing to a file:
method for reading the content of a file, even if that is actually an API endpoint.
It will transparently handle the following syntax for pointing to a resource:

- an absolute file path (within the TYPO3 root path or :code:`TYPO3_CONF_VARS[BE][lockRootPath]`),
e.g. :file:`/var/foo/web/fileadmin/import/bar.csv`
Expand Down

0 comments on commit a798b26

Please sign in to comment.