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

Update flysystem usage after upgrade #515

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
fail-fast: false
matrix:
operating-system: [ ubuntu-latest ]
php-version: [ '7.4', '8.0', '8.1' ]
php-version: [ '8.1', '8.2', '8.3' ]
include:
- php-version: '8.1'
- php-version: '8.3'
coverage: true

steps:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"require": {
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"oat-sa/lib-generis-search": "^2.1.2",
"oat-sa/generis": ">=15.24",
"oat-sa/generis": "dev-feat/REL-1723/update-flysystem-lib as 16.0.0",
"oat-sa/tao-core": ">=54.0.0",
"oat-sa/extension-tao-item": ">=v12.1.0",
"oat-sa/extension-tao-itemqti": ">=29.14.5",
Expand Down
8 changes: 4 additions & 4 deletions model/export/service/SharedStimulusCSSExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace oat\taoMediaManager\model\export\service;

use core_kernel_classes_Resource;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\FilesystemOperator;
use oat\oatbox\filesystem\FileSystemService;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\fileManagement\FlySystemManagement;
Expand All @@ -44,12 +44,12 @@ public function pack(core_kernel_classes_Resource $mediaResource, string $link,
$fs = $this->getFileSystem();
$cssPath = dirname($link) . DIRECTORY_SEPARATOR . StoreService::CSS_DIR_NAME;

if (!$fs->has($cssPath)) {
if (!$fs->directoryExists($cssPath)) {
return;
}

$files = $fs->listContents($cssPath);
if (!count($files)) {
if (!count($files->toArray())) {
return;
}

Expand All @@ -61,7 +61,7 @@ public function pack(core_kernel_classes_Resource $mediaResource, string $link,
}
}

private function getFileSystem(): FilesystemInterface
private function getFileSystem(): FilesystemOperator
{
return $this->getFileSystemService()
->getFileSystem($this->getFlySystemManagement()->getOption(FlySystemManagement::OPTION_FS));
Expand Down
5 changes: 3 additions & 2 deletions model/fileManagement/FlySystemManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace oat\taoMediaManager\model\fileManagement;

use League\Flysystem\FilesystemException;
use oat\oatbox\filesystem\File;
use oat\oatbox\service\ConfigurableService;
use League\Flysystem\Filesystem;
Expand All @@ -37,7 +38,7 @@ class FlySystemManagement extends ConfigurableService implements FileManagement
* @param string|File $fileSource
* @param string $label
* @return string
* @throws \League\Flysystem\FileExistsException
* @throws FilesystemException
*/
public function storeFile($fileSource, $label)
{
Expand All @@ -59,7 +60,7 @@ public function deleteDirectory(string $directoryPath): bool

public function getFileSize($link)
{
return $this->getFilesystem()->getSize($link);
return $this->getFilesystem()->fileSize($link);
}

/**
Expand Down
30 changes: 18 additions & 12 deletions model/sharedStimulus/css/repository/StylesheetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
namespace oat\taoMediaManager\model\sharedStimulus\css\repository;

use core_kernel_classes_Resource;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use oat\generis\model\data\Ontology;
use League\Flysystem\FilesystemInterface;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\MediaService;
use League\Flysystem\FileNotFoundException;
use oat\oatbox\filesystem\FileSystemService;
use oat\taoMediaManager\model\fileManagement\FlySystemManagement;
use oat\taoMediaManager\model\fileManagement\FileSourceUnserializer;
Expand All @@ -48,38 +48,44 @@ public function getPath(string $uri): string
return dirname((string) $link);
}

public function listContents(string $path): array
public function listContents(string $path): iterable
{
return $this->getFileSystem()->listContents($path);
}

/**
* @throws FileNotFoundException
* @throws FilesystemException
*/
public function read(string $path): string
{
return $this->getFileSystem()->read($path);
}

public function put(string $path, string $contents): bool
/**
* @throws FilesystemException
*/
public function write(string $path, string $contents): void
{
return $this->getFileSystem()->put($path, $contents);
$this->getFileSystem()->write($path, $contents);
}

public function putStream(string $path, $streamResource): bool
/**
* @throws FilesystemException
*/
public function writeStream(string $path, $streamResource): void
{
return $this->getFileSystem()->putStream($path, $streamResource);
$this->getFileSystem()->writeStream($path, $streamResource);
}

/**
* @throws FileNotFoundException
* @throws FilesystemException
*/
public function delete(string $path): bool
public function delete(string $path): void
{
return $this->getFileSystem()->delete($path);
$this->getFileSystem()->delete($path);
}

private function getFileSystem(): FilesystemInterface
private function getFileSystem(): FilesystemOperator
{
$flySystemManagementFs = $this->getFlySystemManagement()->getOption(FlySystemManagement::OPTION_FS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getList(ListStylesheets $listStylesheetsDTO): array
$path = $stylesheetRepository->getPath($listStylesheetsDTO->getUri());
$list = $stylesheetRepository->listContents(
$path . DIRECTORY_SEPARATOR . StylesheetRepository::STYLESHEETS_DIRECTORY
);
)->toArray();
/**
* here sorting files by creation date so that in case of css .selector collisions
* the rules will be applied from the last stylesheet added to the passage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace oat\taoMediaManager\model\sharedStimulus\css\service;

use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemException;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\sharedStimulus\css\dto\LoadStylesheet;
use oat\taoMediaManager\model\sharedStimulus\css\repository\StylesheetRepository;
Expand All @@ -45,7 +45,7 @@ public function load(LoadStylesheet $loadStylesheetDTO): array
);

return $this->cssToArray($content);
} catch (FileNotFoundException $e) {
} catch (FilesystemException $e) {
$this->logDebug(
sprintf(
'Passage %s does not contain stylesheet %s. An empty array will be returned.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace oat\taoMediaManager\model\sharedStimulus\css\service;

use Exception;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemException;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\sharedStimulus\css\dto\SaveStylesheetClasses;
use oat\taoMediaManager\model\sharedStimulus\css\repository\StylesheetRepository;
Expand Down Expand Up @@ -52,7 +52,7 @@ public function save(SaveStylesheetClasses $saveStylesheetClassesDTO): void
}

$content = $this->getCssContentFromArray($cssClassesArray);
$this->getStylesheetRepository()->put(
$this->getStylesheetRepository()->write(
$path . DIRECTORY_SEPARATOR . $saveStylesheetClassesDTO->getStylesheetUri(),
$content
);
Expand All @@ -62,7 +62,7 @@ private function removeStoredStylesheet(string $path): void
{
try {
$this->getStylesheetRepository()->delete($path);
} catch (FileNotFoundException $exception) {
} catch (FilesystemException $exception) {
$this->logDebug(sprintf('Stylesheet %s to delete was not found when trying to clear styles', $path));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function save(UploadedStylesheet $uploadedStylesheetDTO): array

$tmpResource = $uploadedStylesheetDTO->getFileResource();
$size = filesize($uploadedStylesheetDTO->getTmpFileLink());
$this->getStylesheetRepository()->putStream($link, $tmpResource);
$this->getStylesheetRepository()->writeStream($link, $tmpResource);

if (is_resource($tmpResource)) {
fclose($tmpResource);
Expand Down
3 changes: 1 addition & 2 deletions model/sharedStimulus/factory/CommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace oat\taoMediaManager\model\sharedStimulus\factory;

use League\Flysystem\FilesystemInterface;
use oat\generis\model\fileReference\FileReferenceSerializer;
use oat\oatbox\filesystem\Directory;
use oat\oatbox\filesystem\FileSystemService;
Expand All @@ -38,7 +37,7 @@ class CommandFactory extends ConfigurableService
{
public const DEFAULT_DIRECTORY = 'sharedStimulusUploads';

/** @var FilesystemInterface */
/** @var Directory */
private $directory;

public function makeCreateCommandByRequest(ServerRequestInterface $request): CreateCommand
Expand Down
6 changes: 3 additions & 3 deletions model/sharedStimulus/service/PatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use core_kernel_classes_Resource as Resource;
use core_kernel_persistence_Exception;
use InvalidArgumentException;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\FilesystemOperator;
use LogicException;
use oat\generis\model\fileReference\FileReferenceSerializer;
use oat\generis\model\OntologyAwareTrait;
Expand Down Expand Up @@ -82,7 +82,7 @@ public function patch(PatchCommand $command): SharedStimulus
);
$sharedStimulusStoredSourceFile = $this->getFileSourceUnserializer()->unserialize((string)$link);

$this->getFileSystem()->putStream($sharedStimulusStoredSourceFile, $file->readStream());
$this->getFileSystem()->writeStream($sharedStimulusStoredSourceFile, $file->readStream());

$content = $file->read();
$resource->editPropertyValues(
Expand Down Expand Up @@ -161,7 +161,7 @@ private function getFileSourceUnserializer(): FileSourceUnserializer
return $this->getServiceLocator()->get(FileSourceUnserializer::class);
}

private function getFileSystem(): FilesystemInterface
private function getFileSystem(): FilesystemOperator
{
return $this->getFileSystemService()
->getFileSystem($this->getFlySystemManagement()->getOption(FlySystemManagement::OPTION_FS));
Expand Down
12 changes: 6 additions & 6 deletions model/sharedStimulus/service/StoreService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace oat\taoMediaManager\model\sharedStimulus\service;

use League\Flysystem\FilesystemInterface;
use League\Flysystem\FilesystemOperator;
use oat\oatbox\filesystem\File;
use oat\oatbox\filesystem\FileSystemService;
use oat\oatbox\service\ConfigurableService;
Expand Down Expand Up @@ -69,15 +69,15 @@ public function storeStream(
$fs = $this->getFileSystem();

$dirname = $this->getUniqueName($stimulusFilename);
$fs->createDir($dirname);
$fs->createDirectory($dirname);

$fs->putStream(
$fs->writeStream(
$dirname . DIRECTORY_SEPARATOR . $stimulusFilename,
$stimulusXmlStream
);

if (count($cssFiles)) {
$fs->createDir($dirname . DIRECTORY_SEPARATOR . self::CSS_DIR_NAME);
$fs->createDirectory($dirname . DIRECTORY_SEPARATOR . self::CSS_DIR_NAME);
foreach ($cssFiles as $file) {
if (!file_exists($file)) {
$this->getLogger()->notice(sprintf("file %s does not exist", $file));
Expand All @@ -89,7 +89,7 @@ public function storeStream(
continue;
}

$fs->putStream(
$fs->writeStream(
$dirname . DIRECTORY_SEPARATOR . self::CSS_DIR_NAME . DIRECTORY_SEPARATOR . basename($file),
fopen($file, 'r')
);
Expand All @@ -104,7 +104,7 @@ protected function getUniqueName(string $name): string
return uniqid(hash('crc32', $name));
}

private function getFileSystem(): FilesystemInterface
private function getFileSystem(): FilesystemOperator
{
return $this->getFileSystemService()
->getFileSystem($this->getFlySystemManagement()->getOption(FlySystemManagement::OPTION_FS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace oat\taoMediaManager\test\unit\model\export;

use core_kernel_classes_Resource;
use League\Flysystem\DirectoryListing;
use oat\generis\test\MockObject;
use oat\generis\test\TestCase;
use oat\oatbox\filesystem\FileSystem;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function testPack(string $link, string $expectedDir, array $fileNames, ar
$fileSystemMock = $this->initFileSystemMock();

$fileSystemMock->expects(self::once())
->method('has')
->method('directoryExists')
->with($expectedDir)
->willReturn(true);

Expand All @@ -72,7 +73,7 @@ public function testPack(string $link, string $expectedDir, array $fileNames, ar

$fileSystemMock->expects(self::once())
->method('listContents')
->willReturn($fileNames);
->willReturn(new DirectoryListing($fileNames));


$sharedStimulusCSSExporterService = $this->getPreparedServiceInstance($fileSystemMock);
Expand Down Expand Up @@ -131,7 +132,7 @@ private function initFileSystemMock(): FileSystem
{
return $this->getMockBuilder(FileSystem::class)
->disableOriginalConstructor()
->onlyMethods(['has', 'listContents', 'read'])
->onlyMethods(['directoryExists', 'listContents', 'read'])
->getMock();
}

Expand Down
7 changes: 3 additions & 4 deletions test/unit/model/sharedStimulus/service/StoreServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ public function testStoreSharedStimulus(): void

$fileSystemMock = $this->initFileSystemMock();
$fileSystemMock->expects(self::once())
->method('createDir')
->method('createDirectory')
->with($fakeUniqueName);

$fileSystemMock->expects(self::once())
->method('putStream')
->willReturn(true);
->method('writeStream');

$stimulusStoreService = $this->getPreparedServiceInstance($fileSystemMock);
$stimulusStoreService->expects(self::once())
Expand All @@ -78,7 +77,7 @@ private function initFileSystemMock(): FileSystem
{
return $this->getMockBuilder(FileSystem::class)
->disableOriginalConstructor()
->onlyMethods(['createDir', 'putStream'])
->onlyMethods(['createDirectory', 'writeStream'])
->getMock();
}

Expand Down
Loading