Skip to content

Commit

Permalink
Export: import validation refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chlulei committed Oct 25, 2024
1 parent db03ec2 commit a2ecdba
Show file tree
Hide file tree
Showing 194 changed files with 4,119 additions and 3,643 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

declare(strict_types=1);

use ILIAS\Export\ImportHandler\ilFactory as ilImportHandlerFactory;
use ILIAS\Export\ImportHandler\Factory as ilImportHandlerFactory;
use ILIAS\Export\ImportStatus\ilFactory as ilImportStatusFactory;
use ILIAS\Export\ImportStatus\StatusType;
use ILIAS\Export\ImportStatus\I\ilCollectionInterface as ilImportStatusCollectionInterface;
use ILIAS\Export\Schema\ilXmlSchemaFactory as ilXMLSchemaFactory;

/**
* Description of ilDidacticTemplateImport
Expand Down Expand Up @@ -94,17 +94,16 @@ public function validateImportFile(): ilImportStatusCollectionInterface
$status = new ilImportStatusFactory();
if ($this->getInputType() !== self::IMPORT_FILE) {
return $status->collection()->withAddedStatus($status->handler()
->withType(ImportStatus\StatusType::FAILED)
->withType(StatusType::FAILED)
->withContent($status->content()->builder()->string()
->withString("Invalid import status, import status 'IMPORT_FILE' expected.")));
}
$schema = new ilXMLSchemaFactory();
$import = new ilImportHandlerFactory();
$xml_spl_info = new SplFileInfo($this->getInputFile());
$xsd_spl_info = $schema->getLatest(self::SCHEMA_TYPE);
$xml_file_handler = $import->file()->xml()->withFileInfo($xml_spl_info);
$xsd_file_handler = $import->file()->xsd()->withFileInfo($xsd_spl_info);
return $import->file()->validation()->handler()->validateXMLFile($xml_file_handler, $xsd_file_handler);
$xsd_spl_info = $import->schema()->folder()->handler()->getLatest(self::SCHEMA_TYPE);
$xml_file_handler = $import->file()->xml()->handler()->withFileInfo($xml_spl_info);
$xsd_file_handler = $import->file()->xsd()->handler()->withFileInfo($xsd_spl_info);
return $import->validation()->handler()->validateXMLFile($xml_file_handler, $xsd_file_handler);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions components/ILIAS/Export/README-technical.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ $xml_file_spl = new SplFileInfo('path to my xml file')
$xsd_file_spl = new SplFileInfo('path to my xsd file')

// Initialize a xml/xsd file handler
$import = new \ILIAS\Export\ImportHandler\ilFactory();
$xml_file_handler = $import->file()->xml()->withFileInfo($xml_file_spl);
$xsd_file_handler = $import->file()->xsd()->withFileInfo($xsd_file_spl);
$import = new \ILIAS\Export\ImportHandler\Factory();
$xml_file_handler = $import->file()->xml()->handler()->withFileInfo($xml_file_spl);
$xsd_file_handler = $import->file()->xsd()->hanlder()->withFileInfo($xsd_file_spl);

/** @var \ILIAS\Export\ImportStatus\ilCollection $validation_results */
// Validate
Expand All @@ -274,17 +274,17 @@ $xml_file_spl = new SplFileInfo('path to my xml file')
$xsd_file_spl = new SplFileInfo('path to my xsd file')

// Initialize a xml/xsd file handler
$import = new \ILIAS\Export\ImportHandler\ilFactory();
$xml_file_handler = $import->file()->xml()->withFileInfo($xml_file_spl);
$xsd_file_handler = $import->file()->xsd()->withFileInfo($xsd_file_spl);
$import = new \ILIAS\Export\ImportHandler\Factory();
$xml_file_handler = $import->file()->xml()->handler()->withFileInfo($xml_file_spl);
$xsd_file_handler = $import->file()->xsd()->handler()->withFileInfo($xsd_file_spl);

// Build xPath to xml node
// $path->toString() = '/RootElement/namespace:TargetElement'
/** @var \ILIAS\Export\ImportHandler\File\Path\ilHandler $path */
$path = $import->file()->path()->handler()
/** @var \ILIAS\Export\ImportHandler\Path\Handler $path */
$path = $import->path()->handler()
->withStartAtRoot(true)
->withNode($import->file()->path()->node()->simple()->withName('RootElement'))
->withNode($import->file()->path()->node()->simple()->withName('namespace:TargetElement'));
->withNode($import->path()->node()->simple()->withName('RootElement'))
->withNode($import->path()->node()->simple()->withName('namespace:TargetElement'));

// Because the path contains the namespace 'namespace' we have to add the namespace
// info to the xml file handler
Expand Down
99 changes: 99 additions & 0 deletions components/ILIAS/Export/classes/ImportHandler/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Export\ImportHandler;

use ILIAS\Data\Factory as DataFactory;
use ILIAS\Export\ImportHandler\File\Factory as FileFactory;
use ILIAS\Export\ImportHandler\I\FactoryInterface as ImportHandlerFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\FactoryInterface as FileFactoryInterface;
use ILIAS\Export\ImportHandler\I\Parser\FactoryInterface as ParserFactoryInterface;
use ILIAS\Export\ImportHandler\I\Path\FactoryInterface as PathFactoryInterface;
use ILIAS\Export\ImportHandler\I\Schema\FactoryInterface as SchemaFactoryInterface;
use ILIAS\Export\ImportHandler\I\Validation\FactoryInterface as ValidationFactoryInterface;
use ILIAS\Export\ImportHandler\Parser\Factory as ParserFactory;
use ILIAS\Export\ImportHandler\Path\Factory as PathFactory;
use ILIAS\Export\ImportHandler\Schema\Factory as SchemaFactory;
use ILIAS\Export\ImportHandler\Validation\Factory as ValidationFactory;
use ILIAS\Export\ImportStatus\ilFactory as ImportStatusFactory;
use ilLanguage;
use ilLogger;

class Factory implements ImportHandlerFactoryInterface
{
protected ilLogger $logger;
protected ilLanguage $lng;
protected ImportStatusFactory $import_status_factory;
protected DataFactory $data_factory;

public function __construct()
{
global $DIC;
$this->logger = $DIC->logger()->root();
$this->lng = $DIC->language();
$this->lng->loadLanguageModule("exp");
$this->import_status_factory = new ImportStatusFactory();
$this->data_factory = new DataFactory();
}

public function parser(): ParserFactoryInterface
{
return new ParserFactory(
$this,
$this->logger
);
}

public function file(): FileFactoryInterface
{
return new FileFactory(
$this,
$this->import_status_factory,
$this->logger,
$this->lng,
$this->data_factory
);
}

public function schema(): SchemaFactoryInterface
{
return new SchemaFactory(
$this,
$this->data_factory,
$this->logger
);
}

public function path(): PathFactoryInterface
{
return new PathFactory(
$this->logger
);
}

public function validation(): ValidationFactoryInterface
{
return new ValidationFactory(
$this->import_status_factory,
$this,
$this->logger
);
}
}
80 changes: 80 additions & 0 deletions components/ILIAS/Export/classes/ImportHandler/File/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Export\ImportHandler\File;

use ILIAS\Data\Factory as DataFactory;
use ILIAS\Export\ImportHandler\File\Namespace\Factory as FileNamespaceFactory;
use ILIAS\Export\ImportHandler\File\XML\Factory as XMLFileFactory;
use ILIAS\Export\ImportHandler\File\XSD\Factory as XSDFileFactory;
use ILIAS\Export\ImportHandler\I\FactoryInterface as ImportHandlerFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\FactoryInterface as FileFactory;
use ILIAS\Export\ImportHandler\I\File\Namespace\FactoryInterface as FileNamespaceFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\XML\FactoryInterface as XMLFileFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\XSD\FactoryInterface as XSDFileFactoryInterface;
use ILIAS\Export\ImportStatus\ilFactory as ImportStatusFactory;
use ilLanguage;
use ilLogger;

class Factory implements FileFactory
{
protected ImportHandlerFactoryInterface $import_handler;
protected ilLogger $logger;
protected ilLanguage $lng;
protected ImportStatusFactory $import_status_factory;
protected DataFactory $data_factory;

public function __construct(
ImportHandlerFactoryInterface $import_handler,
ImportStatusFactory $import_status_factory,
ilLogger $logger,
ilLanguage $lng,
DataFactory $data_factory
) {
$this->import_handler = $import_handler;
$this->import_status_factory = $import_status_factory;
$this->logger = $logger;
$this->lng = $lng;
$this->data_factory = $data_factory;
}

public function xml(): XMLFileFactoryInterface
{
return new XMLFileFactory(
$this->import_handler,
$this->import_status_factory,
$this->logger,
$this->lng,
$this->data_factory
);
}

public function xsd(): XSDFileFactoryInterface
{
return new XSDFileFactory(
$this->import_handler
);
}

public function namespace(): FileNamespaceFactoryInterface
{
return new FileNamespaceFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,57 @@

namespace ILIAS\Export\ImportHandler\File;

use ilImportException;
use ILIAS\Export\ImportHandler\I\File\ilHandlerInterface as ilFileHandlerInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilCollectionInterface as ilFileNamespaceCollectionInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilFactoryInterface as ilFileNamespaceFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilHandlerInterface as ilFileNamespaceHandlerInterface;
use ILIAS\Export\ImportHandler\I\File\HandlerInterface as FileHandlerInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\CollectionInterface as FileNamespaceCollectionInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\FactoryInterface as FileNamespaceFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\HandlerInterface as FileNamespaceHandlerInterface;
use SplFileInfo;

class ilHandler implements ilFileHandlerInterface
class Handler implements FileHandlerInterface
{
protected SplFileInfo $xml_file_info;
protected ilFileNamespaceFactoryInterface $namespace;
protected ilFileNamespaceCollectionInterface $namespaces;
protected SplFileInfo $spl_file_info;
protected FileNamespaceFactoryInterface $namespace;
protected FileNamespaceCollectionInterface $namespaces;

public function __construct(
ilFileNamespaceFactoryInterface $namespace
FileNamespaceFactoryInterface $namespace
) {
$this->namespace = $namespace;
$this->namespaces = $namespace->collection();
}

public function withAdditionalNamespace(ilFileNamespaceHandlerInterface $namespace_handler): ilFileHandlerInterface
public function withAdditionalNamespace(FileNamespaceHandlerInterface $namespace_handler): FileHandlerInterface
{
$clone = clone $this;
$clone->namespaces = $clone->namespaces->withElement($namespace_handler);
return $clone;
}

public function getNamespaces(): ilFileNamespaceCollectionInterface
public function getNamespaces(): FileNamespaceCollectionInterface
{
return $this->namespaces;
}

public function withFileInfo(SplFileInfo $file_info): ilFileHandlerInterface
public function withFileInfo(SplFileInfo $file_info): FileHandlerInterface
{
$clone = clone $this;
$clone->xml_file_info = $file_info;
$clone->spl_file_info = $file_info;
return $clone;
}

public function getFileName(): string
{
return $this->xml_file_info->getFilename();
return $this->spl_file_info->getFilename();
}

public function getFilePath(): string
{
return $this->fileExists()
? $this->xml_file_info->getRealPath()
: $this->xml_file_info->getPath() . DIRECTORY_SEPARATOR . $this->xml_file_info->getFilename();
? $this->spl_file_info->getRealPath()
: $this->spl_file_info->getPath() . DIRECTORY_SEPARATOR . $this->spl_file_info->getFilename();
}

public function getSubPathToDirBeginningAtPathEnd(string $dir_name): ilFileHandlerInterface
public function getSubPathToDirBeginningAtPathEnd(string $dir_name): FileHandlerInterface
{
$parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
$trimmed_str = '';
Expand All @@ -84,11 +83,11 @@ public function getSubPathToDirBeginningAtPathEnd(string $dir_name): ilFileHandl
}
}
$clone = clone $this;
$clone->xml_file_info = new SplFileInfo($trimmed_str);
$clone->spl_file_info = new SplFileInfo($trimmed_str);
return $clone;
}

public function getSubPathToDirBeginningAtPathStart(string $dir_name): ilFileHandlerInterface
public function getSubPathToDirBeginningAtPathStart(string $dir_name): FileHandlerInterface
{
$parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
$trimmed_str = '';
Expand All @@ -101,18 +100,18 @@ public function getSubPathToDirBeginningAtPathStart(string $dir_name): ilFileHan
}
}
$clone = clone $this;
$clone->xml_file_info = new SplFileInfo($trimmed_str);
$clone->spl_file_info = new SplFileInfo($trimmed_str);
return $clone;
}

public function getPathToFileLocation(): string
{
return $this->xml_file_info->getPath();
return $this->spl_file_info->getPath();
}

public function fileExists(): bool
{
return $this->xml_file_info->getRealPath() !== false;
return $this->spl_file_info->getRealPath() !== false;
}

public function getPathPart(string $pattern): string|null
Expand Down
Loading

0 comments on commit a2ecdba

Please sign in to comment.