Skip to content

Commit

Permalink
Tests + PHPStan level 9 + Dependency update
Browse files Browse the repository at this point in the history
- updated minimal required version of `latte` to `^3.0`
- added all missing tests
- changed PHPStan level to `9`
- fixed all PHPStan issues
- updated README
  • Loading branch information
tg666 committed Dec 29, 2022
1 parent 8c49958 commit 845170c
Show file tree
Hide file tree
Showing 46 changed files with 519 additions and 142 deletions.
74 changes: 32 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ extensions:
base_path: /data/files
filesystem:
adapter: League\Flysystem\Local\LocalFilesystemAdapter(%wwwDir%/data/files)
config: # an optional config for filesystem adapter
config: [] # an optional config for filesystem adapter
assets:
path/to/file.png: my/file.png # single file copying
path/to/directory: my-directory # copy whole directory
s3:
config:
host: https://my-buket.s3.amazonaws.com
host: https://my-bucket.s3.amazonaws.com
filesystem:
adapter: League\Flysystem\AwsS3V3\AwsS3V3Adapter(@s3client, my-bucket)
```
Expand All @@ -53,19 +53,18 @@ extensions:
|------------------------|----------------|---------|-------------------------------------------------------------------------------------------------|
| base_path | string | `''` | Base path to a directory where the files are accessible. |
| host | null or string | `null` | Hostname, use if the files are not stored locally or if you want to generate an absolute links. |
| version_parameter_name | `_v` | default | Name of a version parameter in URL. |
| version_parameter_name | string | `_v` | Name of a version parameter in URL. |

### Basic usage

Generated DI Container will contain an autowired services of type `FileStorageProviderInterface` and `FileStorageInterface` (the default storage).

```php
<?php

use Nette\DI\Container;
use SixtyEightPublishers\FileStorage\FileStorageInterface;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;

/** @var \Nette\DI\Container $container */
/** @var Container $container */

$defaultStorage = $container->getByType(FileStorageInterface::class);

Expand All @@ -80,11 +79,11 @@ $s3storage = $provider->get('s3');
#### Persisting files

```php
<?php
use SixtyEightPublishers\FileStorage\FileStorageInterface;

/** @var \SixtyEightPublishers\FileStorage\FileStorageInterface $storage */
/** @var FileStorageInterface $storage */

# Create resource from local file:
# Create a resource from a local file:
$resource = $storage->createResourceFromLocalFile(
$storage->createPathInfo('test/invoice.pdf'),
__DIR__ . '/path/to/invoice.pdf'
Expand All @@ -106,9 +105,9 @@ $storage->save($resource->withPathInfo(
#### Check a file existence

```php
<?php
use SixtyEightPublishers\FileStorage\FileStorageInterface;

/** @var \SixtyEightPublishers\FileStorage\FileStorageInterface $storage */
/** @var FileStorageInterface $storage */

if ($storage->exists($storage->createPathInfo('test/invoice.pdf'))) {
echo 'file exists!';
Expand All @@ -118,19 +117,19 @@ if ($storage->exists($storage->createPathInfo('test/invoice.pdf'))) {
#### Deleting files

```php
<?php
use SixtyEightPublishers\FileStorage\FileStorageInterface;

/** @var \SixtyEightPublishers\FileStorage\FileStorageInterface $storage */
/** @var FileStorageInterface $storage */

$storage->delete($storage->createPathInfo('test/invoice.pdf'));
```

#### Create links to files

```php
<?php
use SixtyEightPublishers\FileStorage\FileStorageInterface;

/** @var \SixtyEightPublishers\FileStorage\FileStorageInterface $storage */
/** @var FileStorageInterface $storage */

# /data/files/test/invoice.pdf
echo $storage->link($storage->createPathInfo('test/invoice.pdf'));
Expand All @@ -145,43 +144,41 @@ echo $fileInfo->link();
#### Cleaning the storage

```php
<?php

use Nette\DI\Container;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;
use SixtyEightPublishers\FileStorage\Cleaner\StorageCleanerInterface;

/** @var \Nette\DI\Container $container */
/** @var Container $container */

$cleaner = $container->getByType(StorageCleanerInterface::class);
$provider = $container->getByType(FileStorageProviderInterface::class);
$storage = $provider->get('default');

# get files count in specific namespace:
# get files count in the specific namespace:
$cleaner->getCount($storage->getFilesystem(), [
StorageCleanerInterface::OPTION_NAMESPACE => 'test',
]);

# get files count in whole storage:
# get files count in the whole storage:
$cleaner->getCount($storage->getFilesystem());

# remove files in specific namespace:
# remove files in the specific namespace:
$cleaner->clean($storage->getFilesystem(), [
StorageCleanerInterface::OPTION_NAMESPACE => 'test',
]);

# clean whole storage:
# clean the whole storage:
$cleaner->clean($storage->getFilesystem());
```

#### Assets copying

```php
<?php

use Nette\DI\Container;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;
use SixtyEightPublishers\FileStorage\Asset\AssetsCopierInterface;

/** @var \Nette\DI\Container $container */
/** @var Container $container */

$copier = $container->getByType(AssetsCopierInterface::class);
$provider = $container->getByType(FileStorageProviderInterface::class);
Expand All @@ -194,17 +191,12 @@ $copier->copy($provider->get('s3'));
Assets can be defined in the configuration under each storage separately but compiler extensions can define other assets:

```php
<?php

use Nette\DI\CompilerExtension;
use SixtyEightPublishers\FileStorage\Bridge\Nette\DI\Assets;
use SixtyEightPublishers\FileStorage\Bridge\Nette\DI\AssetsProviderInterface;

final class MyCompilerExtension extends CompilerExtension implements AssetsProviderInterface
{
/**
* {@inheritDoc}
*/
public function provideAssets() : array
{
return [
Expand All @@ -222,20 +214,19 @@ final class MyCompilerExtension extends CompilerExtension implements AssetsProvi
The package provides custom Doctrine DBAL type `file_info`. You can register it manually in this way:

```php
<?php

use Doctrine\DBAL\Types\Type;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;
use SixtyEightPublishers\FileStorage\Bridge\Doctrine\DbalType\FileInfoType;

/** @var \SixtyEightPublishers\FileStorage\FileStorageProviderInterface $fileStorageProvider */
/** @var FileStorageProviderInterface $fileStorageProvider */

Type::addType(FileInfoType::NAME, FileInfoType::class);

# this line is important:
Type::getType(FileInfoType::NAME)->setFileStorageProvider($fileStorageProvider);
```

Or you can use a compiler extension `FileStorageDoctrineExtension` but the extension requires an integration of package [68publishers/doctrine-bridge](https://github.com/68publishers/doctrine-bridge).
Or you can use a compiler extension `FileStorageDoctrineExtension`. The extension requires an integration of package [68publishers/doctrine-bridge](https://github.com/68publishers/doctrine-bridge).

```neon
extensions:
Expand All @@ -248,8 +239,6 @@ extensions:
#### Example entity and persistence

```php
<?php

use Doctrine\ORM\Mapping as ORM;
use SixtyEightPublishers\FileStorage\FileInfoInterface;

Expand All @@ -262,10 +251,8 @@ class File

/**
* @ORM\Column(type="file_info")
*
* @var \SixtyEightPublishers\FileStorage\FileInfoInterface
*/
protected $source;
private FileInfoInterface $source;

public function __construct(FileInfoInterface $source)
{
Expand All @@ -280,15 +267,18 @@ class File
```

```php
/** @var Doctrine\ORM\EntityManagerInterface $em */
/** @var \SixtyEightPublishers\FileStorage\FileStorageInterface $storage */
use Doctrine\ORM\EntityManagerInterface;
use SixtyEightPublishers\FileStorage\FileStorageInterface;

/** @var EntityManagerInterface $em */
/** @var FileStorageInterface $storage */

$pathInfo = $storage->createPathInfo('test/avatar.png');
$resource = $storage->createResourceFromLocalFile($pathInfo, __DIR__ . '/path/to/uploaded/file.png');

$storage->save($resource);

$pathInfo->setVersion(time());
$pathInfo = $pathInfo->withVersion(time());
$entity = new File($storage->createFileInfo($pathInfo));

$em->persist($entity);
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
},
"require-dev": {
"68publishers/doctrine-bridge": "^1.0",
"doctrine/dbal": "^2.13.1 || ^3.1.1",
"friendsofphp/php-cs-fixer": "^3.13",
"latte/latte": "^2.7 || ^3.0",
"latte/latte": "^3.0",
"league/flysystem-memory": "^3.10",
"mockery/mockery": "^1.5",
"nette/application": "^3.1.8",
"nette/bootstrap": "^3.1",
"nette/di": "^3.0.10",
"nette/tester": "^2.4.3",
Expand All @@ -38,7 +40,7 @@
"conflict": {
"nette/di": "<3.0.10",
"nette/schema": "<1.1",
"latte/latte": "<2.7",
"latte/latte": "<3.0",
"68publishers/doctrine-bridge": "<1.0",
"symfony/console": "<5.0"
},
Expand Down
6 changes: 1 addition & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ includes:
- vendor/phpstan/phpstan-nette/rules.neon

parameters:
level: 8
level: 9
paths:
- src
ignoreErrors:
-
message: '~If condition is always false\.~'
path: src/Bridge/Nette/DI/FileStorageLatteExtension.php
5 changes: 4 additions & 1 deletion src/Asset/AssetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function create(FilesystemOperator $localFilesystem, string $from, string

$normalizedDirectory = str_replace('\\', '/', rtrim($from, '\\/')) . '/';

return $localFilesystem->listContents($normalizedDirectory, FilesystemReader::LIST_DEEP)
/** @var array<AssetInterface> $assets */
$assets = $localFilesystem->listContents($normalizedDirectory, FilesystemReader::LIST_DEEP)
->filter(static function (StorageAttributes $attributes) {
return $attributes->isFile();
})
Expand All @@ -40,5 +41,7 @@ public function create(FilesystemOperator $localFilesystem, string $from, string
return new Asset($attributes->path(), $namespace . substr($attributes->path(), strlen($normalizedDirectory)));
})
->toArray();

return $assets;
}
}
12 changes: 10 additions & 2 deletions src/Bridge/Doctrine/DbalType/FileInfoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use SixtyEightPublishers\FileStorage\Exception\RuntimeException;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;
use SixtyEightPublishers\DoctrineBridge\Type\ContainerAwareTypeInterface;
use function assert;
use function is_array;
use function is_string;

class FileInfoType extends JsonType implements ContainerAwareTypeInterface
{
Expand Down Expand Up @@ -41,18 +44,23 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
$fileStorage = $this->getFileStorageProvider()->get();

if (!$value instanceof PathInfoInterface) {
$value = $fileStorage->createPathInfo((string) $value);
assert(is_string($value));
$value = $fileStorage->createPathInfo($value);
}

$value = $fileStorage->createFileInfo($value);
}

return parent::convertToDatabaseValue($value, $platform);
$value = parent::convertToDatabaseValue($value, $platform);
assert(null === $value || is_string($value));

return $value;
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?FileInfoInterface
{
$value = parent::convertToPHPValue($value, $platform);
assert(null === $value || is_array($value));

if (null === $value) {
return null;
Expand Down
22 changes: 0 additions & 22 deletions src/Bridge/Latte/FileStorageLatte2Extension.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/Bridge/Latte/FileStorageLatte3Extension.php

This file was deleted.

0 comments on commit 845170c

Please sign in to comment.