Skip to content

Commit

Permalink
Merge pull request #2584 from oat-sa/feat/REL-1723/update-flysystem
Browse files Browse the repository at this point in the history
Update flysystem usage after upgrade
  • Loading branch information
augustas authored Nov 28, 2024
2 parents ae99f60 + dfdf3d2 commit 81bf766
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 87 deletions.
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
"oat-sa/lib-tao-qti": "^7.8.1",
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"naneau/semver": "~0.0.7",
"oat-sa/generis": ">=15.39.0",
"oat-sa/tao-core": ">=54.25.0",
"oat-sa/generis": ">=16.0.0",
"oat-sa/tao-core": ">=54.26.0",
"oat-sa/extension-tao-item": ">=12.4.0",
"oat-sa/extension-tao-test": ">=16.3.0"
},
Expand Down
6 changes: 3 additions & 3 deletions model/Export/AbstractQTIItemExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

namespace oat\taoQtiItem\model\Export;

use oat\oatbox\filesystem\FilesystemException;
use oat\oatbox\reporting\Report;
use oat\tao\helpers\Base64;
use oat\tao\model\media\MediaBrowser;
use oat\taoQtiItem\model\Export\Exception\AssetStylesheetZipTransferException;
use oat\taoQtiItem\model\Export\Stylesheet\AssetStylesheetLoader;
use core_kernel_classes_Property;
use DOMDocument;
use League\Flysystem\FileNotFoundException;
use oat\oatbox\filesystem\Directory;
use oat\oatbox\service\ServiceManager;
use oat\tao\model\media\ProcessedFileStreamAware;
Expand Down Expand Up @@ -180,7 +180,7 @@ public function export($options = [])

try {
$xml = Service::singleton()->getXmlByRdfItem($this->getItem());
} catch (FileNotFoundException $e) {
} catch (FilesystemException $e) {
$report->setMessage($this->getExportErrorMessage(__('cannot find QTI XML')));
$report->setType(\common_report_Report::TYPE_ERROR);
return $report;
Expand Down Expand Up @@ -371,7 +371,7 @@ private function addAssetStylesheetToZip(string $link, string $baseDirectoryName
foreach ($assetStylesheets as $stylesheetFile) {
$this->addFile(
$stylesheetFile['stream'],
$this->buildAssetStylesheetPath($basepath, $baseDirectoryName, $stylesheetFile['basename'])
$this->buildAssetStylesheetPath($basepath, $baseDirectoryName, basename($stylesheetFile['path']))
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions model/Export/ExportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use common_exception_UserReadableException;
use core_kernel_classes_Class;
use core_kernel_classes_Resource;
use League\Flysystem\FileNotFoundException;
use oat\oatbox\filesystem\FilesystemException;
use oat\tao\model\export\ExportElementException;
use oat\taoQtiItem\model\ItemModel;
use oat\taoQtiItem\model\qti\Service;
Expand Down Expand Up @@ -202,7 +202,7 @@ protected function isInstanceValid($item)
{
try {
$xml = Service::singleton()->getXmlByRdfItem($item);
} catch (FileNotFoundException $e) {
} catch (FilesystemException $e) {
}

if (empty($xml)) {
Expand Down
4 changes: 2 additions & 2 deletions model/Export/QtiPackageExportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
use core_kernel_classes_Resource;
use DomDocument;
use Exception;
use League\Flysystem\FileNotFoundException;
use oat\oatbox\event\EventManagerAwareTrait;
use oat\oatbox\filesystem\FilesystemException;
use oat\oatbox\PhpSerializable;
use oat\oatbox\PhpSerializeStateless;
use oat\oatbox\service\ServiceManager;
Expand Down Expand Up @@ -121,7 +121,7 @@ public function export($formValues, $destination)
$manifest = $exporter->getManifest();

$report->add($subReport);
} catch (FileNotFoundException $e) {
} catch (FilesystemException $e) {
$report->add(Report::createFailure(__('Item "%s" has no xml document', $item->getLabel())));
} catch (Exception $e) {
$report->add(
Expand Down
20 changes: 12 additions & 8 deletions model/Export/Stylesheet/AssetStylesheetLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

namespace oat\taoQtiItem\model\Export\Stylesheet;

use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\StorageAttributes;
use oat\generis\model\OntologyAwareTrait;
use oat\oatbox\filesystem\FilesystemException;
use oat\oatbox\filesystem\FilesystemInterface;
use oat\oatbox\filesystem\FileSystemService;
use oat\oatbox\service\ConfigurableService;

Expand All @@ -51,15 +52,18 @@ public function loadAssetsFromAssetResource(string $link): ?array

$stylesheetPath = $this->buildAssetPathFromPropertyName($property);
try {
$cssFiles = $this->getFileSystem()->listContents($stylesheetPath);
$cssFiles = $this->getFileSystem()->listContents($stylesheetPath)->toArray();
$cssFilesInfo = [];

foreach ($cssFiles as $key => $file) {
$cssFiles[$key]['stream'] = $this->getFileSystem()->readStream(
$stylesheetPath . DIRECTORY_SEPARATOR . $file['basename']
$cssFilesInfo[$key] = $file instanceof StorageAttributes ? $file->jsonSerialize() : $file;
$cssFilesInfo[$key]['stream'] = $this->getFileSystem()->readStream(
$stylesheetPath . DIRECTORY_SEPARATOR . basename($file['path'])
);
}

return $cssFiles;
} catch (FileNotFoundException $exception) {
return $cssFilesInfo;
} catch (FilesystemException $exception) {
$this->getLogger()->notice(
sprintf(
'Stylesheet %s not found for resource %s',
Expand All @@ -80,7 +84,7 @@ private function buildAssetPathFromPropertyName(string $property)
DIRECTORY_SEPARATOR,
[
dirname($property),
self::ASSET_CSS_DIRECTORY_NAME
self::ASSET_CSS_DIRECTORY_NAME,
]
);
}
Expand Down
2 changes: 0 additions & 2 deletions model/Listener/ReplaceCopiedQtiXmlIdentifierListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

use common_Exception;
use core_kernel_persistence_Exception;
use League\Flysystem\FileExistsException;
use oat\oatbox\service\ServiceManager;
use oat\taoItems\model\event\ItemContentClonedEvent;
use oat\taoQtiItem\model\qti\copyist\QtiXmlDataManager;
Expand All @@ -39,7 +38,6 @@ class ReplaceCopiedQtiXmlIdentifierListener
{
/**
* @param ItemContentClonedEvent $itemContentClonedEvent
* @throws FileExistsException
* @throws common_Exception
* @throws core_kernel_persistence_Exception
* @throws tao_models_classes_FileNotFoundException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use common_Exception;
use oat\generis\Helper\UuidPrimaryKeyTrait;
use oat\oatbox\service\ConfigurableService;
use League\Flysystem\FileExistsException;
use oat\oatbox\filesystem\Directory;
use oat\taoQtiItem\model\Export\Stylesheet\AssetStylesheetLoader;
use oat\taoQtiItem\model\pack\QtiAssetPacker\PackedAsset;
Expand Down Expand Up @@ -63,7 +62,7 @@ public function injectNonRDFXincludeRelatedAssets(
if ($stylesheetFiles = $passageStylesheetLoader->loadAssetsFromAssetResource($passageResourceIdentifier)) {
try {
$this->includeSharedStimulusStylesheets($qtiItem, $publicDirectory, $stylesheetFiles, $xInclude);
} catch (QtiModelException | FileExistsException $e) {
} catch (QtiModelException $e) {
$this->logWarning(
sprintf(
'Compilation: Injecting stylesheet for Passage %s failed with message %s',
Expand All @@ -77,7 +76,6 @@ public function injectNonRDFXincludeRelatedAssets(

/**
* @throws QtiModelException
* @throws FileExistsException
* @throws common_Exception
*/
private function includeSharedStimulusStylesheets(
Expand All @@ -98,15 +96,15 @@ private function includeSharedStimulusStylesheets(
foreach ($stylesheetFiles as $stylesheetFile) {
$targetPath = implode(DIRECTORY_SEPARATOR, [
$stylesheetTargetPubDirectory,
$stylesheetFile['basename']
basename($stylesheetFile['path'])
]);

$publicDirectory->getFile($targetPath)->write($stylesheetFile['stream']);

$qtiStylesheet = new Stylesheet(
[
'href' => $targetPath,
'title' => $stylesheetFile['basename'],
'title' => basename($stylesheetFile['path']),
'type' => 'text/css'
]
);
Expand Down
4 changes: 2 additions & 2 deletions model/flyExporter/extractor/QtiExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace oat\taoQtiItem\model\flyExporter\extractor;

use League\Flysystem\FileNotFoundException;
use oat\oatbox\filesystem\FilesystemException;
use oat\taoQtiItem\model\qti\Service;

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ private function loadXml(\core_kernel_classes_Resource $item)
if (empty($xml)) {
throw new ExtractorException('No content found for item ' . $item->getUri());
}
} catch (FileNotFoundException $e) {
} catch (FilesystemException $e) {
throw new ExtractorException(
'qti.xml file was not found for item ' . $item->getUri() . '; The item might be empty.'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

namespace oat\taoQtiItem\model\portableElement\parser\itemParser;

use League\Flysystem\FileNotFoundException;
use oat\taoQtiItem\model\portableElement\exception\PortableElementInconsistencyModelException;
use oat\taoQtiItem\model\portableElement\element\PortableElementObject;
use oat\taoQtiItem\model\portableElement\model\PortableModelRegistry;
Expand Down Expand Up @@ -276,7 +275,7 @@ protected function parsePortableElement(PortableElementModel $model, Element $po
$configData['paths'][$id] = $this->getSourceAdjustedNodulePath($path);
;
} else {
throw new FileNotFoundException(
throw new \tao_models_classes_FileNotFoundException(
"The portable config {$configFile} references a missing module file "
. "{$id} => {$path}"
);
Expand Down
20 changes: 8 additions & 12 deletions model/portableElement/storage/PortableElementFileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,8 @@ public function registerFiles(PortableElementObject $object, $files, $source)
}

$fileId = $this->getPrefix($object) . $object->getRegistrationFileId($file);
if ($fileSystem->has($fileId)) {
$registered = $fileSystem->updateStream($fileId, $resource);
} else {
$registered = $fileSystem->writeStream($fileId, $resource);
}
$fileSystem->writeStream($fileId, $resource);
$registered = true;
if (is_resource($resource)) {
fclose($resource);
}
Expand All @@ -127,16 +124,15 @@ public function registerFiles(PortableElementObject $object, $files, $source)
*/
public function unregisterFiles(PortableElementObject $object, $files)
{
$deleted = true;
$filesystem = $this->getFileStorage();
foreach ($files as $relPath) {
$fileId = $this->getPrefix($object) . $relPath;
if (!$filesystem->has($fileId)) {
if (!$filesystem->fileExists($fileId)) {
throw new \common_Exception('File does not exists in the filesystem: ' . $relPath);
}
$deleted = $filesystem->delete($fileId);
$filesystem->delete($fileId);
}
return $deleted;
return true;
}

/**
Expand All @@ -146,13 +142,13 @@ public function unregisterFiles(PortableElementObject $object, $files)
*/
public function unregisterAllFiles(PortableElementObject $object)
{
return $this->getFileStorage()->deleteDir($this->getPrefix($object));
return $this->getFileStorage()->deleteDirectory($this->getPrefix($object));
}

public function getFileContentFromModelStorage(PortableElementObject $object, $file)
{
$filePath = $this->getPrefix($object) . $file;
if ($this->getFileStorage()->has($filePath)) {
if ($this->getFileStorage()->fileExists($filePath)) {
return $this->getFileStorage()->read($filePath);
}
throw new PortableElementFileStorageException('Unable to find file "' . $file . '"' .
Expand All @@ -168,7 +164,7 @@ public function getFileContentFromModelStorage(PortableElementObject $object, $f
public function getFileStream(PortableElementObject $object, $file)
{
$filePath = $this->getPrefix($object) . $file;
if ($this->getFileStorage()->has($filePath)) {
if ($this->getFileStorage()->fileExists($filePath)) {
return new Stream($this->getFileStorage()->readStream($filePath));
}
throw new PortableElementFileStorageException($filePath);
Expand Down
6 changes: 3 additions & 3 deletions model/portableElement/storage/PortableElementRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function get($identifier)
{
$fileSystem = $this->getConfigFileSystem();

if ($fileSystem->has($identifier)) {
if (!empty($identifier) && $fileSystem->fileExists($identifier)) {
return json_decode($fileSystem->read($identifier), true);
}

Expand All @@ -145,7 +145,7 @@ private function getAll()

foreach ($contents as $file) {
if ($file['type'] === 'file') {
$identifier = $file['filename'];
$identifier = $file['path'];
$elements[$identifier] = $this->get($identifier);
}
}
Expand All @@ -161,7 +161,7 @@ private function getAll()
*/
private function set($identifier, $value)
{
$this->getConfigFileSystem()->put($identifier, json_encode($value));
$this->getConfigFileSystem()->write($identifier, json_encode($value));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions model/qti/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use common_exception_Error;
use common_exception_NotFound;
use oat\oatbox\filesystem\File;
use oat\oatbox\filesystem\FilesystemException;
use oat\taoQtiItem\model\qti\parser\XmlToItemParser;
use tao_helpers_Uri;
use common_exception_FileSystemError;
Expand All @@ -45,7 +46,6 @@
use common_Exception;
use Exception;
use oat\taoItems\model\media\ItemMediaResolver;
use League\Flysystem\FileNotFoundException;

/**
* The QTI_Service gives you a central access to the managment methods of the
Expand Down Expand Up @@ -99,7 +99,7 @@ public function getDataItemByRdfItem(core_kernel_classes_Resource $item, $langCo
if (!$returnValue->getAttributeValue('xml:lang')) {
$returnValue->setAttribute('xml:lang', \common_session_SessionManager::getSession()->getDataLanguage());
}
} catch (FileNotFoundException $e) {
} catch (FilesystemException $e) {
// fail silently, since file might not have been created yet
// $returnValue is then NULL.
common_Logger::d('item(' . $item->getUri() . ') is empty, newly created?');
Expand Down
2 changes: 1 addition & 1 deletion scripts/fixItemGhostResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

// maybe it's a dirty way but it's quicker. too much modification would have been required in ItemUpdater
$adapter = $dir->getFileSystem()->getAdapter();
if (!$adapter instanceof \League\Flysystem\Adapter\Local) {
if (!$adapter instanceof \League\Flysystem\Local\LocalFilesystemAdapter) {
throw new \Exception(__CLASS__ . ' can only handle local files');
}

Expand Down
Loading

0 comments on commit 81bf766

Please sign in to comment.