Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add typehint on adapter #694

Merged
merged 48 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
20b0b04
feat: add typehint on adapter
KevinArtus Apr 21, 2023
ab3604b
Type interface for adapters
KevinArtus Apr 21, 2023
ed22632
Type AsyncAwsS3
KevinArtus Apr 21, 2023
a7db6fd
Type AwsS3
KevinArtus Apr 21, 2023
7ef46f7
Type AzureBlobStorage
KevinArtus Apr 21, 2023
f4065b1
Type DoctrineDbal
KevinArtus Apr 21, 2023
17fb146
fix constructor AsyncAwsS3
KevinArtus Apr 21, 2023
79eda31
Type Flysystem
KevinArtus Apr 21, 2023
b05376b
Type PhpseclibSftp
KevinArtus Apr 24, 2023
20b4484
Type Zip
KevinArtus Apr 24, 2023
f16ad45
Type GridFS and fix declaration checksum
KevinArtus Apr 24, 2023
1a9a23d
fix muchafm review
KevinArtus Jun 2, 2023
a6487f3
refacto: add types on adapter
Apr 24, 2023
7a1b35c
fix: review
May 26, 2023
7bf16a8
fix: review
Jun 2, 2023
aecd1fa
Refacto: Add type on Stream classes
KevinArtus Apr 24, 2023
c7d798a
fix pedro review
KevinArtus May 25, 2023
1597198
refactor: add typeint to FilesystemMap, StreamWrapper and StreamMode
PedroTroller Apr 24, 2023
aa1b69f
Refacto: Add type on Util classes
KevinArtus Apr 24, 2023
b134d9c
fix muchafm review
KevinArtus Apr 28, 2023
d91a8a2
fix: pedro review
Apr 28, 2023
ffe0412
fix pedro's review
KevinArtus Jun 2, 2023
4a4e4ac
refacto: spec types
Apr 28, 2023
c282e47
fix: review
May 26, 2023
af1fda7
refactor: fix Ftp and StreamWrapper errors
PedroTroller Oct 23, 2023
fd3ac32
refactor: fix lvl5 errors for AwsS3 and AzureBlobStorage adapters
PedroTroller Oct 23, 2023
06d948c
ci: upgrade phpstan to 1.10.39
PedroTroller Oct 23, 2023
dd628b0
test: fix filesystem typehint in phpunit tests
PedroTroller Oct 23, 2023
b3fa95d
fix: fix phpspec tests
PedroTroller Oct 23, 2023
4fbaebf
refactor: apply cs fixing
PedroTroller Oct 27, 2023
178fe56
test: fix phpspec tests
PedroTroller Oct 27, 2023
bc76e74
Update src/Gaufrette/Adapter/AwsS3.php
PedroTroller Oct 27, 2023
ec744b9
Update src/Gaufrette/Adapter/PhpseclibSftp.php
PedroTroller Oct 27, 2023
b99b550
Update src/Gaufrette/Adapter/AzureBlobStorage.php
PedroTroller Oct 27, 2023
826acdf
Update src/Gaufrette/Adapter.php
PedroTroller Oct 27, 2023
8e7ecbe
Update src/Gaufrette/Adapter/AzureBlobStorage.php
PedroTroller Oct 27, 2023
c4fba38
Update src/Gaufrette/Adapter/Ftp.php
PedroTroller Oct 27, 2023
3539ec7
Update src/Gaufrette/Adapter/AzureBlobStorage.php
PedroTroller Oct 27, 2023
94d89cd
Update src/Gaufrette/Adapter/DoctrineDbal.php
PedroTroller Oct 27, 2023
bf12aef
refactor: set type for mimetype aware adapters
PedroTroller Oct 30, 2023
74be6f5
docs: add explanation for bitwire value in StreamWrapper
PedroTroller Oct 30, 2023
e2bcd99
fix: introduction of readonly promoted properties
PedroTroller Oct 30, 2023
64a9a23
Update src/Gaufrette/Adapter/AwsS3.php
KevinArtus Oct 30, 2023
1173c0e
Update src/Gaufrette/Stream/InMemoryBuffer.php
PedroTroller Oct 30, 2023
55ad928
Update src/Gaufrette/Stream/Local.php
PedroTroller Oct 30, 2023
3a5592d
Update src/Gaufrette/Stream/Local.php
PedroTroller Oct 30, 2023
b797abe
Update src/Gaufrette/Adapter/AsyncAwsS3.php
KevinArtus Oct 30, 2023
3881d3e
Update src/Gaufrette/Stream/InMemoryBuffer.php
PedroTroller Oct 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 9 additions & 33 deletions src/Gaufrette/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,48 @@ interface Adapter
/**
* Reads the content of the file.
*
* @param string $key
*
* @return string|bool if cannot read content
PedroTroller marked this conversation as resolved.
Show resolved Hide resolved
PedroTroller marked this conversation as resolved.
Show resolved Hide resolved
*/
public function read($key);
public function read(string $key): string|bool;

/**
* Writes the given content into the file.
*
* @param string $key
* @param string $content
*
* @return int|bool The number of bytes that were written into the file
*/
public function write($key, $content);
public function write(string $key, mixed $content): int|bool;
PedroTroller marked this conversation as resolved.
Show resolved Hide resolved

/**
* Indicates whether the file exists.
*
* @param string $key
*
* @return bool
*/
public function exists($key);
public function exists(string $key): bool;

/**
* Returns an array of all keys (files and directories).
*
* @return array
* @return array<int, string>
*/
public function keys();
public function keys(): array;

/**
* Returns the last modified time.
*
* @param string $key
*
* @return int|bool An UNIX like timestamp or false
*/
public function mtime($key);
public function mtime(string $key): int|bool;

/**
* Deletes the file.
*
* @param string $key
*
* @return bool
*/
public function delete($key);
public function delete(string $key): bool;

/**
* Renames a file.
*
* @param string $sourceKey
* @param string $targetKey
*
* @return bool
*/
public function rename($sourceKey, $targetKey);
public function rename(string $sourceKey, string $targetKey): bool;

/**
* Check if key is directory.
*
* @param string $key
*
* @return bool
*/
public function isDirectory($key);
public function isDirectory(string $key): bool;
}
83 changes: 29 additions & 54 deletions src/Gaufrette/Adapter/AsyncAwsS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,24 @@
/**
* Amazon S3 adapter using the AsyncAws.
*
* @author Michael Dowling <[email protected]>
* @author Michael Dowling <[email protected]>
* @author Tobias Nyholm <[email protected]>
*/
class AsyncAwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator, MimeTypeProvider
{
/** @var SimpleS3Client */
protected $service;
/** @var string */
protected $bucket;
/** @var array */
protected $options;
/** @var bool */
protected $bucketExists;
/** @var array */
protected $metadata = [];
/** @var bool */
protected $detectContentType;

/**
* @param SimpleS3Client $service
* @param string $bucket
* @param array $options
* @param bool $detectContentType
*/
public function __construct(SimpleS3Client $service, $bucket, array $options = [], $detectContentType = false)
{
protected array $options;
protected bool $bucketExists;
protected array $metadata = [];

KevinArtus marked this conversation as resolved.
Show resolved Hide resolved
public function __construct(
PedroTroller marked this conversation as resolved.
Show resolved Hide resolved
private readonly SimpleS3Client $service,
muchafm marked this conversation as resolved.
Show resolved Hide resolved
private readonly string $bucket,
array $options = [],
private readonly bool $detectContentType = false
) {
if (!class_exists(SimpleS3Client::class)) {
throw new \LogicException('You need to install package "async-aws/simple-s3" to use this adapter');
}
$this->service = $service;
$this->bucket = $bucket;
$this->options = array_replace(
[
'create' => false,
Expand All @@ -49,14 +36,12 @@ public function __construct(SimpleS3Client $service, $bucket, array $options = [
],
$options
);

$this->detectContentType = $detectContentType;
}

/**
* {@inheritdoc}
*/
public function setMetadata($key, $content)
public function setMetadata(string $key, array $content): void
{
// BC with AmazonS3 adapter
if (isset($content['contentType'])) {
PedroTroller marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -70,15 +55,15 @@ public function setMetadata($key, $content)
/**
* {@inheritdoc}
*/
public function getMetadata($key)
public function getMetadata(string $key): array
{
return $this->metadata[$key] ?? [];
}

/**
* {@inheritdoc}
*/
public function read($key)
public function read(string $key): string|bool
{
$this->ensureBucketExists();
$options = $this->getOptions($key);
Expand All @@ -102,7 +87,7 @@ public function read($key)
/**
* {@inheritdoc}
*/
public function rename($sourceKey, $targetKey)
public function rename(string $sourceKey, string $targetKey): bool
{
$this->ensureBucketExists();
$options = $this->getOptions(
Expand All @@ -121,9 +106,8 @@ public function rename($sourceKey, $targetKey)

/**
* {@inheritdoc}
* @param string|resource $content
*/
public function write($key, $content)
public function write(string $key, mixed $content): int|bool
{
$this->ensureBucketExists();
$options = $this->getOptions($key);
Expand Down Expand Up @@ -153,15 +137,15 @@ public function write($key, $content)
/**
* {@inheritdoc}
*/
public function exists($key)
public function exists(string $key): bool
{
return $this->service->has($this->bucket, $this->computePath($key));
}

/**
* {@inheritdoc}
*/
public function mtime($key)
public function mtime(string $key): int|bool
{
try {
$result = $this->service->headObject($this->getOptions($key));
Expand All @@ -175,14 +159,14 @@ public function mtime($key)
/**
* {@inheritdoc}
*/
public function size($key)
public function size(string $key): int
{
$result = $this->service->headObject($this->getOptions($key));

return (int) $result->getContentLength();
}

public function mimeType($key)
public function mimeType(string $key): string
{
$result = $this->service->headObject($this->getOptions($key));

Expand All @@ -192,15 +176,15 @@ public function mimeType($key)
/**
* {@inheritdoc}
*/
public function keys()
public function keys(): array
{
return $this->listKeys();
}

/**
* {@inheritdoc}
*/
public function listKeys($prefix = '')
public function listKeys(string $prefix = ''): array
{
$this->ensureBucketExists();

Expand All @@ -223,7 +207,7 @@ public function listKeys($prefix = '')
/**
* {@inheritdoc}
*/
public function delete($key)
public function delete(string $key): bool
{
try {
$this->service->deleteObject($this->getOptions($key));
Expand All @@ -237,7 +221,7 @@ public function delete($key)
/**
* {@inheritdoc}
*/
public function isDirectory($key)
public function isDirectory(string $key): bool
{
$result = $this->service->listObjectsV2([
'Bucket' => $this->bucket,
Expand All @@ -261,7 +245,7 @@ public function isDirectory($key)
* @throws \RuntimeException if the bucket does not exists or could not be
* created
*/
protected function ensureBucketExists()
protected function ensureBucketExists(): bool
{
if ($this->bucketExists) {
return true;
Expand All @@ -288,7 +272,7 @@ protected function ensureBucketExists()
return true;
}

protected function getOptions($key, array $options = [])
protected function getOptions(string $key, array $options = []): array
{
$options['ACL'] = $this->options['acl'];
$options['Bucket'] = $this->bucket;
Expand All @@ -303,7 +287,7 @@ protected function getOptions($key, array $options = [])
return $options;
}

protected function computePath($key)
protected function computePath(string $key): string
{
if (empty($this->options['directory'])) {
return $key;
Expand All @@ -314,22 +298,13 @@ protected function computePath($key)

/**
* Computes the key from the specified path.
*
* @param string $path
*
* return string
*/
protected function computeKey($path)
protected function computeKey(string $path): string
{
return ltrim(substr($path, strlen($this->options['directory'])), '/');
}

/**
* @param string|resource $content
*
* @return string
*/
private function guessContentType($content)
private function guessContentType(mixed $content): false|string
KevinArtus marked this conversation as resolved.
Show resolved Hide resolved
{
$fileInfo = new \finfo(FILEINFO_MIME_TYPE);

Expand Down
Loading