Utilities¶
The extension also provides a couple of utility classes. Their features are described here.
Format conversions¶
The \Cobweb\Svconnector\Utility\ConnectorUtility
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 = \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml_cs($phpArray);
Again one’s own conversion method may be used if needed.
Reading files¶
The \Cobweb\Svconnector\Utility\FileUtility
provides a general
method for reading the content of a file. It will transparently handle
the following syntax for pointing to a file:
- an absolute file path (within the TYPO3 root path or
TYPO3_CONF_VARS[BE][lockRootPath]
), e.g./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\Svconnector\Utility\AbstractFileReader
. 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:whatever_you_want
).
Using the \Cobweb\Svconnector\Utility\FileUtility
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 getFileContent()
method will return false
is some error happened reading
the file. An error message is available to retrieve using:
$error = $fileUtility->getError();