Utilities
The extension also provides a couple of utility classes. Their features are described here.
Format conversions
The \Cobweb\ class provides a
conversion method utility, which transforms a
XML structure into a PHP array without losing any information. This
method is simply called as:
$phpArray = \Cobweb\Svconnector\Utility\ConnectorUtility::convertXmlToArray($xml)
Of course one's own conversion method may be used if needed. The conversion from a PHP array to a XML structure can safely rely on TYPO3 CMS API. e.g.:
$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>' . "\n" . GeneralUtility::array2xml($result);
Again one's own conversion method may be used if needed.
Reading files
The \Cobweb\ provides a general
method for reading the content of a file, even if that is actually an API endpoint.
It will transparently handle the following syntax for pointing to a resource:
- an absolute file path (within the TYPO3 root path or
TYPO3_), e.g.CONF_ VARS [BE] [lock Root Path] /var/foo/ web/ fileadmin/ import/ bar. csv - a file path relative to the TYPO3 root, e.g.
fileadmin/import/ foo. txt - a file reference using the
EXT:syntax, e.g.EXT:foo/ Resources/ Private/ Data/ bar. txt - a fully qualified URL, e.g.
http://www. example. com/ foo. txt - a FAL reference including storage ID and file identifier, e.g.
FAL:2:/ foo. txt - a custom syntax, starting with whatever keyword you want, e.g.
MYKEY:whatever_ you_ want
For the latter, you need to implement a "reader" class which will handle this custom reference.
This class must inherit from \Cobweb\. It must
be declared like a hook, using the custom keyword (without the colon) as a key. Example:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['svconnector']['fileReader']['MYKEY']
The class must implement the read method, which is expected to return the
file's content as a string, or false if some problem happened. An error message
about the problem can be reported from within the reader using:
$this->fileUtility->setError('Some reason here');
The read method receives the full syntax as input (in the above example,
MYKEY:).
Using the \Cobweb\ in your own Connector service
is very easy. Here is how it's done in the "svconnector_csv" extension
$fileUtility = GeneralUtility::makeInstance(\Cobweb\Svconnector\Utility\FileUtility::class);
$fileContent = $fileUtility->getFileContent($parameters['filename']);
The get method will return false if some error happened reading
the file. An error message is available to retrieve using:
$error = $fileUtility->getError();
If you would rather have the content stored into a (temporary) file rather than returned
directly, you can use the get, which will return the
full path to the file where the content is stored. It is up to you to read the file
and delete it once done:
$fileUtility = GeneralUtility::makeInstance(\Cobweb\Svconnector\Utility\FileUtility::class);
// Get the content stored into a temp file
$filename = $fileUtility->getFileAsTemporaryFile($parameters['filename']);
// Read from the file
$content = file_get_contents($filename);
// Remove the file
unlink($filename);
Method get will also return false when something went
wrong reading the distant data.