Skip to content

Commit

Permalink
[1.x] Upgrade to Flysystem 3 (#442)
Browse files Browse the repository at this point in the history
* Starting the process to update flysystem to 3

* Wrapping up flysystem support for filesystem adapter

* Wrapping up flysystem upgrade

* Wrapping up flysystem migration
  • Loading branch information
srtfisher authored Mar 1, 2024
1 parent 659782d commit 8cf3004
Show file tree
Hide file tree
Showing 12 changed files with 682 additions and 416 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"illuminate/view": "^9.52.15",
"laravel/serializable-closure": "^1.3.1",
"league/commonmark": "^2.4.0",
"league/flysystem": "^1.1.10",
"league/flysystem-cached-adapter": "^1.1",
"league/flysystem": "^3.15",
"monolog/monolog": "^2.9.1",
"nesbot/carbon": "^2.68.1",
"nette/php-generator": "^4.1",
Expand All @@ -51,7 +50,7 @@
"alleyinteractive/alley-coding-standards": "^1.0.1",
"alleyinteractive/wp-match-blocks": "^1.0 || ^2.0 || ^3.0",
"guzzlehttp/guzzle": "^7.7",
"league/flysystem-aws-s3-v3": "^1.0",
"league/flysystem-aws-s3-v3": "^3.15",
"mockery/mockery": "^1.6.6",
"php-stubs/wp-cli-stubs": "^2.8",
"phpstan/phpdoc-parser": "^1.23.1",
Expand Down
156 changes: 156 additions & 0 deletions src/mantle/filesystem/adapter/class-aws-s3-adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* AWS_S3_Adapter class file
*
* @package Mantle
*/

namespace Mantle\Filesystem\Adapter;

use Aws\S3\S3Client;
use DateTimeInterface;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter as S3Adapter;
use League\Flysystem\FilesystemOperator;
use Mantle\Filesystem\Filesystem_Adapter;

/**
* AWS S3 Adapter
*/
class AWS_S3_Adapter extends Filesystem_Adapter {
/**
* The AWS S3 client.
*
* @var \Aws\S3\S3Client
*/
protected $client;

/**
* Create a new AwsS3V3FilesystemAdapter instance.
*
* @param \League\Flysystem\FilesystemOperator $driver
* @param \League\Flysystem\AwsS3V3\AwsS3V3Adapter $adapter
* @param array $config
* @param \Aws\S3\S3Client $client
* @return void
*/
public function __construct( FilesystemOperator $driver, S3Adapter $adapter, array $config, S3Client $client ) {
parent::__construct( $driver, $adapter, $config );

$this->client = $client;
}

/**
* Get the URL for the file at the given path.
*
* @param string $path
* @return string
*/
public function url( string $path ): string {
// If an explicit base URL has been set on the disk configuration then we will use
// it as the base URL instead of the default path. This allows the developer to
// have full control over the base path for this filesystem's generated URLs.
if ( isset( $this->config['url'] ) ) {
return $this->concat_path_to_url( $this->config['url'], $this->prefixer->prefixPath( $path ) );
}

return $this->client->getObjectUrl(
$this->config['bucket'],
$this->prefixer->prefixPath( $path )
);
}

/**
* Determine if temporary URLs can be generated.
*
* @return bool
*/
public function provides_temporary_urls(): bool {
return true;
}

/**
* Get a temporary URL for the file at the given path.
*
* @param string $path
* @param \DateTimeInterface $expiration
* @param array $options
* @return string
*/
public function temporary_url( string $path, $expiration, array $options = [] ): string {
$command = $this->client->getCommand(
'GetObject',
array_merge(
[
'Bucket' => $this->config['bucket'],
'Key' => $this->prefixer->prefixPath( $path ),
],
$options
)
);

$uri = $this->client->createPresignedRequest(
$command,
$expiration,
$options
)->getUri();

// If an explicit base URL has been set on the disk configuration then we will use
// it as the base URL instead of the default path. This allows the developer to
// have full control over the base path for this filesystem's generated URLs.
if ( isset( $this->config['temporary_url'] ) ) {
$uri = $this->replace_base_url( $uri, $this->config['temporary_url'] );
}

return (string) $uri;
}

/**
* Get a temporary upload URL for the file at the given path.
*
* @param string $path
* @param \DateTimeInterface $expiration
* @param array $options
* @return array
*/
public function temporary_upload_url( string $path, $expiration, array $options = [] ): array {
$command = $this->client->getCommand(
'PutObject',
array_merge(
[
'Bucket' => $this->config['bucket'],
'Key' => $this->prefixer->prefixPath( $path ),
],
$options
)
);

$signed_request = $this->client->createPresignedRequest(
$command,
$expiration,
$options
);

$uri = $signed_request->getUri();

// If an explicit base URL has been set on the disk configuration then we will use
// it as the base URL instead of the default path. This allows the developer to
// have full control over the base path for this filesystem's generated URLs.
if ( isset( $this->config['temporary_url'] ) ) {
$uri = $this->replace_base_url( $uri, $this->config['temporary_url'] );
}

return [
'url' => (string) $uri,
'headers' => $signed_request->getHeaders(),
];
}

/**
* Get the underlying S3 client.
*
* @return \Aws\S3\S3Client
*/
public function get_client(): S3Client {
return $this->client;
}
}
33 changes: 33 additions & 0 deletions src/mantle/filesystem/adapter/class-local-adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Local_Adapter class file
*
* @package Mantle
*/

namespace Mantle\Filesystem\Adapter;

use Mantle\Filesystem\Filesystem_Adapter;

/**
* Local Filesystem Adapter
*/
class Local_Adapter extends Filesystem_Adapter {
/**
* Get the URL for the file at the given path.
*
* @param string $path
* @return string
*/
public function url( string $path ): string {
// If an explicit base URL has been set on the disk configuration then we
// will use it as the base URL instead of the default path. This allows the
// developer to have full control over the base path for this filesystem's
// generated URLs.
if ( ! empty( $this->config['url'] ) ) {
return $this->concat_path_to_url( $this->config['url'], $path );
}

return rtrim( wp_upload_dir()['baseurl'], '/' ) . '/' . ltrim( $path, '/' );
}
}
15 changes: 15 additions & 0 deletions src/mantle/filesystem/class-file-not-found-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Filesystem_Manager class file
*
* @package Mantle
*/

namespace Mantle\Filesystem;

use Exception;

/**
* File Not Found Exception
*/
class File_Not_Found_Exception extends Exception {}
Loading

0 comments on commit 8cf3004

Please sign in to comment.